@echo off

for %%i in (.) do set problem=%%~ni

set author=va
set fail_test=""

mkdir tests

cd src
echo Compiling generator
javac Gen.java || goto CompGenError
echo Generating tests...
java Gen || goto GenError
cd ..

echo Compiling author solution
if EXIST %problem%_%author%.dpr (dcc32 -cc %problem%_%author%.dpr || goto CompSourceError)
if EXIST %problem%_%author%.cpp (g++ %problem%_%author%.cpp -o %problem%_%author%.exe || goto CompSourceError)
if EXIST %problem%_%author%.java (javac %problem%_%author%.java -encoding utf8 || goto CompSourceError)

rem javac %problem%_%author%.java || goto CompSourcesError

rem dcc32 -U..\..\lib -cc check.dpr
call javac -classpath ../../lib/testlib4j.jar Check.java -encoding utf8 
call jar cf Check.jar *.class

echo Generating answers...
for %%i in (tests\??) do (
	echo Test %%i
	copy %%i %problem%.in > nul

        set fail_test = %%i

        if EXIST %problem%_%author%.exe (call %problem%_%author%.dpr || goto SourceRunningError)
        if EXIST %problem%_%author%.class (call java -Xmx256M -Xss64M -ea %problem%_%author% || goto SourceRunningError)
        if EXIST %problem%_%author%.py (call python %problem%_%author%.py || goto SourceRunningError)

	copy %problem%.out %%i.a > nul
        java -Xmx256M -Xss64M -cp ../../lib/testlib4j.jar;Check.jar ru.ifmo.testlib.CheckerFramework Check %%i %%i.a %%i.a
)

goto End

:CompGenError 
  echo Cannot compile generator
  goto End
:GenError 
  echo Cannot generate tests
  goto End
:CompSourcesError
  echo Cannot compile source
  goto End
:SourceRunningError
  echo Fail in generating answers on test %fail_test%

:End

rem call clean