#!/bin/bash

NASHORN=lib/ext/nashorn.jar
JRE=@JRE@
#unused
JAVA=$JRE/bin/java
LAUNCHER_BOOTCLASSPATH='@LAUNCHER_BOOTCLASSPATH@'
LAUNCHER_FLAGS=-Xms8m
CLASSNAME=@MAIN_CLASS@
BINARY_LOCATION=@BIN_LOCATION@
SPLASH_LOCATION=@JAVAWS_SPLASH_LOCATION@
RUN_ARGS_LOCATION=@MODULARJDK_ARGS_LOCATION@
PROGRAM_NAME=@PROGRAM_NAME@
CP=$JRE/lib/rt.jar:$JRE/lib/ext/jfxrt.jar

# individual parts of bootclasspath for more easy processing
NETX_JAR=@NETX_JAR@
PLUGIN_JAR=@PLUGIN_JAR@
JSOBJECT_JAR=@JSOBJECT_JAR@
TAGSOUP_JAR=@TAGSOUP_JAR@
RHINO_JAR=@RHINO_JAR@
# windows only:
#MSLINKS_JAR=@MSLINKS_JAR@


## resolve folder of this script, following all symlinks:
## http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
SCRIPT_SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SCRIPT_SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  SCRIPT_DIR="$( cd -P "$( dirname "$SCRIPT_SOURCE" )" && pwd )"
  SCRIPT_SOURCE="$(readlink "$SCRIPT_SOURCE")"
  # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
  [[ $SCRIPT_SOURCE != /* ]] && SCRIPT_SOURCE="$SCRIPT_DIR/$SCRIPT_SOURCE"
done
readonly SCRIPT_DIR="$( cd -P "$( dirname "$SCRIPT_SOURCE" )" && pwd )"
readonly PORTABLE_ITW_HOME="`dirname $SCRIPT_DIR`"


## resolve custom JRE:
CONFIG_HOME=$XDG_CONFIG_HOME
if [ "x$CONFIG_HOME" = "x" ] ; then
  CONFIG_HOME=~/.config
fi;
PROPERTY_NAME=deployment.jre.dir
CUSTOM_JRE_REGEX="^$PROPERTY_NAME *= *"
CUSTOM_JRE=`grep "$CUSTOM_JRE_REGEX" $CONFIG_HOME/icedtea-web/deployment.properties 2>/dev/null |  sed "s/$CUSTOM_JRE_REGEX//g"`
#now check in legacy one
if [ "x$CUSTOM_JRE" = "x" ] ; then
  CUSTOM_JRE=`grep "$CUSTOM_JRE_REGEX" ~/.icedtea/deployment.properties 2>/dev/null |  sed "s/$CUSTOM_JRE_REGEX//g"`
fi;
#now check in global one
if [ "x$CUSTOM_JRE" = "x" ] ; then
  CUSTOM_JRE=`grep "$CUSTOM_JRE_REGEX" /etc/.java/.deploy/deployment.properties 2>/dev/null |  sed "s/$CUSTOM_JRE_REGEX//g"`
fi;
if [ ! "x$JAVA_HOME" = "x" ] ; then
  echo "Warning! JAVA_HOME of $JAVA_HOME in play!"
  CUSTOM_JRE=$JAVA_HOME
fi


# Support portable ITW:
# note, that this is temporary, experimental solution, moreover for 1.7 and will likely change since 1.8
# by this, sh launchers can be again a bit more portable then win ones, thats why we moved to native ones since 1.8
# still this is very dummy
if [ ! -f $NETX_JAR ] ; then
  ITW_LIBS="BUNDLED";
fi
if [ "x$ITW_LIBS" == "xBUNDLED" -o ! "x$ITW_HOME" = "x" ] ; then
  if [ "x$ITW_HOME" = "x" ] ; then
    ITW_HOME=$PORTABLE_ITW_HOME
  fi
  BINARY_LOCATION="$ITW_HOME/bin/`basename \"$BINARY_LOCATION\"`"
  SPLASH_LOCATION="$ITW_HOME/share/icedtea-web/`basename \"$SPLASH_LOCATION\"`"
  RUN_ARGS_LOCATION="$ITW_HOME/bin/`basename \"$RUN_ARGS_LOCATION\"`"
  NETX_JAR="$ITW_HOME/share/icedtea-web/`basename \"$NETX_JAR\"`"
  PLUGIN_JAR="$ITW_HOME/share/icedtea-web/`basename \"$PLUGIN_JAR\"`"
  JSOBJECT_JAR="$ITW_HOME/share/icedtea-web/`basename \"$JSOBJECT_JAR\"`"
  LAUNCHER_BOOTCLASSPATH="-Xbootclasspath/a:$NETX_JAR:$PLUGIN_JAR:$JSOBJECT_JAR:$ITW_HOME/linux-deps-runtime/`basename \"$TAGSOUP_JAR\"`:$ITW_HOME/linux-deps-runtime/`basename \"$RHINO_JAR\"`"
  echo "warning, using portable itw from $ITW_HOME: $LAUNCHER_BOOTCLASSPATH $BINARY_LOCATION $SPLASH_LOCATION"
fi


# Fix classpaths for custom JRE:
if [ "x$CUSTOM_JRE" != "x" ] ; then
  if [ -e "$CUSTOM_JRE" -a -e "$CUSTOM_JRE/bin/java" ] ; then
    JAVA=$CUSTOM_JRE/bin/java
    CP=$CUSTOM_JRE/lib/rt.jar:$CUSTOM_JRE/lib/ext/jfxrt.jar
    LAUNCHER_BOOTCLASSPATH=$LAUNCHER_BOOTCLASSPATH:$CUSTOM_JRE/$NASHORN
  else
    echo "Your custom JRE $CUSTOM_JRE read from deployment.properties under key $PROPERTY_NAME as $CUSTOM_JRE is not valid. Using default ($JRE, $CP) in attempt to start. Please fix this."
  fi
else
  LAUNCHER_BOOTCLASSPATH="$LAUNCHER_BOOTCLASSPATH:$JRE/$NASHORN"
fi;


# Support Modular JDK (jigsaw):
MODULAR_JDK="NO"
fullversion=`${JAVA} -version 2>&1`
version=`echo $fullversion | head -n 1 | cut -d'-' -f1 | cut -d'"' -f2 | cut -d'.' -f1`
if [ "$version" -eq "1" ]; then
  version=`echo $fullversion | head -n 1 | cut -d'-' -f1 | cut -d'"' -f2 | cut -d'.' -f2`
fi
if [ "$version" -ge "9" ]; then
  MODULAR_JDK="YES"
fi


# Build Java command line:
JAVA_ARGS=( )
ARGS=( )
COMMAND=()

i=0
j=0

# Filter script args:
SPLASH="false"
if [ "x$ICEDTEA_WEB_SPLASH" = "x" ] ; then
  SPLASH="true"
fi;

while [ "$#" -gt "0" ]; do
  case "$1" in
    -J*)
      JAVA_ARGS[$i]="${1##-J}"
      i=$((i+1))
      ;;
    *)
      ARGS[$j]="$1"
      j=$((j+1))
      if [[ "$1" =~ ^[-]*headless ]] ; then   
        SPLASH="false"
      fi
      ;;
  esac
  shift
done

# TODO: inline args without using COMMAND[array] to unify linux/windows scripts
k=0
COMMAND[k]="${JAVA}"
k=$((k+1))
if [ "$SPLASH" = "true" ] ; then
  COMMAND[k]="-splash:${SPLASH_LOCATION}"
  k=$((k+1))
fi;
COMMAND[k]="${LAUNCHER_BOOTCLASSPATH}"
k=$((k+1))
COMMAND[k]="${LAUNCHER_FLAGS}"
k=$((k+1))

if [ "x$MODULAR_JDK" == "xYES" ] ; then
  COMMAND[k]="--patch-module"
  k=$((k+1))
  COMMAND[k]="java.desktop=$NETX_JAR:$PLUGIN_JAR"
  k=$((k+1))
  # jsobject must be patched separately from plugin
  # otherwise netscape pkg would be shared by two modules, which is forbiden
  # plugin jar may not be built
  if [ ! "x$JSOBJECT_JAR" == "x" ] ; then
    COMMAND[k]="--patch-module"
    k=$((k+1))
    COMMAND[k]="jdk.jsobject=$JSOBJECT_JAR"
    k=$((k+1))
  fi

  # add JDK9+ arg file:
  COMMAND[k]="@$RUN_ARGS_LOCATION"
  k=$((k+1))
fi


i=0
while [ "$i" -lt "${#JAVA_ARGS[@]}" ]; do
  COMMAND[k]="${JAVA_ARGS[$i]}"
  i=$((i+1))
  k=$((k+1))
done

COMMAND[k]="-classpath"
k=$((k+1))
COMMAND[k]="${CP}"
k=$((k+1))

COMMAND[k]="-Dicedtea-web.bin.name=${PROGRAM_NAME}"
k=$((k+1))
COMMAND[k]="-Dicedtea-web.bin.location=${BINARY_LOCATION}"
k=$((k+1))

COMMAND[k]="${CLASSNAME}"
k=$((k+1))
j=0
while [ "$j" -lt "${#ARGS[@]}" ]; do
  COMMAND[k]="${ARGS[$j]}"
  j=$((j+1))
  k=$((k+1))
done

exec -a "$PROGRAM_NAME" "${COMMAND[@]}"

exit $?

