#!/bin/sh
set -e

if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  cat <<USAGE >&2
Usage: $(basename "$0") [options] [.exs file] [data]

The following options are exclusive to IEx:

  --dbg pry           Sets the backend for Kernel.dbg/2 to IEx.pry/0
  --dot-iex "FILE"    Evaluates FILE, line by line, to set up IEx' environment.
                      Defaults to evaluating .iex.exs or ~/.iex.exs, if any exists.
                      If FILE is empty, then no file will be loaded.
  --remsh NAME        Connects to a node using a remote shell.

It accepts all other options listed by "elixir --help".
USAGE
  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
}

SELF=$(readlink_f "$0")
SCRIPT_PATH=$(dirname "$SELF")
exec "$SCRIPT_PATH"/elixir --no-halt --erl "-user elixir" -e ":elixir.start_iex()" +iex "$@"
