#!/usr/bin/env bash

# Common options for both scala-cli and java based launchers

#/*--------------------------------------------------------------------------
# *  Credits: This script is based on the script generated by sbt-pack.
# *--------------------------------------------------------------------------*/

# 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

# restore stty settings (echo in particular)
function restoreSttySettings() {
  stty $saved_stty
  saved_stty=""
}

scala_exit_status=127
function onExit() {
  [[ "$saved_stty" != "" ]] && restoreSttySettings
  exit $scala_exit_status
}

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

unset cygwin mingw msys darwin

# COLUMNS is used together with command line option '-pageWidth'.
if command -v tput >/dev/null 2>&1; then
  export COLUMNS="$(tput -Tdumb cols)"
fi

case "`uname`" in
  CYGWIN*) cygwin=true
          ;;
  MINGW*) mingw=true
          ;;
  MSYS*) msys=true
          ;;
  Darwin*) darwin=true
           if [ -z "$JAVA_VERSION" ] ; then
             JAVA_VERSION="CurrentJDK"
           else
            echo "Using Java version: $JAVA_VERSION" 1>&2
           fi
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
           fi
           JAVACMD="`which java`"
           ;;
esac

unset CYGPATHCMD
if [[ ${cygwin-} || ${mingw-} || ${msys-} ]]; then
  # cygpath is used by various windows shells: cygwin, git-sdk, gitbash, msys, etc.
  CYGPATHCMD=`which cygpath 2>/dev/null`
  case "$TERM" in
    rxvt* | xterm* | cygwin*)
      stty -icanon min 1 -echo
      JAVA_OPTS="$JAVA_OPTS -Djline.terminal=unix"
    ;;
  esac
fi

# Resolve JAVA_HOME from javac command path
if [ -z "$JAVA_HOME" ]; then
  javaExecutable="`which javac`"
  if [ -n "$javaExecutable" -a -f "$javaExecutable" -a ! "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
    # readlink(1) is not available as standard on Solaris 10.
    readLink=`which readlink`
    if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
      javaExecutable="`readlink -f \"$javaExecutable\"`"
      javaHome="`dirname \"$javaExecutable\"`"
      javaHome=`expr "$javaHome" : '\(.*\)/bin'`
      JAVA_HOME="$javaHome"
      export JAVA_HOME
    fi
  fi
fi

if [ -z "${JAVACMD-}" ] ; then
  if [ -n "${JAVA_HOME-}"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD="`which java`"
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

if [ -z "$JAVA_HOME" ] ; then
  echo "Warning: JAVA_HOME environment variable is not set."
fi

CLASSPATH_SUFFIX=""
# Path separator used in EXTRA_CLASSPATH
PSEP=":"
PROG_HOME_URI="file://$PROG_HOME"

# translate paths to Windows-mixed format before running java
if [ -n "${CYGPATHCMD-}" ]; then
  [ -n "${PROG_HOME-}" ] &&
    PROG_HOME=`"$CYGPATHCMD" -am "$PROG_HOME"`
    PROG_HOME_URI="file:///$PROG_HOME" # Add extra root dir prefix
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`"$CYGPATHCMD" -am "$JAVA_HOME"`
  CLASSPATH_SUFFIX=";"
  PSEP=";"
elif [[ ${mingw-} || ${msys-} ]]; then
  # For Mingw / Msys, convert paths from UNIX format before anything is touched
  [ -n "$PROG_HOME" ] &&
    PROG_HOME="`(cd "$PROG_HOME"; pwd -W | sed 's|/|\\\\|g')`"
    PROG_HOME_URI="file:///$PROG_HOME" # Add extra root dir prefix
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME="`(cd "$JAVA_HOME"; pwd -W | sed 's|/|\\\\|g')`"
  CLASSPATH_SUFFIX=";"
  PSEP=";"
fi

declare -a scala_args
addScala () {
  scala_args+=("'$1'")
}
