#!/bin/bash

if [[ "$1" == "--test" ]]; then
	export NODE_APP_INSTANCE=test
  shift
fi

if [[ "$1" == "--dev" ]]; then
	export NODE_ENV=development
  src_dir='src'
  shift
else
	export NODE_ENV=production
  src_dir='dist'
fi

script_dir=$(dirname $0)
script="${script_dir}/${src_dir}/bin/${1}.js"

if [[ -z "$1" ]]; then
  echo "usage: $(basename $0)[--test] [--dev] <command>"
  echo ""
  echo "Available commands are"
  echo ""
  find ${script_dir}/${src_dir}/bin -name \*.js -exec basename {} .js \;
  exit -1
fi

# For scripts that need config access
export NODE_CONFIG_DIR="${script_dir}/config"

if [[ ! -e "${script}" ]]; then
  echo error: Script ${script} does not exist
  exit -1
fi

shift
# See https://stackoverflow.com/questions/448407/bash-script-to-receive-and-repass-quoted-parameters
babel-node -- "${script}" "$@"
