#!/bin/sh
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# * This source file is part of SableVM.                            *
# *                                                                 *
# * See the file "LICENSE" for the copyright information and for    *
# * the terms and conditions for copying, distribution and          *
# * modification of this source file.                               *
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
echo=echo
exec=echo

echo=`which true`
exec=exec

JVM=/usr/bin/sablevm
REST=
OPTS=

if [ $# -eq 0 ]; then
  echo "You need to specify some parameters for java-sablevm wrapper."
  echo "Help option is not implemented yet. See 'man java-sablevm' for now."
  exit 0
fi

if [ "$1" != "" ]; then OPTSDONE=0; else OPTSDONE=1; fi
if [ "$CLASSPATH" = "" ]; then CLASSPATH=; fi
$echo "This is 'java' wrapper for sablevm"

# Timezone detection should be handled in Classpath, but it's broken, so
# we work it around here.
if [ -r /etc/timezone ]; then
  ZONE_NAME=`cat /etc/timezone | sed 's/[ \t]*#/#/g' | grep -v '^#' | \
                grep -v '^[ \t]*$' | head -n 1 | awk '{ print $1 }'`
  if [ "X$ZONE_NAME" != "X" ]; then
    OPTS="-Duser.timezone=${ZONE_NAME}"
  fi
fi

# this may deserve some better handling - what if someone specifies another
# java.library.path in the cmdline w/o /usr/lib ?
if [ "X$LD_LIBRARY_PATH" != "X" ]; then
  OPTS="-p java.library.path=/usr/lib:$LD_LIBRARY_PATH"
else
  OPTS="-p java.library.path=/usr/lib"
fi

while [ $OPTSDONE -eq 0 ]
 do
  case "$1" in
    -help|--help|-h|-\?|-X)
      $echo "help!"
      echo "Help option is not implemented yet. See 'man java-sablevm' for now."
      exit 0
    ;;
    -debug|-Xdebug|--debug)
      $echo "debug option"
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
    ;;
    -version|--version|-V)
      $echo "version"
      exec $JVM -V
    ;;
    -ss|-ms|-mx)
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
      shift
    ;;
    -X*)
      $echo "catched some eXtra option: '$1'"
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
    ;;
    -client|-server|-noclassgc|-noverify|--noverify)
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2 ;;
    -da|-disableassertions|-dsa|-disablesystemassertions)
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2 ;;
    -ea|-enableassertions|-esa|-enablesystemassertions)
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
      echo "You can compile SableVM with --enable-debugging-features option instead." >&2
    ;;
    -classpath|--classpath|-cp|--cp)
      $echo "SET classpath $2"
      CLASSPATH="$2"
      shift
    ;;
    -classpath=*)
      rest=${1#-classpath}
      value=${rest#*=}
      $echo "SET classpath $value"
      CLASSPATH="$value"
    ;;
    --classpath=*)
      rest=${1#--classpath}
      value=${rest#*=}
      $echo "SET classpath $value"
      CLASSPATH="$value"
    ;;
    -addclasspath|--addclasspath)
      $echo "ADD classpath $2"
      CLASSPATH="$CLASSPATH:$2"
      shift
    ;;
    -D*)
      # FIXME: we don't handle property names and values containing spaces and special chars
      rest=${1#-D}
      $echo "defined property $1"
      value=${rest#*=}
      key=${rest%%=$value}
      $echo "rest='$rest' key='$key' value='$value'"
      OPTS="$OPTS --property=${key}=${value}"
    ;;
    -jar|--jar)
      $echo "JAR file $2"
      CLASSPATH="$2:$CLASSPATH"
      CLASS=`unzip -p $2 META-INF/MANIFEST.MF|grep -i "Main-Class: "|tr -d \\\\r | awk '{ print $2 }'`
      RES=$?
      if [ $RES -ne 0 ]; then
        echo "Error: Unable to uncompress $2"; exit 1;
      fi
      if [ "$CLASS" = "" ]; then
        echo "Error: No MANIFEST.MF or Main-Class: in MANIFEST.MF found in $2"; exit 1;
      fi
      $echo class: $CLASS
      OPTS="$OPTS $CLASS"
      # -jar is always the last option passed to java - the rest is being
      # passed to executed program (main method)
      OPTSDONE=1
      shift
      shift
    ;;
    -v|-verbose|--verbose)
      $echo "verbose output"
      OPTS="$OPTS -v"
    ;;
    -verbose-gc|--verbose-gc)
      $echo "verbose garbage collection"
      OPTS="$OPTS -g"
    ;;
    -verbose*jni)
      $echo "verbose JNI"
      OPTS="$OPTS -j"
    ;;
    -verbosemem|-verbosejit|-verbose*)
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2 ;;
    -showversion|--showversion)
      $echo "just catched '$1' request"
      # not exec, just show version and continue!
      $JVM -V
    ;;
    -vmdebug)
      shift # a string of comma-separated flags follows -vmdebug
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
      echo "You can compile SableVM with --enable-debugging-features option instead." >&2
    ;;
    -*)
      echo "Warning: $1 option not RECOGNIZED by java-sablevm wrapper." >&2
      echo "A not recognized option will be just passed to SableVM." >&2
      echo "Note that we don't know if we should expect an argument here!" >&2
      echo "It almost _surely_ will result in an errors when the param is followed" >&2
      echo "by an argument. Refer to 'man java-sablevm' and 'man sablevm'." >&2
      REST="$REST $1"
    ;;
    --)
      OPTSDONE=1
    ;;
    *)
      OPTSDONE=1
    ;;
  esac
  if [ $OPTSDONE != 1 ]; then shift; fi
done

$echo CP: 7"$CLASSPATH"7
$echo OP: 7"$OPTS"7
$echo RE: 7"$REST"7
$echo @@: 7"$@"7

if [ "$CLASSPATH" != "" ]; then
    CLASSPATH="/usr/share/java/gnujaxp.jar:$CLASSPATH";
else
    CLASSPATH="/usr/share/java/gnujaxp.jar:.";
fi

CPPARAM="--classpath=$CLASSPATH";
export CLASSPATH
# this is so that jikes worked w/o problems, i.e. when called by Ant
# should be substituted by @datadir@/sablevm/sablevm-classpath in Makefile
BOOTCLASSPATH=/usr/share/sablevm/sablevm-classpath
export BOOTCLASSPATH

TST="$CLASSPATH_PARAM$OPTS$REST$*"
$echo TST: $TST

if [ "$TST" != "" ]; then
    $echo $JVM -Y $CPPARAM $OPTS $REST "$@"
    $exec $JVM -Y $CPPARAM $OPTS $REST "$@"
else
  echo "You need to specify some parameters for java-sablevm wrapper."
  echo "Help option is not implemented yet. See 'man java-sablevm' for now."
fi
        
exit 0
