#!/bin/bash
#
# Copyright (C) 2003 Ondrej Lhotak
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
CONTEST_DIR=`dirname $0`/..;

SUCCESS=115
COMPILE_ERR=99
FORMAT_ERR=102
INCORRECT=105
RUNTIME_ERR=114
TIME_ERR=116
IGNORE=103


FILE=`echo $CONTEST_DIR/var/submissions/$1.*`;
if echo FILE | grep ' '; then
    echo ERROR: multiple submissions with same number: $FILE.
    exit 1;
fi
if [ -z $FILE -o ! -e $FILE ]; then
    echo No submission with number $1.
    exit 1;
fi

if  $CONTEST_DIR/bin/has_been_judged $1 ; then
    echo Submission $1 has already been judged.
    exit 1;
fi

mkdir $CONTEST_DIR/var/judgedirs/$1
cp $CONTEST_DIR/var/submissions/$1.* $CONTEST_DIR/var/judgedirs/$1
EXTENSION=${FILE/*./}
if [ ! -e $CONTEST_DIR/bin/compile_$EXTENSION ]; then
    echo No compilation plugin for submissions with extension $EXTENSION.
    exit 1;
fi
if [ ! -e $CONTEST_DIR/bin/run_$EXTENSION ]; then
    echo No running plugin for submissions with extension $EXTENSION.
    exit 1;
fi
TMP=${FILE/%.$EXTENSION/}
PROBLEM=${TMP/*./}
TMP=${FILE/%.$PROBLEM.$EXTENSION/}
TEAM=${TMP/*./}

SUGGESTION=;

$CONTEST_DIR/bin/compile_$EXTENSION $1 $PROBLEM $TEAM && 
$CONTEST_DIR/bin/run_$EXTENSION $1 $PROBLEM $TEAM && 
$CONTEST_DIR/bin/compare_output $1 $PROBLEM $TEAM

case $? in
    $SUCCESS     ) SUGGESTION=s;;
    $COMPILE_ERR ) SUGGESTION=c;;
    $FORMAT_ERR  ) SUGGESTION=f;;
    $INCORRECT   ) SUGGESTION=i;;
    $RUNTIME_ERR ) SUGGESTION=r;;
    $TIME_ERR    ) SUGGESTION=t;;
    $IGNORE      ) SUGGESTION=g;;
esac

while true; do
    echo 
    echo 's - satisfactory (correct submission)'
    echo 'c - compile error'
    echo 'f - format error'
    echo 'i - incorrect (wrong answer)'
    echo 'r - runtime error'
    echo 't - timeout (ran too long)'
    echo 'g - ignore submission (do not penalize)'
    echo '^c - ignore for now (do not judge)'
    echo -n "Enter judgement (default is $SUGGESTION): "
    read JUDGEMENT;
    if [ -z $JUDGEMENT ]; then JUDGEMENT=$SUGGESTION; fi
    if echo $JUDGEMENT | grep ^[scfirtg]$ >& /dev/null ;  then
        echo `$CONTEST_DIR/bin/timestamp`:J:$1:$JUDGEMENT >> $CONTEST_DIR/var/log
        echo Judgement $JUDGEMENT noted.
        TEXT_JUDGEMENT='';
        case $JUDGEMENT in
            s ) TEXT_JUDGEMENT="CORRECT";;
            c ) TEXT_JUDGEMENT="Compile error";;
            f ) TEXT_JUDGEMENT="Format error";;
            i ) TEXT_JUDGEMENT="Wrong answer";;
            r ) TEXT_JUDGEMENT="Runtime error";;
            t ) TEXT_JUDGEMENT="Ran too long";;
        esac
        if [ -n "$TEXT_JUDGEMENT" ]; then
            SUB=sub`perl -e 'print 2000000000-time;'`
            echo "Judgement for problem $PROBLEM: $TEXT_JUDGEMENT" > $CONTEST_DIR/var/submitdirs/$TEAM/$SUB;
        fi;
        break;
    fi
    echo Invalid judgement: $JUDGEMENT
done
