#!/bin/sh
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  echo "Usage: `basename $0` [options] [.exs file] [data]

  -e COMMAND                  Evaluates the given command (*)
  -r FILE                     Requires the given files/patterns (*)
  -S SCRIPT                   Finds and executes the given script in PATH
  -pr FILE                    Requires the given files/patterns in parallel (*)
  -pa PATH                    Prepends the given path to Erlang code path (*)
  -pz PATH                    Appends the given path to Erlang code path (*)

  --app APP                   Starts the given app and its dependencies (*)
  --cookie COOKIE             Sets a cookie for this distributed node
  --detached                  Starts the Erlang VM detached from console
  --erl SWITCHES              Switches to be passed down to Erlang (*)
  --help, -h                  Prints this message and exits
  --hidden                    Makes a hidden node
  --logger-otp-reports BOOL   Enables or disables OTP reporting
  --logger-sasl-reports BOOL  Enables or disables SASL reporting
  --name NAME                 Makes and assigns a name to the distributed node
  --no-halt                   Does not halt the Erlang VM after execution
  --sname NAME                Makes and assigns a short name to the distributed node
  --version, -v               Prints Elixir version and exits
  --werl                      Uses Erlang's Windows shell GUI (Windows only)

** Options marked with (*) can be given more than once
** Options given after the .exs file or -- are passed down to the executed code
** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS or --erl" >&2
  exit 1
fi

readlink_f () {
  cd "$(dirname "$1")" > /dev/null
  filename="$(basename "$1")"
  if [ -h "$filename" ]; then
    readlink_f "$(readlink "$filename")"
  else
    echo "`pwd -P`/$filename"
  fi
}

MODE="elixir"
ERL_EXEC="erl"
ERL=""
I=1

while [ $I -le $# ]; do
  S=1
  eval "PEEK=\${$I}"
  case "$PEEK" in
    +iex)
        MODE="iex"
        ;;
    +elixirc)
        MODE="elixirc"
        ;;
    -v|--compile|--no-halt)
        ;;
    -e|-r|-pr|-pa|-pz|--remsh|--app)
        S=2
        ;;
    --detached|--hidden)
        ERL="$ERL `echo $PEEK | cut -c 2-`"
        ;;
    --cookie)
        I=$(expr $I + 1)
        eval "VAL=\${$I}"
        ERL="$ERL -setcookie "$VAL""
        ;;
    --sname|--name)
        I=$(expr $I + 1)
        eval "VAL=\${$I}"
        ERL="$ERL `echo $PEEK | cut -c 2-` "$VAL""
        ;;
    --logger-otp-reports)
        I=$(expr $I + 1)
        eval "VAL=\${$I}"
        if [ "$VAL" = 'true' ] || [ "$VAL" = 'false' ]; then
            ERL="$ERL -logger handle_otp_reports "$VAL""
        fi
        ;;
    --logger-sasl-reports)
        I=$(expr $I + 1)
        eval "VAL=\${$I}"
        if [ "$VAL" = 'true' ] || [ "$VAL" = 'false' ]; then
            ERL="$ERL -logger handle_sasl_reports "$VAL""
        fi
        ;;
    --erl)
        I=$(expr $I + 1)
        eval "VAL=\${$I}"
        ERL="$ERL "$VAL""
        ;;
    --werl)
        USE_WERL=true
        ;;
    *)
        break
        ;;
  esac
  I=$(expr $I + $S)
done

SELF=$(readlink_f "$0")
SCRIPT_PATH=$(dirname "$SELF")

if [ "$OSTYPE" = "cygwin" ]; then SCRIPT_PATH=$(cygpath -m "$SCRIPT_PATH"); fi
if [ "$MODE" != "iex" ]; then ERL="-noshell -s elixir start_cli $ERL"; fi

# Check for terminal support
if [ "$OS" != "Windows_NT" ]; then
  if test -t 1 -a -t 2; then ERL="-elixir ansi_enabled true $ERL"; fi
fi

if [ "$OS" = "Windows_NT" ] && [ $USE_WERL ]; then
    ERL_EXEC="werl"
fi

if [ -z "$ERL_PATH" ]; then
  if [ -f "$SCRIPT_PATH/../releases/RELEASES" ] && [ -f "$SCRIPT_PATH/erl" ]; then
    ERL_PATH="$SCRIPT_PATH"/"$ERL_EXEC"
  else
    ERL_PATH="$ERL_EXEC"
  fi
fi

exec "$ERL_PATH" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL -extra "$@"
