#!/bin/sh 
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# * This file is part of SableVM.                             *
# * See the file "LICENSE" for Copyright information and the  *
# * terms and conditions for copying, distribution and        *
# * modification of SableVM.                                  *
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

echo=echo
exec=echo

echo=/bin/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"

# 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
# options found in kaffe's java command manual
    -help|--help|-h|-\?)
      $echo "help!"
      echo "Help option is not implemented yet. See 'man java-sablevm' for now."
      exit 0
    ;;
    -debug|--debug|-Xdebug|--Xdebug)
      $echo "debug option"
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
    ;;
    -version|--version|-V)
      $echo "version"
      exec $JVM -V
    ;;
    -ss|--ss)
      $echo "max stack size: $2"
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
      shift
    ;;
    -ms|--ms)
      $echo "initial heap size: $2"
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
      shift
    ;;
    -mx|--mx)
      $echo "maximum heap size: $2"
      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
    ;;
    -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
    ;;
    -noverify|--noverify)
      $echo "no verification"
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
    ;;
    -D*)
      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 "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 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|--v|-verbose|--verbose)
      $echo "verbose output"
      OPTS="$OPTS -v"
    ;;
    -verbose-gc|--verbose-gc)
      $echo "verbose garbage collection"
      OPTS="$OPTS -g"
    ;;
    -verbosemem|--verbosemem)
      $echo "verbose memory allocation statistics"
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
    ;;
    -verbosejit|--verbosejit)
      $echo "verbose JIT"
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&2
    ;;
    -verbose*|--verbose*)
      $echo "just catched '$1' request"
      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|--vmdebug)
      $echo "VM debugging enabled"
      echo "Warning: $1 option not implemented in java-sablevm wrapper." >&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
      EST="$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="--classpath=$CLASSPATH";
fi

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

if [ "$TST" != "" ]; then
    $echo $JVM -Y $CLASSPATH $OPTS $REST $@
    $exec $JVM -Y $CLASSPATH $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
