#!/bin/sh

# ------------------------------------------------------------------------------
# Copyright (C) 2009 STMicroelectronics
#
# This file is part of "Mind Compiler" is free software: you can redistribute 
# it and/or modify it under the terms of the GNU Lesser General Public License 
# as published by the Free Software Foundation, either version 3 of the 
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT 
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: mind@ow2.org
#
# Authors: Matthieu Leclercq
# Contributors: 
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Mind Compiler batch script 2.0
#
# Required ENV vars:
# ------------------
#   JAVA_HOME - location of a JDK home dir
#
# Optional ENV vars
# -----------------
#   MIND_HOME - location of mind's installed home dir
#   MIND_OPTS - parameters passed to the Java VM running the mind compiler
#     e.g. to specify logging levels, use
#       set MIND_OPTS=-Ddefault.console.level=FINE -Ddefault.file.level=FINER
#   See documentation for more detail on logging system.


# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
mingw=false
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  MINGW*) mingw=true;;
  Darwin*) darwin=true 
           if [ -z "$JAVA_VERSION" ] ; then
             JAVA_VERSION="CurrentJDK"
           else
             echo "Using Java version: $JAVA_VERSION"
           fi
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
           fi
           ;;
esac
if [ -z "$JAVA_HOME" ] ; then
  if [ -r /etc/gentoo-release ] ; then
    JAVA_HOME=`java-config --jre-home`
  fi
fi
if [ -z "$MIND_HOME" ] ; then
  ## resolve links - $0 may be a link to mind's home
  PRG="$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

  saveddir=`pwd`

  MIND_HOME=`dirname "$PRG"`/..

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

  cd "$saveddir"
  # echo Using mindc at $MIND_HOME
fi

MIND_RUNTIME="$MIND_HOME"/runtime
MIND_LIB="$MIND_HOME"/lib
MIND_EXT="$MIND_HOME"/ext

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

# For Migwn, ensure paths are in UNIX format before anything is touched
if $mingw ; then
  [ -n "$MIND_HOME" ] &&
    MIND_HOME="`(cd "$MIND_HOME"; pwd)`"
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
  # TODO classpath?
fi


## Launcher class name
LAUNCHER=org.ow2.mind.Launcher

## Preparation of the java command to be executed
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"
    elif [ -f "$JAVA_HOME"  -a -x "$JAVA_HOME" ] ; then
      # In some cases (such as on TRAVIS CI system) $JAVA_HOME directly point the java executable.
      JAVACMD="$JAVA_HOME"
    else 
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD="`which java`"
  fi
fi

## Print error if the java command is not ready
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

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  [ -n "$MIND_HOME" ] &&
    MIND_HOME=`cygpath --path --windows "$MIND_HOME"`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
  [ -n "$HOME" ] &&
    HOME=`cygpath --path --windows "$HOME"`
  [ -n "$MIND_LIB" ] &&
    MIND_LIB=`cygpath --path --windows "$MIND_LIB"`
  [ -n "$MIND_EXT" ] &&
    MIND_EXT=`cygpath --path --windows "$MIND_EXT"`
  [ -n "$MIND_RUNTIME" ] &&
    MIND_RUNTIME=`cygpath --path --windows "$MIND_RUNTIME"`
fi




for i in "${MIND_LIB}"/*.jar "${MIND_EXT}"/*.jar; do
  if [ -z "$MIND_CLASSPATH" ] ; then
    if $cygwin; then
      lib=`cygpath --path --windows "${i}"`
      MIND_CLASSPATH="${lib}"
    else
      MIND_CLASSPATH="${i}"
    fi
  else
    if $cygwin; then
      lib=`cygpath --path --windows "${i}"`
      MIND_CLASSPATH="${MIND_CLASSPATH};${lib}"
    else
      MIND_CLASSPATH="${MIND_CLASSPATH}:${i}"
    fi
  fi
done


##########################################
#echo MIND_LIB=$MIND_LIB
#echo MIND_RUNTIME=$MIND_RUNTIME
#echo MIND_CLASSPATH=$MIND_CLASSPATH
###########################################

exec "$JAVACMD" \
  -classpath "${MIND_CLASSPATH}" \
  ${MIND_OPTS} \
  -Dmindc.launcher.name=mindc \
  ${LAUNCHER} \
  -src-path="${MIND_RUNTIME}" \
  "$@"


