#! /bin/sh

#  Copyright 2007-2008 Konrad-Zuse-Zentrum für Informationstechnik Berlin
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

# inspired by the start script of ant

# Extract launch and scalaris arguments, (see details below).
scalaris_exec_args=
no_config=false
scalaris_exec_debug=false
show_help=false
for arg in "$@" ; do
  if [ "$arg" = "--noconfig" ] ; then
    no_config=true
  elif [ "$arg" = "--execdebug" ] ; then
    scalaris_exec_debug=true
  elif [ my"$arg" = my"-h"  -o my"$arg" = my"--help"  ] ; then
    show_help=true
    scalaris_exec_args="$scalaris_exec_args -help"
  else
    if [  my"$arg" = my"-h"  -o  my"$arg" = my"-help" ] ; then
      show_help=true
    fi
    scalaris_exec_args="$scalaris_exec_args \"$arg\""
  fi
done

# Source/default scalaris configuration
if $no_config ; then
  rpm_mode=false
else
  # load system-wide scalaris configuration
  # load system-wide scalaris configuration (ONLY if SCALARIS_JAVA_HOME has NOT been set)
  if [ -z "$SCALARIS_JAVA_HOME" -o "$SCALARIS_JAVA_HOME" = "/usr/share/java/scalaris" ]; then
      if [ -f "/etc/scalaris/scalaris-java.conf" ] ; then
          . /etc/scalaris/scalaris-java.conf
      fi
  fi

  # load user scalaris configuration
  if [ -f "$HOME/.scalaris/scalaris-java.conf" ] ; then
    . $HOME/.scalaris/scalaris-java.conf
  fi

  # provide default configuration values
  if [ -z "$rpm_mode" ] ; then
    rpm_mode=false
  fi
fi

# Setup Java environment in rpm mode
if $rpm_mode ; then
  if [ -f /usr/share/java-utils/java-functions ] ; then
    . /usr/share/java-utils/java-functions
    set_jvm
    set_javacmd
  fi
fi

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  Darwin*) darwin=true
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
           fi
           ;;
esac

if [ -z "$SCALARIS_JAVA_HOME" -o ! -d "$SCALARIS_JAVA_HOME" ] ; then
  ## resolve links - $0 may be a link to scalaris's home
  PRG="$0"
  progname=`basename "$0"`

  # need this for relative symlinks
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
    else
    PRG=`dirname "$PRG"`"/$link"
    fi
  done

  SCALARIS_JAVA_HOME=`dirname "$PRG"`

  # make it fully qualified
  SCALARIS_JAVA_HOME=`cd "$SCALARIS_JAVA_HOME" && pwd`
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$SCALARIS_JAVA_HOME" ] &&
    SCALARIS_JAVA_HOME=`cygpath --unix "$SCALARIS_JAVA_HOME"`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

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

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

# Build local classpath manually in non-rpm mode or
# use the Jpackage helper in rpm mode with basic and default jars
# specified in the scalaris-java.conf configuration. Because the launcher
# is used, libraries linked in SCALARIS_JAVA_HOME/lib will also be included, but this
# is discouraged as it is not java-version safe. A user should
# request optional jars and their dependencies via the OPT_JAR_LIST
# variable
if $rpm_mode && [ -x /usr/bin/build-classpath ] ; then
  LOCALCLASSPATH="$(/usr/bin/build-classpath scalaris jakarta-commons-cli)"

  # If the user requested to try to add some other jars to the classpath
  if [ -n "$OPT_JAR_LIST" ] ; then
    _OPTCLASSPATH="$(/usr/bin/build-classpath $OPT_JAR_LIST 2> /dev/null)"
    if [ -n "$_OPTCLASSPATH" ] ; then 
      LOCALCLASSPATH="$LOCALCLASSPATH:$_OPTCLASSPATH"
    fi
  fi

  # Explicitly add javac path to classpath, assume JAVA_HOME set
  # properly in rpm mode
  if [ -f "$JAVA_HOME/lib/tools.jar" ] ; then
    LOCALCLASSPATH="$LOCALCLASSPATH:$JAVA_HOME/lib/tools.jar"
  fi
  if [ -f "$JAVA_HOME/lib/classes.zip" ] ; then
    LOCALCLASSPATH="$LOCALCLASSPATH:$JAVA_HOME/lib/classes.zip"
  fi

  # if CLASSPATH_OVERRIDE env var is set, LOCALCLASSPATH will be
  # user CLASSPATH first and scalaris-found jars after.
  # In that case, the user CLASSPATH will override scalaris-found jars
  #
  # if CLASSPATH_OVERRIDE is not set, we'll have the normal behaviour
  # with scalaris-found jars first and user CLASSPATH after
  if [ -n "$CLASSPATH" ] ; then
    # merge local and specified classpath 
    if [ -z "$LOCALCLASSPATH" ] ; then 
      LOCALCLASSPATH="$CLASSPATH"
    elif [ -n "$CLASSPATH_OVERRIDE" ] ; then
      LOCALCLASSPATH="$CLASSPATH:$LOCALCLASSPATH"
    else
      LOCALCLASSPATH="$LOCALCLASSPATH:$CLASSPATH"
    fi

    # remove class path from launcher -cp option
    CLASSPATH=""
  fi
