#!/bin/sh

# Compare wrapper-script to be called from 'testcase_run.sh'.
#
# Usage: $0 <testdata.in> <testdata.out> <feedback_dir> [optional arguments] < <team output>
#
# <testdata.in>   File containing testdata input.
# <testdata.out>  File containing the correct output.
# <feedback_dir>  Directory where one can store judgemessage.txt, teammessage.txt, score.txt...
#
#
# Exits with exitcode
# - 1  if an internal error occurs,
# - 42 if the team output is a correct output,
# - 43 if the team output is incorrect.
#
# This script calls another program to check the results.
# Calling syntax:
#
#    $CHECK_PROGRAM <testdata.in> <testdata.out> < <team output>
#
# The $CHECK_PROGRAM should return the contents of <diff.out> to
# standard output. It must exit with exitcode zero to indicate
# successful checking.

SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
CHECK_PROGRAM="${SCRIPTPATH}/CheckGuessGame.class"
CHECK_PROGRAM_COMMAND="java -cp ../guessgame_cmp CheckGuessGame"
echo "Program = $CHECK_PROGRAM"
echo "Program call = $CHECK_PROGRAM_COMMAND"


TESTIN="$1"
TESTOUT="$2"
FEEDBACKDIR="$3"

# Options to pass to check program:
CHECK_OPTIONS="$FEEDBACKDIR"

DIFFOUT="${FEEDBACKDIR}/judgemessage.txt"

if [ ! -f "$CHECK_PROGRAM" ]; then
	echo "Error: '$CHECK_PROGRAM' not found." >&2
	echo "Internal error"
	exit 1
fi

# Run the program:
$CHECK_PROGRAM_COMMAND "$TESTIN" "$TESTOUT" $CHECK_OPTIONS < /dev/stdin > "$DIFFOUT"
EXITCODE=$?

# Exit with failure, when non-zero exitcode found:
if [ $EXITCODE -ne 0 ]; then
	echo "Error: '$CHECK_PROGRAM' exited with exitcode $EXITCODE." >&2
	echo "Internal error"
	exit 1
fi

# Check result and write result file:
if [ -s "$DIFFOUT" ]; then
	exit 43 # Wrong Answer
else
	exit 42 # Correct
fi

exit 0
