#!/usr/bin/env bash

PROBLEM=dreamrun

if [ -z "$1" ]
then
	echo No parameters
	exit
fi

cmd="echo No solution found"

echo Compiling solution

if [ -f "$PROBLEM"_"$1".cpp ]
then
	g++ "$PROBLEM"_"$1".cpp -O2 -Wall -std=c++11 -o "$PROBLEM"_"$1".bin
	if [ $? -ne 0 ]
	then
		echo "Compilation error"
		exit
	fi
	chmod +x "$PROBLEM"_"$1".bin
	cmd=./"$PROBLEM"_"$1".bin
fi
if [ -f "$PROBLEM"_"$1".java ]
then
	javac "$PROBLEM"_"$1".java
	if [ $? -ne 0 ]
	then
		echo "Compilation error"
		exit
	fi
	cmd="java $PROBLEM"_"$1"
fi

echo Checking solution...

echo `date` > result_"$PROBLEM"_"$1".log

for f in tests/??
do
    rm -f "$PROBLEM".out
    echo "running on test" $f
    cp $f "$PROBLEM".in

    `$cmd`

    echo $f >> result_"$PROBLEM"_"$1".log

    java -jar check.jar $f $PROBLEM.out $f.a >> result_"$PROBLEM"_"$1".log
    if [ $? -ne 0 ]
	then
        echo failed
    else
        echo ok
    fi
done
