#! /bin/bash

# file: prep			G. Moody	21 December 2013
#				Last revised:	3 March 2017
# Challenge 2017 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

DATA=$CHALLENGE/data
DB=validation

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 [ ! -f dependencies.txt ]; then
    echo "Error: Text file 'dependencies.txt' 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 \`sort /challenge/RECORDS\`; do
        if ! egrep -m1 -x "\$rec,(A|N|O|~)" answers.txt >>/tmp/prep/answers; then
            echo "Error: Result for record '\$rec' is missing or invalid"
        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 dependencies.txt
chmod 755 setup.sh next.sh

if ! timeout -k 10 120 /challenge/prep-pkgdeps --list dependencies.txt \\
     >/tmp/prep/pkgdeps 2>/tmp/prep/pkglist.log; then
    echo "Error: unable to install dependencies"
    exit 1
fi

if [ -e DRYRUN ]; then
    touch /tmp/prep/00_dry_run
fi
touch /tmp/prep/00_unpacked
EOF

cat >pkgdeps.sh <<EOF
#! /bin/bash
# Install package dependencies.

cd /home/vmuser
if ! timeout -k 10 600 /challenge/prep-pkgdeps --install dependencies.txt \\
     >&/tmp/prep/pkginst.log; then
    echo "Error: unable to install dependencies"
    exit 1
fi
touch /tmp/prep/00_pkgdeps
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 1200 ./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_success
EOF

cat >licensecheck.sh <<EOF
#! /bin/bash
timeout -k 10 180 /challenge/find-copyrights /challenge/$SE >> /tmp/prep/00_warnings
touch /tmp/prep/00_licensechecked
EOF

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

# Run the above scripts in the VM.

echo 'licensecheck'
cvmrun -T 240 -c './licensecheck.sh' licensecheck.sh \
    $CHALLENGE_ENTRY `which find-copyrights` \
    -i /tmp/prep -o results/prep -t 00_licensechecked

echo 'unpack'
cvmrun --rw -T 360 -c './unpack.sh >&/tmp/prep/00_log' unpack.sh \
    $CHALLENGE_ENTRY $DATA/$DB/RECORDS `which prep-pkgdeps` \
    -i /tmp/prep -o results/prep -t 00_unpacked

if [ -s results/prep/pkgdeps ]; then
    echo 'pkgdeps'
    challenge-get-pkgdeps < results/prep/pkgdeps \
        2> results/prep/00_log
    cvmrun --rw --overlay -T 660 --user root \
        -c './pkgdeps.sh >&/tmp/prep/00_log' pkgdeps.sh \
        `which prep-pkgdeps` deb \
        -i /tmp/prep -o results/prep -t 00_pkgdeps
fi

opt=
if [ -f results/prep/00_matlab ]; then
    opt="--net --opt $CHALLENGE/matlab.img $CHALLENGE/mlconf.sh"
fi

echo 'compile'
cvmrun $opt --rw --overlay -T 1260 -J 2 \
    -c './compile.sh >&/tmp/prep/00_log' \
    compile.sh \
    -i /tmp/prep -o results/prep -t 00_success
