#! /bin/bash

# file: prep			G. Moody	21 December 2013
#				Last revised:	21 April 2015
# Challenge 2015 evaluation, stage 1:  prepare an entry for evaluation

set -e

# Check the environment variables.
if [ ! -d "$CHALLENGE" ]; then
    echo "CHALLENGE not set"
    exit 1
fi
if [ ! -f "$CHALLENGE_ROOT_IMG" ]; then
    echo "CHALLENGE_ROOT_IMG not set"
    exit 1
fi
if [ ! -f "$CHALLENGE_HOME_IMG" ]; then
    echo "CHALLENGE_HOME_IMG not set"
    exit 1
fi
if [ ! -f "$CHALLENGE_ENTRY" ]; then
    echo "CHALLENGE_ENTRY not set"
    exit 1
fi

SE=`basename "$CHALLENGE_ENTRY"`

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

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

EN=$SE

case \$EN in
  entry.tar.gz) if ! tar xfz /challenge/entry.tar.gz ; then
		    echo "Error: entry.tar.gz cannot be unpacked"
		    exit 1
		fi ;;
  entry.zip)    if ! unzip -o -q /challenge/entry.zip ; then
		    echo "Error: entry.zip cannot be unpacked"
		    exit 1
		fi ;;
  *)		echo "Error: invalid entry name (\$EN)"
		exit 1 ;;
esac

# Check to see if all required components are present, reject if not.
EX=0
if [ ! -s setup.sh ]; then
    echo "Error: Script 'setup.sh' is missing"; EX=1;
fi
if [ ! -s next.sh ]; then
    echo "Error: Script 'next.sh' is missing"; EX=1;
fi
if [ ! -s AUTHORS.txt ]; then
    echo "Error: Text file 'AUTHORS.txt' is missing or empty"; EX=1;
fi
if [ ! -s LICENSE.txt ]; then
    echo "Error: Text file 'LICENSE.txt' is missing or empty"; EX=1;
fi
if [ ! -f answers.txt ]; then
    echo "Error: Results file 'answers.txt' is missing"; EX=1
else
    sed 's/\\r\$//' -i answers.txt
    for rec in \`cat /challenge/RECORDS\`; do
        if ! grep -m1 -x "\$rec,[01]" answers.txt >>/tmp/prep/answers; then
            echo "Error: Result for record '\$rec' is missing"
        fi
    done | sed '11{s/.*/ .../;q}' | grep . && EX=1
fi

if [ -z "$CHALLENGE_IGNORE_MISSING" ] && [ \$EX -ne 0 ]; then
    echo "Required component(s) missing -- not scored"
    exit 1
fi

sed 's/\\r\$//' -i setup.sh next.sh answers.txt
chmod 755 setup.sh next.sh

if grep -q '^NEED_MATLAB=1' setup.sh; then
    touch /tmp/prep/00_matlab
fi
touch /tmp/prep/00_unpacked
EOF

cat >compile.sh <<EOF
#! /bin/bash
# Compile or perform other one-time initialization of the entry.
cd

[ -f /challenge/mlconf.sh ] && . /challenge/mlconf.sh

timeout -k 10 300 ./setup.sh >/tmp/prep/setup.log 2>&1
x=\$?
if [ \$x != 0 ]; then
    case \$x in
	124) echo "Error: setup.sh timed out after 5 minutes" ;;
	  *) echo "Error: setup.sh failed (status \$x)" ;;
    esac
    exit 1   # quit if error or timeout in setup.sh
fi
touch /tmp/prep/00_compiled
EOF

cat >licensecheck.sh <<EOF
#! /bin/bash
cd /opt/vmuser
timeout -k 10 180 /challenge/check-entry-copyrights >> /tmp/prep/00_warnings
touch /tmp/prep/00_success
EOF

chmod +x unpack.sh compile.sh licensecheck.sh

# (FIXME: Ubuntu locks up if we use -J 2 while limited to 2 real CPUs, why?)

# Run the script generated by prep in the VM.
cvmrun --rw -T 240 -c './unpack.sh >&/tmp/prep/00_log' unpack.sh \
    $CHALLENGE_ENTRY $CHALLENGE/data/quiz/RECORDS \
    -i /tmp/prep -o results/prep
[ -f results/prep/00_unpacked ]

if [ -f results/prep/00_matlab ]; then
    cvmrun --net --opt $CHALLENGE/matlab.img \
        --rw -T 360 -c './compile.sh >&/tmp/prep/00_log' compile.sh \
        $CHALLENGE/mlconf.sh -i /tmp/prep -o tainted-prep
    $CHALLENGE/scrub tainted-prep results/prep
else
    cvmrun --rw -T 360 -c './compile.sh >&/tmp/prep/00_log' compile.sh \
	-i /tmp/prep -o results/prep
fi
[ -f results/prep/00_compiled ]

cvmrun -T 240 --home $CHALLENGE/home.img --opt $CHALLENGE_HOME_IMG \
    -c './licensecheck.sh' licensecheck.sh `which check-entry-copyrights` \
    -i /tmp/prep -o results/prep
[ -f results/prep/00_success ]
