#!/bin/sh

#
# This file simply redirects all passed arguments
# to org.checkerframework.framework.util.CheckerDevelMain
#
# This script loads the .class files found in the
# build directory before it uses any jar files.
# Thus, a user can just do
#  ./gradlew compileJava
# to debug changes rather than creating .jar files with "./gradlew assemble".

# When editing, keep this file in sync with javac-debug in this directory.

mydir="`dirname $0`"
case `uname -s` in
    CYGWIN*)
      mydir=`cygpath -m $mydir`
      ;;
esac

binaryDir="${mydir}"/../dist

jdkPaths="${mydir}"/../jdk/annotated

cfDir="${mydir}"/../..
annoToolsDir="${cfDir}"/../annotation-tools
stubparserDir="${cfDir}"/../stubparser
classes="build/classes/java/main"
resources="build/resources/main"

# Put afu jar files last, as they might contain out-of-date CF files.
buildDirs="${cfDir}"/dataflow/"${classes}":"${cfDir}"/javacutil/"${classes}":"${cfDir}"/framework/"${classes}":"${cfDir}"/framework/"${resources}":"${cfDir}"/checker/"${classes}":"${cfDir}"/checker/"${resources}":"${stubparserDir}"/stubparser.jar:"${annoToolsDir}"/scene-lib/bin:"${annoToolsDir}"/annotation-file-utilities/annotation-file-utilities-all.jar

## Preserve quoting and spaces in arguments, which would otherwise be lost
## due to being passed through the shell twice.
# Unset IFS and use newline as arg separator to preserve spaces in args
DUALCASE=1  # for MKS: make case statement case-sensitive (6709498)
saveIFS="$IFS"
nl='
'
for i in "$@" ; do
   IFS=
   args=$args$nl"'"$i"'"
   IFS="$saveIFS"
done

# TODO: We really only want the qualifiers on the CP, but there is no
# easy way to determine them here. ../bin/javac can use
# checker-qual.jar, but it might not exist yet.
# Set .cp to all build directories for now.

# Add the following option for verbose output:
#     "-DCheckerDevelMain.verbose=TRUE" \

eval "java" \
     "-ea " \
     "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005" \
     "-DCheckerDevelMain.cp=${buildDirs} " \
     "-DCheckerDevelMain.pp=${buildDirs} " \
     "-DCheckerDevelMain.compile.bcp=${jdkPaths} " \
     "-DCheckerDevelMain.runtime.cp=${JAVA_HOME}/lib/tools.jar " \
     "-DCheckerDevelMain.binary=${binaryDir} " \
     "-classpath ${buildDirs} " \
     "org.checkerframework.framework.util.CheckerDevelMain" \
     ${args}
