#! /bin/bash

# file: exam			G. Moody	21 December 2013
#				Last revised:	 5 January 2014
# Challenge 2014 evaluation, stage 3:
# Run a Challenge 2014 entry on the test set in a virtual machine,
# and save its output in a tarball on the standard output.
#
# 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-t-dat.tar.gz
HEA=set-t-hea.tar.gz
cp -p $DAT $HEA $WDIR
cd $WDIR

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

# Annotate the test set.
FAILURES=0
TIMEOUTS=0
for R in \`cat RECORDS\`
do
    if ! timeout -k 20 20 /bin/bash ./next.sh \$R >/dev/null 2>&1; then
	case \$? in
	    124) let TIMEOUTS++ ;;
	      *) let FAILURES++ ;;
        esac
    fi
    let N++
    echo \$N >report
done

# Make the error report.
if [ \$FAILURES -gt 0 ]; then
    echo "next.sh failed on \$FAILURES record(s) in the test set" >report
fi
if [ \$TIMEOUTS -gt 0 ]; then
    echo "next.sh timed out on \$TIMEOUTS record(s) in the test set" >>report
fi
if ! grep -q next.sh report; then
    echo success >report
fi

exit 0
EOF

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

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

# Unpack the test 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

# Copy the helper script into the current directory.
cp -p /media/cdrom0/exam0.sh .

# Run the test, with a 1-hour time limit.
if ! timeout -k 3600 3600 /bin/bash ./exam0.sh >/dev/null 2>&1; then
    NP=\`cat report\`
    NR=\`wc -l <RECORDS\`
    PCT=\`echo "\$NP 100 * \$NR / p" | dc\`
    echo "Stage 3 timed out after one hour $NP $NR (\$PCT% complete)" >report
fi
# Write the report and test set annotations as a uuencoded tarball to the
# standard output.
tar cfz - report *.qrs 2>/dev/null | uuencode -m /dev/stdout

exit 0
EOF

chmod +x exam.sh exam0.sh

# Run the script generated by exam in the VM.
$BVM exec -n $VM --rw -c './exam.sh' exam.sh exam0.sh $DAT $HEA

exit 0
