#!/bin/sh

resolve_symlink  ()
{ 
   TARGET=`ls -l "$1"| awk '/\ ->\ /{print $NF}'`

   if [ -n "$TARGET" ] ; then
      RESULT="$TARGET"
      case "$RESULT" in
         /*) break ;;				# absolute symlink
         *)  RESULT=`dirname "$0"`/"$RESULT" ;;	# relative symlink
      esac
   else
      RESULT=$1
   fi

   echo "$RESULT"
}

UNAME=`uname -s 2>/dev/null | tr '[:upper:]' '[:lower:]'`
ARCH=`uname -pm 2>/dev/null | tr '[:upper:]' '[:lower:]' | tr ' ' '-'`

if [ -z "$JAVA_HOME" ] ; then
    JAVA=`which java`
    if [ -z "$JAVA" ] ; then
	echo "Cannot find JAVA. Please set your PATH or \$JAVA_HOME."
	exit 1
    fi
else
    if [ "$UNAME" = "darwin" ] ; then
	JRE=$JAVA_HOME/Home
	JAVA=$JRE/bin/java
# other OS
    else
	JRE=$JAVA_HOME/jre
	JAVA=$JRE/bin/java
    fi
fi
echo "Using JDK installation from:      $JAVA_HOME"


# For debugging reasons disable JIT (otherwise strace doesn't work)
JAVA_OPTIONS=""
#JAVA_OPTIONS=-classic

# Uncomment the following definition for remote debugging on port 1234
#JAVA_OPTIONS='-DKeyDebugFlag=on -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1234'

#

if [ -z "$KEY_HOME" ] ; then
   KEY_HOME=`resolve_symlink "$0"`	# resolve symlink name
   KEY_HOME=`dirname "$KEY_HOME"`		# strip executable filename
   KEY_HOME=`cd "$KEY_HOME";pwd`	# and now expand the path to full
   KEY_HOME=`dirname "$KEY_HOME"`		# strip bin/ sirectory
fi
echo "Using KeY installation from:      $KEY_HOME"

#
# KeY CLASSPATH
#

jar_files="key.core/lib/antlr.jar
key.core/lib/recoderKey.jar
key.util.test/lib/hamcrest-core-1.3.jar
key.util.test/lib/junit-4.12.jar"
keyclasspath=""

for i in $jar_files ; do
    current_jar="$KEY_HOME/$i"
    if [ ! -f "$current_jar" ]
    then
       echo Cannot find $current_jar. 
       echo Copy or link the file into the
       echo $KEY_LIB directory.
       exit 1
    else
       keyclasspath="${keyclasspath}:$current_jar"
    fi
done

included_projects="key.util
key.util.test
key.core
key.core.test
key.core.testgen
key.core.testgen.test
key.core.proof_references
key.core.proof_references.test
key.removegenerics
key.removegenerics.test
key.core.proof_references
key.core.proof_references.test
key.core.symbolic_execution
key.core.symbolic_execution.test"

for i in $included_projects ; do
    current_project="$KEY_HOME/$i"
    if [ ! -d "$current_project/bin" ]
    then
       echo Directory $current_project/bin not found.
       echo Building project $current_project
       cd "$current_project"
       ant
       if [ $? -ne 0 ]; then
           exit # in case build fails
       fi
       cd -
    fi
    keyclasspath="${keyclasspath}:$current_project/bin"
done

#
#Function prints help message
#

list_help(){
  echo "./runTest [options] [filename]"
  echo "\nOptions for using this script"
  echo "  J-option: pass option to the JVM following the -J option convention of javac\n"
  echo "Debugging:\n"
  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 <port>: turn remote debugging on; parameter for listening port"

  echo "Other:\n"

  echo "  --Khelp, -Kh	: prints this help message"
  echo "  --help	: prints help message of the KeY prover"

}

#
# KeY-specific command line options
#
keysysprops=""
keyoptions=""
while [ $# -ne 0 ]; do
    case $1 in
#     -K options for configuration options of KeY, such as debugmode, flags,
# remote debugging
	--Kh)
# 	    set ${1} #removes the minus from the option, s.t. it can be passed to key as option
	    list_help
	    exit;;
	--Khelp)
# 	    set ${1#--} #removes the minus from the option, s.t. it can be passed to key as option
	    list_help
	    exit;;
        --K-keydebug) keysysprops="${keysysprops}-DKeyDebugFlag=on "
            shift
            continue;;
        --K-debugclassloader)
keysysprops="${keysysprops}-DKeyDebugClassLoader=on
"
            shift
            continue;;

	--K-debugprefix) keysysprops="${keysysprops}-Dkey.debug.prefix=${2}"
	    shift
	    shift
	    continue;;


	--K-remotedebug) JAVA_OPTIONS="-Xdebug -Xnoagent -Djava.compiler=NONE
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${2}"
	    shift
	    shift
	    continue;;


# -J options follwing conventions of Java for -J options
	-J*)
	    option=${1#-J}
	    JAVA_OPTIONS="${JAVA_OPTIONS}${option} "
	    shift
	    continue;;
	  
	  *) break;;

    esac
done

PATH=${PATH}:$KEY_LIB

if [ "" = "$1" ] 
then
    echo "No particular test case specified. All tests will be run."
    testcase=de.uka.ilkd.key.suite.TestKey
else
    testcase=`echo $1 | tr '/' '.'`
fi

$JAVA $JAVA_OPTIONS $keysysprops \
-ea:de.uka.ilkd.key... -noverify -Djvm.dir=$JVM_DIR -Xms64m -Xmx2048m \
-Dkey.home="$KEY_HOME" \
-Djava.awt.headless=true \
-classpath "$keyclasspath" junit.textui.TestRunner $testcase 

