#!/bin/bash

problem=demo
author=$1

if [ "$author" == "" ]; then
  echo No parameters
  exit
fi

exec=

echo Compiling solution
if [ -f ${problem}_${author}.dpr ]; then
  dcc32 -cc ${problem}_${author}.dpr || exit
  exec="./${problem}_${author}.exe"
fi
if [ -f ${problem}_${author}.cpp ]; then
  g++ ${problem}_${author}.cpp -O2 -Wall -std=c++11 -o ${problem}_${author}.exe || exit
  exec="./${problem}_${author}.exe"
fi
if [ -f ${problem}_${author}.java ]; then
  javac ${problem}_${author}.java -encoding utf8 || exit
  exec="java -ea -Xss64M -Xmx512M ${problem}_${author}"
fi
if [ -f ${problem}_${author}.py ]; then 
  exec="python ${problem}_${author}.py"
fi
#if EXIST %problem%_%author%.d (
#dmd %problem%_%author%.d
#set exec=run -m 512M %problem%_%author%.exe
#)

if [ "$exec" == "" ]; then
  echo Solution not found
  exit
fi
                               
echo Checking solution...

date > result_${problem}_${author}.log

for i in `find tests/ | sort -n | grep tests/..$`;
do
    rm -f ${problem}.out
    echo running on test $i
    cp $i ${problem}.in > /dev/null

    $exec
    
    if [ -f check.exe ]; then
      ./check $i ${problem}.out ${i}.a
    else
      if [ -f check.jar ]; then
        java -jar check.jar $i ${problem}.out ${i}.a
      else
        echo Checker not found
        exit
      fi
    fi
    if [ $? -ne 0 ]; then
        echo $i failed >> result_${problem}_${author}.log
    else 
        echo $i ok >> result_${problem}_${author}.log
    fi
done

rm -f ${problem}.in ${problem}.out
