#!/bin/bash

#credit to https://github.com/mkropat/sh-realpath
realpath() {
    canonicalize_path "$(resolve_symlinks "$1")"
}

resolve_symlinks() {
    _resolve_symlinks "$1"
}

_resolve_symlinks() {
    _assert_no_path_cycles "$@" || return

    local dir_context path
    path=$(readlink -- "$1")
    if [ $? -eq 0 ]; then
        dir_context=$(dirname -- "$1")
        _resolve_symlinks "$(_prepend_dir_context_if_necessary "$dir_context" "$path")" "$@"
    else
        printf '%s\n' "$1"
    fi
}

_prepend_dir_context_if_necessary() {
    if [ "$1" = . ]; then
        printf '%s\n' "$2"
    else
        _prepend_path_if_relative "$1" "$2"
    fi
}

_prepend_path_if_relative() {
    case "$2" in
        /* ) printf '%s\n' "$2" ;;
         * ) printf '%s\n' "$1/$2" ;;
    esac
}

_assert_no_path_cycles() {
    local target path

    target=$1
    shift

    for path in "$@"; do
        if [ "$path" = "$target" ]; then
            return 1
        fi
    done
}

canonicalize_path() {
    if [ -d "$1" ]; then
        _canonicalize_dir_path "$1"
    else
        _canonicalize_file_path "$1"
    fi
}

_canonicalize_dir_path() {
    (cd "$1" 2>/dev/null && pwd -P)
}

_canonicalize_file_path() {
    local dir file
    dir=$(dirname -- "$1")
    file=$(basename -- "$1")
    (cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file")
}

this="$(realpath "$0")"
dir="$(dirname "$this")"

usage() {
  compiler "-help"
  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
  -rdebug                  enable remote debugging
EOF
}

compiler() {
  eval "$java" "$vmargs" -classpath "'$classpath'" edu.cornell.cs.cs4120.xth.Main "$@"
}
compilerprint() {
  echo "$java" "$vmargs" -classpath "'$classpath'" edu.cornell.cs.cs4120.xth.Main "$@"
}

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="-XX:-OmitStackTraceInFastThrow"
classpath=
java=java


while true; do
    case "$1" in
        "")
            break
            ;;
        -V)
            verbose=1
            shift
            ;;
        -classpath|-cp)
            shift
            extra_cp="$extra_cp:$1"
            shift
            ;;
        -ext)
            shift
            ext="$1"
            shift
            ;;
        -j)
            shift
            vmargs="$vmargs '$1'"
            shift
            ;;
        -J)
            shift
            java="'$1'"
            shift
            ;;
	-rdebug)
	    shift
	    vmargs="${vmargs} -Xdebug -Xrunjdwp:transport=dt_socket,address=6666,server=y,suspend=y"
	    ;;
	    
        -h)
            usage=1
            break
            ;;
        *)
            args="$args '$1'"
            shift
            ;;
    esac
done

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


classpath="$dir/classes:$dir/lib/xth.jar:$dir/lib/java_cup.jar:$dir/lib/itextpdf-5.5.8.jar"
classpath="$classpath:$extra_cp"
classpath=`fixclasspath "$classpath"`


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


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

compiler "$args"

