#!/bin/sh

###############################################################################
## Part 1: - Ensure a proper installation of KeY.
##         - Checks for `key.ui` script generated by gradle installDist
##         - Runs gradle on demand

#
# $KEY_HOME
#
if [ -z "$KEY_HOME" ]
then
    KEY_HOME=$(readlink -f "$0")        # resolve symlink name
    KEY_HOME=$(dirname "$KEY_HOME")     # get directory with script file
    KEY_HOME=$(dirname "$KEY_HOME")     # get directory above
fi
echo "Using KeY installation from: $KEY_HOME"


START_SCRIPT=$KEY_HOME/key.ui/build/install/key.ui/bin/key.ui

if [ ! -f $START_SCRIPT ]; then
    echo "The script '$START_SCRIPT' does not exists."
    echo "Please run './gradlew :key.ui:installDist' in directory '$KEY_HOME' to create it."
    exit 1
fi

###############################################################################
## Part 2: Parses KeY-specific parameters
##
##

# We use `JAVA_OPTS' to set JVM options to the generated `key.ui` script

# Initialise the options with default options.
# OLD   # For debugging reasons disable JIT (otherwise strace doesn't work)
# OLD   JAVA_OPTS="-XX:+UseG1GC"
JAVA_OPTS=

add_jvm_option() {
   JAVA_OPTS="${JAVA_OPTS} $@"
}

list_help() {
    echo "Runscript for the KeY system\n"
    echo "Usage: ./key [K/J-options] [options] [filename]"
    echo "\nOptions for using this script"
    echo "Options starting with --K- or --J- must come first."
    echo "They are technical options which will be passed to the java process."
    echo "  --J-option: pass option to the JVM following the -J option convention of javac"
    echo "  --K-keydebug: turn debugging on"
    echo "  --K-debugclassloader: switch the Debugclassloadflag on to load classes for debugging"
    echo "  --K-debugprefix <debugclass>: send debug output of debugclass to stadard out"
    echo "  --K-remotedebug [wait] <port>: turn remote debugging on;"
    echo "                parameter for listening port, wait for connection if 'wait' specified"
    echo "  --K-assertionflag: switch assertionflag on, s.t. debug assertions are used\n"
    echo "Other:\n"
    echo "  --K-server:  switches Java -server on "
    echo "  --K-help, -Kh	: prints this help message"
    echo "  --help	: prints help message of the KeY prover"
}


add_jvm_option "-Dsun.awt.exception.handler=de.uka.ilkd.key.gui.ExceptionalHandler"
add_jvm_option "-Dkey.examples.dir=$KEY_HOME/key.ui/examples"

while [ $# -ne 0 ]; do
    case $1 in
        #     -K options for configuration options of KeY, such as debugmode, flags,
        # remote debugging
	--Kh)
	    list_help
	    exit;;

	--K-help)
	    list_help
	    exit;;

	--K-keydebug)
	    add_jvm_option -DKeyDebugFlag=on
	    shift
	    continue;;

	--K-debugclassloader)
	    add_jvm_option -DKeyDebugClassLoader=on
	    shift
	    continue;;

	--K-debugprefix)
            add_jvm_option -Dkey.debug.prefix=$2
	    shift
	    shift
	    continue;;

	--K-assertionflag)
            add_jvm_option -DKeyAssertionFlag=true
	    shift
	    continue;;

	--K-remotedebug)
            if [ "$2" = "wait" ]
            then
                wait="y"
                shift
            else
                wait="n"
            fi
            add_jvm_option -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=${wait},address=$2
	    shift
	    shift
	    continue;;

	--K-server)
            add_jvm_option -server
	    shift
	    continue;;

        # -J options following conventions of Java for -J options
        # special treatment of the options which are also set by default
        # values: xms and Xmx
	--J-Xms*)
	    option=${1#--J}
	    add_jvm_option -Xms=${option}
	    shift
	    continue;;

	--J-Xmx*)
	    option=\${1#--J}
            add_jvm_option -Xmx=${option}
	    shift
	    continue;;

	--J*)
	    option=${1#--J}
	    add_jvm_option ${option}
	    shift
	    continue;;

        # key options beginning with a minus or filenames
        # set headless mode for all of them
	--auto)
            add_jvm_option -Djava.awt.headless=true
	    break;;
	--auto-loadonly)
            add_jvm_option -Djava.awt.headless=true
	    break;;
	--help)
            add_jvm_option -Djava.awt.headless=true
	    break;;
	--show-properties)
            add_jvm_option -Djava.awt.headless=true
	    break;;
        *)
            break;;
    esac
done

export JAVA_OPTS
$START_SCRIPT "$@"
