#!/bin/sh

prg=`basename "$0"`
dir=`dirname "$0"`/..

usage() {
  jifc -h
  cat <<EOF
  -j <jvm-options>         pass options to the Java VM
  -J <jvm>                 use a different Java VM (default java in path)
  -V                       echo the java command
EOF
}

fixpath() {
  windows=0

  if [ `uname | grep -c CYGWIN` -ne 0 ]; then
    windows=1
  fi

  cp="$1"

  if [ "$windows" = 1 ]; then 
    cygpath -pw "$cp"
  else
    echo "$cp"
  fi
}

unixfilename() {
  windows=0

  if [ `uname | grep -c CYGWIN` -ne 0 ]; then
    windows=1
  fi

  cp="$1"

  if [ "$windows" = 1 ]; then 
    cygpath -u "$cp"
  else
    echo "$cp"
  fi
}

unixpath() {
  windows=0

  if [ `uname | grep -c CYGWIN` -ne 0 ]; then
    windows=1
  fi

  cp="$1"

  if [ "$windows" = 1 ]; then 
    cygpath -pu "$cp"
  else
    echo "$cp"
  fi
}

runtime_lib_path=
sig_classpath=
lib_classpath=
extra_cp=
args=
vmargs=
classpath=
java=java

jifc() {
  eval "$java" "$vmargs" -Djava.library.path="'$runtime_lib_path'" -classpath "'$classpath'" polyglot.main.Main "$@"
}
jifcprint() {
  echo "$java" "$vmargs" -Djava.library.path="'$runtime_lib_path'" -classpath "'$classpath'" polyglot.main.Main "$@"
}

dir=`unixfilename "$dir"`

while true; do
    case "$1" in
        "")
            break
            ;;
        -V)
            verbose=1
            shift
            ;;
        -classpath)
            shift
            if [ -z "$extra_cp" ]; then
	            extra_cp="`unixpath "$1"`"
            else
	            extra_cp="$extra_cp:`unixpath "$1"`"
            fi
            shift
            ;;
        -j)
            shift
            vmargs="$vmargs '$1'"
            shift
            ;;
        -J)
            shift
            java="'$1'"
            shift
            ;;
        -h)
            usage=1
            break
            ;;
        *)
            args="$args '$1'"
            shift
            ;;
    esac
done

unixclasspath=`unixpath $CLASSPATH`
classpath="$extra_cp"
classpath="$classpath:$dir/classes:$dir/lib/polyglot.jar:$dir/lib/java_cup.jar:$dir/lib/JFlex.jar"
classpath="$classpath:$dir/classes:$dir/lib/jif.jar"

PATH_unix=`unixpath $PATH`
runtime_lib_path="$dir/lib:$PATH_unix"
sig_classpath="$dir/sig-classes:$dir/lib/jifsig.jar"
lib_classpath="$dir/lib-classes:$dir/lib/jiflib.jar:$dir/rt-classes:$dir/lib/jifrt.jar"
classpath="$classpath:$lib_classpath"

if [ -n "$POLYGLOT" ]; then 
    polyglot_unix=`unixpath "$POLYGLOT"` 
    classpath="$classpath:$polyglot_unix/classes:$polyglot_unix/lib/polyglot.jar"
    classpath="$classpath:$polyglot_unix/lib/java_cup.jar:$polyglot_unix/lib/JFlex.jar"
fi

classpath="$classpath:$unixclasspath"

runtime_lib_path=`fixpath "$runtime_lib_path"`
sig_classpath=`fixpath "$sig_classpath"`
classpath=`fixpath "$classpath"`


if [ "$usage" = 1 ]; then
  usage
  exit 0
fi

args="-sigcp '$sig_classpath' $args"
args="-extclass jif.ExtensionInfo $args"

if [ "$verbose" = 1 ]; then
    jifcprint "$args"
fi

jifc "$args"
