@echo off

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

set author=%1

if "%1" == "" (
  echo No parameters
  goto End
)

set exec = echo No execution command

echo Compiling solution
if EXIST %problem%_%author%.dpr (
dcc32 -cc %problem%_%author%.dpr || goto CompSourceError
set exec=run -i %problem%.in -o %problem%.out -m 256M %problem%_%author%.exe
)
if EXIST %problem%_%author%.cpp (
g++ %problem%_%author%.cpp -std=c++11 -O2 -Wall -Wl,--stack=67108864 -o %problem%_%author%.exe || goto CompSourceError
set exec=run -i %problem%.in -o %problem%.out -m 256M %problem%_%author%.exe
)
if EXIST %problem%_%author%.java (
javac %problem%_%author%.java -encoding utf8 || goto CompSourceError
set exec=run -i %problem%.in -o %problem%.out -m 256M java -ea -Xss64M -Xmx256M %problem%_%author%
)
if EXIST %problem%_%author%.py (
set exec=run -i %problem%.in -o %problem%.out -m 256M python %problem%_%author%.py
)
if EXIST %problem%_%author%.pl (
set exec=run -i %problem%.in -o %problem%.out -m 256M perl %problem%_%author%.pl
)

call javac -classpath ../../lib/testlib4j.jar Check.java -encoding utf8 
call jar cf Check.jar *.class

echo Checking solution...
for %%i in (tests\???) do (
    echo running on test %%i
    copy %%i %problem%.in > nul

    %exec%
    java -Xmx256M -Xss64M -cp ../../lib/testlib4j.jar;Check.jar ru.ifmo.testlib.CheckerFramework Check %%i %problem%.out %%i.a
     if errorlevel 1 (
     	echo %%i failed >> result_%problem%_%author%.log	
   	) else (
   	    echo %%i ok >> result_%problem%_%author%.log
   	)


)

goto End

:CompSourcesError
  echo Cannot compile source
  goto End
:SourceRunningError
  echo Fail in running on tests

:End
