#!/bin/bash --posix
#
##############################################################################
# Copyright 2002-2011, LAMP/EPFL
#
# This is free software; see the distribution for copying conditions.
# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
##############################################################################

# Identify the bin dir in the distribution from which this script is running.
bin=`dirname $0`
bin=`cd ${bin} && pwd`

# Set the directory where libraries for scrunch shell live.
SCRUNCH_LIB_DIR="${bin}/../lib"
# Set the conf directory for the scrunch distribution.
SCRUNCH_CONF_DIR="${bin}/../conf"
# Set the main class used to run scrunch shell.
MAIN_CLASS="org.apache.crunch.scrunch.interpreter.InterpreterRunner"

# Not sure what the right default is here: trying nonzero.
scala_exit_status=127
saved_stty=""

# restore stty settings (echo in particular)
function restoreSttySettings() {
  if [[ -n $SCALA_RUNNER_DEBUG ]]; then
    echo "restoring stty: $saved_stty"
  fi

  stty $saved_stty
  saved_stty=""
}

function onExit() {
  if [[ "$saved_stty" != "" ]]; then
    restoreSttySettings
    exit $scala_exit_status
  fi
}

# to reenable echo if we are interrupted before completing.
trap onExit INT

# save terminal settings
saved_stty=$(stty -g 2>/dev/null)
# clear on error so we don't later try to restore them
if [[ ! $? ]]; then
  saved_stty=""
fi
if [[ -n $SCALA_RUNNER_DEBUG ]]; then
  echo "saved stty: $saved_stty"
fi

cygwin=false;
case "`uname`" in
    CYGWIN*) cygwin=true ;;
esac

# Constructing scrunch shell classpath.
SCRUNCH_SHELL_CLASSPATH=""
# Add files in conf dir.
for ext in "$SCRUNCH_CONF_DIR"/* ; do
    if [ -z "$SCRUNCH_SHELL_CLASSPATH" ] ; then
        SCRUNCH_SHELL_CLASSPATH="$ext"
    else
        SCRUNCH_SHELL_CLASSPATH="$SCRUNCH_SHELL_CLASSPATH:$ext"
    fi
done
# Add files in lib dir.
for ext in "$SCRUNCH_LIB_DIR"/*.jar ; do
    if [ -z "$SCRUNCH_SHELL_CLASSPATH" ] ; then
        SCRUNCH_SHELL_CLASSPATH="$ext"
    else
        SCRUNCH_SHELL_CLASSPATH="$SCRUNCH_SHELL_CLASSPATH:$ext"
    fi
done

# Constructing Hadoop classpath.
if [ -z "$HADOOP_HOME" ]; then
    echo "HADOOP_HOME must be set to run the Scrunch shell."
    exit 1
fi
HADOOP_CLASSPATH=`$HADOOP_HOME/bin/hadoop classpath`

CYGWIN_JLINE_TERMINAL=
if $cygwin; then
    if [ "$OS" = "Windows_NT" ] && cygpath -m .>/dev/null 2>/dev/null ; then
        format=mixed
    else
        format=windows
    fi
    SCRUNCH_SHELL_CLASSPATH=`cygpath --path --$format "$SCRUNCH_SHELL_CLASSPATH"`
    case "$TERM" in
        rxvt* | xterm*)
            stty -icanon min 1 -echo
            CYGWIN_JLINE_TERMINAL="-Djline.terminal=scala.tools.jline.UnixTerminal"
        ;;
    esac
fi

[ -n "$JAVA_OPTS" ] || JAVA_OPTS="-Xmx256M -Xms32M"

# break out -D and -J options and add them to JAVA_OPTS as well
# so they reach the underlying JVM in time to do some good.  The
# -D options will be available as system properties.
declare -a java_args
declare -a scala_args

# Don't use the bootstrap classloader.
CPSELECT="-classpath "

while [ $# -gt 0 ]; do
  case "$1" in
    -D*)
      # pass to scala as well: otherwise we lose it sometimes when we
      # need it, e.g. communicating with a server compiler.
      java_args=("${java_args[@]}" "$1")
      scala_args=("${scala_args[@]}" "$1")
      shift
      ;;
    -J*)
      # as with -D, pass to scala even though it will almost
      # never be used.
      java_args=("${java_args[@]}" "${1:2}")
      scala_args=("${scala_args[@]}" "$1")
      shift
      ;;
    -toolcp)
      TOOL_CLASSPATH="$TOOL_CLASSPATH:$2"
      shift 2
      ;;
    *)
      scala_args=("${scala_args[@]}" "$1")
      shift
      ;;
  esac
done

# reset "$@" to the remaining args
set -- "${scala_args[@]}"

if [ -z "$JAVACMD" -a -n "$JAVA_HOME" -a -x "$JAVA_HOME/bin/java" ]; then
    JAVACMD="$JAVA_HOME/bin/java"
fi

"${JAVACMD:=java}" \
  $JAVA_OPTS \
  "${java_args[@]}" \
  ${CPSELECT}${TOOL_CLASSPATH}":"${SCRUNCH_SHELL_CLASSPATH}":"${HADOOP_CLASSPATH} \
  -Dscala.usejavacp=true \
  -Denv.emacs="$EMACS" \
  $CYGWIN_JLINE_TERMINAL \
  $MAIN_CLASS  "$@" \
  -i ${bin}/imports.scala \
  -Yrepl-sync
# The -Yrepl-sync option is a fix for the 2.9.1 REPL. This should probably not be necessary in the future.

# record the exit status lest it be overwritten:
# then reenable echo and propagate the code.
scala_exit_status=$?
onExit