else
  # not using rpm_mode
  if [ -z "$LOCALCLASSPATH" ] ; then
      LOCALCLASSPATH=$SCALARIS_JAVA_HOME/scalaris.jar:$SCALARIS_JAVA_HOME/lib/jakarta-commons-cli-1.1.jar:$SCALARIS_JAVA_HOME/lib/OtpErlang-1.4.2.jar
  else
      LOCALCLASSPATH=$SCALARIS_JAVA_HOME/scalaris.jar:$SCALARIS_JAVA_HOME/lib/jakarta-commons-cli-1.1.jar:$SCALARIS_JAVA_HOME/lib/OtpErlang-1.4.2.jar:$LOCALCLASSPATH
  fi
fi

# For Cygwin, switch paths to appropriate format before running java
# For PATHs convert to unix format first, then to windows format to ensure
# both formats are supported. Probably this will fail on directories with ;
# in the name in the path. Let's assume that paths containing ; are more
# rare than windows style paths on cygwin.
if $cygwin; then
  if [ "$OS" = "Windows_NT" ] && cygpath -m .>/dev/null 2>/dev/null ; then
    format=mixed
  else
    format=windows
  fi
  SCALARIS_JAVA_HOME=`cygpath --$format "$SCALARIS_JAVA_HOME"`
  JAVA_HOME=`cygpath --$format "$JAVA_HOME"`
  LCP_TEMP=`cygpath --path --unix "$LOCALCLASSPATH"`
  LOCALCLASSPATH=`cygpath --path --$format "$LCP_TEMP"`
  if [ -n "$CLASSPATH" ] ; then
    CP_TEMP=`cygpath --path --unix "$CLASSPATH"`
    CLASSPATH=`cygpath --path --$format "$CP_TEMP"`
  fi
  CYGHOME=`cygpath --$format "$HOME"`
fi

# Show script help if requested
if $show_help ; then
  echo $0 '[script options] [options]'
  echo 'Script Options:'
  echo '  --help, --h            print this message and scalaris help'
  echo '  --noconfig             suppress sourcing of /etc/scalaris/scalaris-java.conf'
  echo '                         and $HOME/.scalaris/scalaris-java.conf config files'
  echo '  --execdebug            print scalaris exec line generated by this'
  echo '                         launch script'
  echo '  '
fi
# add a second backslash to variables terminated by a backslash under cygwin
if $cygwin; then
  case "$SCALARIS_JAVA_HOME" in
    *\\ )
    SCALARIS_JAVA_HOME="$SCALARIS_JAVA_HOME\\"
    ;;
  esac
  case "$CYGHOME" in
    *\\ )
    CYGHOME="$CYGHOME\\"
    ;;
  esac
  case "$LOCALCLASSPATH" in
    *\\ )
    LOCALCLASSPATH="$LOCALCLASSPATH\\"
    ;;
  esac
  case "$CLASSPATH" in
    *\\ )
    CLASSPATH="$CLASSPATH\\"
    ;;
  esac
fi

scalaris_java_config=
if [ -n "$SCALARIS_JAVA_CONFIG" ]; then
  scalaris_java_config="-Dscalaris.java.config=\"$SCALARIS_JAVA_CONFIG\""
fi

# Execute scalaris using eval/exec to preserve spaces in paths,
# java options, and scalaris args
scalaris_exec_command="exec \"$JAVACMD\" $SCALARIS_OPTS -classpath \"$LOCALCLASSPATH\" -Dscalaris.java.home=\"$SCALARIS_JAVA_HOME\" $scalaris_java_config de.zib.scalaris.Main $SCALARIS_ARGS $scalaris_exec_args"
if $scalaris_exec_debug ; then
    echo $scalaris_exec_command
fi
eval $scalaris_exec_command
