#!/bin/sh

# Psacal compile wrapper-script for 'compile.sh'.
# See that script for syntax and more info.

DEST="$1" ; shift
MEMLIMIT="$1" ; shift
MAINSOURCE="$1"

if [ -n "$ENTRY_POINT" ]; then
	MAINSOURCE="$ENTRY_POINT"
else
	# Fpc only accepts a single source file, and will detect and compile
	# unit dependencies itself. Let's try to autodetect the main program.
	NUMSOURCES=0
	for i in "$@" ; do
		NUMSOURCES=$((NUMSOURCES+1))
		if grep -E '^[[:space:]]*program[[:space:]]+[A-Za-z_0-9]+([[:space:]]*\([[:space:]]*[A-Za-z_0-9]+[[:space:]]*,[[:space:]]*[A-Za-z_0-9]+[[:space:]]*\))?[[:space:]]*;' "$i" >/dev/null ; then
			FOUND="$i"
		fi
	done
	if [ $NUMSOURCES = 1 ]; then
		true # Skip this case.
	elif [ -n "$FOUND" ]; then
		[ -n "$DEBUG" ] && echo "Debug: using '$FOUND' as main program file."
		MAINSOURCE="$FOUND"
	else
		echo "Warning: could not determine main program file, using '$MAINSOURCE'."
	fi

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

# Add -dONLINE_JUDGE -dDOMJUDGE below if you want it make easier for teams to
# do local debugging.

# -viwn:    Verbose warnings, notes and informational messages
# -02:      Level 2 optimizations (default for speed)
# -Sg:      Support label and goto commands (for those who need it ;-)
# -XS:      Static link with all libraries
fpc -viwn -O2 -Sg -XS -o"$DEST" "$MAINSOURCE"
exitcode=$?

# clean created object files:
rm -f "$DEST.o"

exit $exitcode
