#!/bin/sh

# Java compile wrapper-script for 'compile.sh'.
# See that script for syntax and more info.
#
# This script byte-compiles with the javac compiler and
# generates a shell script to run it with the java interpreter later.
# It autodetects the main class name and optionally renames the source
# file if the class is public.
#
# This script requires that java is installed in the chroot.

DEST="$1" ; shift
MEMLIMIT="$1" ; shift
MAINSOURCE="$1"
MAINCLASS=""
COMPILESCRIPTDIR="$(dirname "$0")"

# Stack size in the JVM in KB. Note that this will be deducted from
# the total memory made available for the heap.
MEMSTACK=65536

# Amount of memory reserved for the Java virtual machine in KB. The
# default below is just above the maximum memory usage of current
# versions of the jvm, but might need increasing in some cases.
MEMJVM=65536

MEMRESERVED=$((MEMSTACK + MEMJVM))

# Calculate Java program memlimit as MEMLIMIT - max. JVM memory usage:
MEMLIMITJAVA=$((MEMLIMIT - MEMRESERVED))

if [ $MEMLIMITJAVA -le 0 ]; then
	echo "internal-error: total memory $MEMLIMIT KiB <= $MEMJVM + $MEMSTACK = $MEMRESERVED KiB reserved for JVM and stack leaves none for heap."
	exit 1
fi

# Java needs filename to match main class:
if [ -z "$ENTRY_POINT" ]; then
	[ -n "$DEBUG" ] && echo "Debug: no ENTRY_POINT provided, trying to detect main class."
else
	[ -n "$DEBUG" ] && echo "Debug: using main class provided by ENTRY_POINT='$ENTRY_POINT'."
	MAINCLASS="$ENTRY_POINT"
fi

TMPFILE=$(mktemp domjudge_javac_output.XXXXXX) || exit 1

# Byte-compile:
javac -encoding UTF-8 -sourcepath . -d . "$@" 2> "$TMPFILE"
EXITCODE=$?

cat $TMPFILE
rm -f $TMPFILE

[ "$EXITCODE" -ne 0 ] && exit $EXITCODE

if [ -z "$MAINCLASS" ]; then
	# Look for class that has the 'main' function:
	CLASSNAMES="$(find ./* -type f -regex '^.*\.class$' \
	            | sed -e 's/\.class$//' -e 's/^\.\///' -e 's/\//./g')"
	MAINCLASS=$(java -cp "$COMPILESCRIPTDIR" DetectMain "$(pwd)" $CLASSNAMES)
	EXITCODE=$?

	# Report the entry point, so it can be saved, e.g. for later replay:
	echo "Info: detected entry_point: $MAINCLASS"

	[ "$EXITCODE" -ne 0 ] && exit $EXITCODE
else
	# Check if entry point is valid
	java -cp "$COMPILESCRIPTDIR" DetectMain "$(pwd)" $MAINCLASS > /dev/null
	EXITCODE=$?
	[ "$EXITCODE" -ne 0 ] && exit $EXITCODE
fi

# Write executing script:
# Executes java byte-code interpreter with following options
# -Xmx: maximum size of memory allocation pool
# -Xms: initial size of memory, improves runtime stability
# -XX:+UseSerialGC: Serialized garbage collector improves runtime stability
# -Xss${MEMSTACK}k: stack size as configured above
# -Dfile.encoding=UTF-8: set file encoding to UTF-8
cat > "$DEST" <<EOF
#!/bin/sh
# Generated shell-script to execute java interpreter on source.

# Detect dirname and change dir to prevent class not found errors.
if [ "\${0%/*}" != "\$0" ]; then
	cd "\${0%/*}"
fi

# Add -DONLINE_JUDGE or -DDOMJUDGE below if you want it make easier for teams
# to do local debugging.

exec java -Dfile.encoding=UTF-8 -XX:+UseSerialGC -Xss${MEMSTACK}k -Xms${MEMLIMITJAVA}k -Xmx${MEMLIMITJAVA}k '$MAINCLASS' "\$@"
EOF

chmod a+x "$DEST"

exit 0
