#!/bin/bash
source ./include.sh
./clean.sh

echo Compiling...
if [ -e ../solutions/$SOL.cpp ] ; then
	g++ -O2 -Wall ../solutions/$SOL.cpp -o ./$SOL.exe
fi
if [ -e ../solutions/$SOL.java ] ; then
	javac ../solutions/$SOL.java -d .
fi
if [ -e gen.cpp ] ; then
	g++ -O2 -Wall gen.cpp -o gen.exe
fi
if [ -e validate.cpp ] ; then
	g++ -O2 -Wall validate.cpp -o validate.exe
fi

echo Generating tests...
if [ -e small.in ] ; then
	cat small.in >>$PROBLEM.in
fi
if [ -e gen.py ] ; then
	python ./gen.py 01 >>$PROBLEM.in
fi
if [ -e gen.cpp ] ; then
	./gen.exe
fi

echo Generating answers...
export fullname=$1
#echo $fullname
export solname=${fullname##*/}
#echo $solname
for t in ??? ; do
	echo Test $t
	cp $t $PROBLEM.in
	if [ -e ./validate.exe ] ; then
		./validate.exe <$PROBLEM.in
	fi
	if [ -e $SOL.py ] ; then
		python $SOL.py
	elif [ -e $solname.class ] ; then
		java -cp . -Xmx128M -Xss32M $solname
	else
		./$SOL.exe #2>$t.e
#		./$SOL.exe <$t >$t.a
	fi
	cp $PROBLEM.out $t.a
done

#echo Splitting tests...
#python ./splittests.py

if [ ! -e ../tests ] ; then
	mkdir ../tests
fi
mv ??   ../tests || mv ???   ../tests
mv ??.a ../tests || mv ???.a ../tests
mv ??.e ../tests || mv ???.e ../tests
if $DOCLEAN ; then
	./clean.sh
fi
