#!/bin/sh

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

usage() {
  polyglot -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
}

fixclasspath() {
  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
}


extra_cp=
args=
vmargs=
classpath=
java=java

dir=`unixfilename "$dir"`

while true; do
    case "$1" in
        "")
            break
            ;;
        -V)
            verbose=1
            shift
            ;;
        -classpath)
            shift
            extra_cp="$extra_cp:$1"
            shift
            ;;
        -ext)
            shift
            ext="$1"
            shift
            ;;
        -j)
            shift
            vmargs="$vmargs '$1'"
            shift
            ;;
        -J)
            shift
            java="'$1'"
            shift
            ;;
        -h)
            usage=1
            break
            ;;
        *)
            args="$args '$1'"
            shift
            ;;
    esac
done

if [ -n "$ext" ]; then
  args="-ext '$ext' $args"
fi


classpath="$dir/classes:$dir/lib/polyglot.jar:$dir/lib/java_cup.jar"
if [ -n "$ext" ]; then
  classpath="$classpath:$dir/lib/$ext.jar"
fi
classpath="$classpath:$extra_cp"
classpath=`fixclasspath "$classpath"`

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

emmajar=$dir/lib/emma.jar
emmajar=`fixclasspath "$emmajar"`
sourcepath=$dir/src
sourcepath=`fixclasspath "$sourcepath"`

if [ "$verbose" = 1 ]; then
  echo "$java" "$vmargs" -classpath "'$classpath'" -jar "'$emmajar'" -r html -raw -merge yes -sp "'$sourcepath'" -cp "'$classpath'" polyglot.main.Main -classpath "'$classpath'" "$@"
fi

  eval "$java" "$vmargs" -classpath "'$classpath'" -jar "'$emmajar'" -r html -raw -merge yes -sp "'$sourcepath'" -cp "'$classpath'" polyglot.main.Main -classpath "'$classpath'" "$@"

