#! /bin/bash

# file: quiz			G. Moody	21 December 2013
#				Last revised:	Ikaro Silva June 6 2014 
# Challenge 2014 evaluation, stage 2:
# Run a Challenge 2014 entry on the training set in a virtual machine,
# and check that its output is as expected.
#
# This script must be run in the VM since it executes user code!

# Check the command-line arguments, quit if invalid.
if [ $# -ne 1 ]; then
    echo "usage: $0 VIRTUALMACHINENAME"
    exit 1
fi

VM=$1
if [ "x$VM" = "x" ]; then
    echo "$0: |$VM| is not a valid virtual machine name";
    exit 1
fi

cd `dirname $0`/data
DAT=set-p-dat.tar.gz
HEA=set-p-hea.tar.gz
cp -p $DAT $HEA $WDIR
cd $WDIR

cat >quiz.sh <<EOF
#! /bin/bash

# Work in the user's home directory, because the root file system is read-only.
cd

# File to be used to log errors, warnings, or success.
LOG=log.txt

function cleanup {
    cd
    if [ -s \$LOG ]; then
        cat \$LOG
    fi
}
trap cleanup EXIT

# Unpack the training set .dat and .hea files into the current directory.
tar xfz /media/cdrom0/$DAT >/dev/null 2>&1
tar xfz /media/cdrom0/$HEA >/dev/null 2>&1

# Annotate the training set, compare results with submitted annotation files.
for R in \`seq 100 199\`
do
    if timeout -k 40 40 /bin/bash ./next.sh \$R >\$LOG 2>&1
    then
	if ! cmp \$R.qrs challenge/2014/set-p/\$R.qrs >/dev/null 2>&1; then
	    echo "next.sh \$R: output does not match submission"
	    exit 1  # quit if mismatch
	else
	    rm -f $R.qrs
	fi
    else
	case $? in
	    124) echo "next.sh \$R: timed out after 40 seconds" >\$LOG ;;
	      *) if [ ! -s \$LOG ]; then
	             echo "next.sh \$R: unknown error" >\$LOG
		 fi ;;
        esac
	cat \$LOG
	exit 1  # quit if error or timeout in next.sh
    fi
done

rm -f \$LOG

if [ -f DRYRUN ]; then
echo "Dry run completed with no error.  Remove the DRYRUN file from" >\$LOG
echo "the entry and resubmit it if you wish to receive a score for it." >>\$LOG
exit 1
fi

echo success
exit 0
EOF

chmod +x quiz.sh

LOG=`mktemp -p $WDIR`

# Run the script generated by quiz in the VM.
timeout 4100 $BVM exec -n $VM --rw -c './quiz.sh' quiz.sh $DAT $HEA >$LOG 2>&1
cat $LOG
grep -q success $LOG

exit $?
