#!/usr/bin/env bash

PROBLEM=dreamrun
AUTHOR=ag

echo Generating tests...
rm -rf tests
cd src
javac TestGen.java
java -Xmx256M -Xss64M -ea TestGen
cd ..

echo Compiling checker...
rm -rf __tmp
mkdir __tmp
javac -classpath "../../lib/testlib4j.jar" -sourcepath . -d __tmp Check.java
cp ../../lib/testlib4j.jar __tmp
cd __tmp
jar xf testlib4j.jar
echo Main-Class: ru.ifmo.testlib.CheckerFramework > META-INF/manifest.1
echo Checker-Class: Check >> META-INF/manifest.1
rm -f *.java
rm -f testlib4j.jar
jar cfm check.jar META-INF/manifest.1 *.class ru
cp check.jar ..
cd ..
rm -rf __tmp

echo Compiling validator...

rm -rf __tmp
mkdir __tmp
javac -sourcepath . -d __tmp Validate.java
cd __tmp
mkdir META-INF
echo Main-Class: Validate > META-INF/manifest.1
jar cfm validate.jar META-INF/manifest.1 *.class
cp validate.jar .. > nul
cd ..
rm -rf __tmp

echo Generating answers...

javac "$PROBLEM"_"$AUTHOR".java -encoding utf8

for f in tests/*
do
    echo Test $f
    cp $f "$PROBLEM".in
    java -jar validate.jar < "$PROBLEM".in

    if [ $? -ne 0 ]
	then
	    echo "Validator rejects test" $f
	    break
	fi
    echo "Validator accepts test" $f

    java -Xmx256M -Xss64M "$PROBLEM"_"$AUTHOR"
    
    cp "$PROBLEM".out $f.a
    echo Check $f
    java -jar check.jar $f $f.a $f.a
done