@echo off

set problem=birch
set author=pkun_n2log
set main=%problem%_%author%

del /Q tests\*
rmdir tests
mkdir tests
rmdir preliminary
mkdir preliminary
echo Compiling generators

pushd src
  echo Compiling GenRandom.java
  javac GenRandom.java 
  copy GenRandom.class .. || exit
popd

echo Generating tests

echo p01 && copy src\p01.hand preliminary\01 > nul
echo p02 && copy src\p02.hand preliminary\02 > nul

echo 01 && copy src\01.hand                                         tests\01 >nul
echo 02 && java GenRandom 890187       14922 231 8 42 100          >tests\02
echo 03 && java GenRandom 271805       55044 610 9 41 100000       >tests\03
echo 04 && java GenRandom 322033       432 216 1 49 50             >tests\04  
echo 05 && java GenRandom 248789       9369 5236 26 24 100000      >tests\05
echo 06 && java GenRandom 604935       77372 13 25 25 100000       >tests\06
echo 07 && java GenRandom 500256       34261 871 25 25 100000      >tests\07
echo 08 && java GenRandom 423509       77643 29 25 25 100000       >tests\08
echo 09 && java GenRandom 924196       578 885 25 25 100000        >tests\09
echo 10 && java GenRandom 133667       62773 550 25 25 100000      >tests\10
echo 11 && java GenRandom 431104       76694 215 25 25 100000      >tests\11
echo 12 && java GenRandom 33187        31210 505 25 25 100000      >tests\12
echo 13 && java GenRandom 166225       19963 410 25 25 100000      >tests\13
echo 14 && java GenRandom 504171       63933 691 25 25 100000      >tests\14
echo 15 && java GenRandom 326553       27093 701 25 25 100000      >tests\15
echo 16 && java GenRandom 698618       57419 3057 143 357 100000   >tests\16
echo 17 && java GenRandom 625121       121104 9185 261 239 100000  >tests\17
echo 18 && java GenRandom 75255        86501 7813 146 354 100000   >tests\18
echo 19 && java GenRandom 857915       69907 9286 272 228 100000   >tests\19
echo 20 && java GenRandom 859284       26479 6324 347 153 100000   >tests\20
echo 21 && java GenRandom 923786       39478 7323 250 250 100000   >tests\21
echo 22 && java GenRandom 212595       83451 8096 250 250 100000   >tests\22
echo 23 && java GenRandom 927320       99141 6559 250 250 100000   >tests\23
echo 24 && java GenRandom 512085       95988 7711 250 250 100000   >tests\24
echo 25 && java GenRandom 555080       19710 9668 250 250 100000   >tests\25
echo 26 && java GenRandom 305797       93308 3544 250 250 100000   >tests\26
echo 27 && java GenRandom 225985       84694 4262 250 250 100000   >tests\27
echo 28 && java GenRandom 277517       11209 3451 250 250 100000   >tests\28
echo 29 && java GenRandom 366047       190574 8010 250 250 100000  >tests\29
echo 30 && java GenRandom 282535       13396 1774 250 250 100000   >tests\30
echo 31 && java GenRandom 636852       63711 9496 1140 860 100000  >tests\31
echo 32 && java GenRandom 35048        95575 6971 1184 816 100000  >tests\32
echo 33 && java GenRandom 157028       88432 7180 1049 951 100000  >tests\33
echo 34 && java GenRandom 960075       65133 8681 1229 771 100000  >tests\34
echo 35 && java GenRandom 154321       94264 7044 1144 856 100000  >tests\35
echo 36 && java GenRandom 240917       85700 3644 1021 979 100000  >tests\36
echo 37 && java GenRandom 415748       82537 5884 882 1118 100000  >tests\37
echo 38 && java GenRandom 187466       75462 4709 965 1035 100000  >tests\38
echo 39 && java GenRandom 244893       82538 8464 1108 892 100000  >tests\39
echo 40 && java GenRandom 57898        62802 3915 1070 930 100000  >tests\40
echo 41 && java GenRandom 672222       71790 1888 1000 1000 100000 >tests\41
echo 42 && java GenRandom 600007       67281 6538 1000 1000 100000 >tests\42
echo 43 && java GenRandom 447141       95573 9053 1000 1000 100000 >tests\43
echo 44 && java GenRandom 867293       77671 6903 1000 1000 100000 >tests\44
echo 45 && java GenRandom 696424       80354 3936 1000 1000 100000 >tests\45
echo 46 && java GenRandom 864613       81686 7570 1000 1000 100000 >tests\46
echo 47 && java GenRandom 727817       68729 5368 1000 1000 100000 >tests\47 
echo 48 && java GenRandom 347523       85701 6473 1000 1000 100000 >tests\48
echo 49 && java GenRandom 17045        65564 5757 1000 1000 100000 >tests\49
echo 50 && java GenRandom 911735       96513 7388 1000 1000 100000 >tests\50

                          
                          
                                                                   
                                                                   

:endGenTest

echo Generating answers

echo Compiling solution
g++ -O2 -Wall -Wextra -Werror -o %main%.exe %main%.cpp || exit

rem echo Compiling validator
rem g++ -O2 -Wall -Wextra -Werror -o validate.exe validate.cpp || exit

echo Compiling validator
if EXIST Validate.java (javac Validate.java || goto continue)
:continue

echo Compiling checker
rem g++ -O2 -Wall -Wextra -Werror -o check.exe check.cpp || exit
dcc32 -U..\..\..\lib -cc check.dpr || exit

rm check.log

echo Generating answers...
for %%i in (preliminary\??) do (
    echo Test %%i
    copy %%i %problem%.in > nul
    if exist Validate.class (
    	java Validate < %problem%.in
    	if errorlevel 1 (
    		echo Validator doesn't accept test %%i
    		goto End
    	)
    	echo Validator accepts test %%i
    )

    set fail_test = %%i

    if EXIST %problem%_%author%.exe (call %problem%_%author%.exe || 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
    if exist check.exe check %%i %%i.a %%i.a
	if exist Check.class java -Xmx256M -Xss64M -cp ../../../lib/testlib4j.jar;. ru.ifmo.testlib.CheckerFramework Check %%i %%i.a %%i.a
  
)

for %%i in (tests\??) do (
    echo Test %%i
    copy %%i %problem%.in > nul
    if exist Validate.class (
    	java Validate < %problem%.in
    	if errorlevel 1 (
    		echo Validator doesn't accept test %%i
    		goto End
    	)
    	echo Validator accepts test %%i
    )

    set fail_test = %%i

    if EXIST %problem%_%author%.exe (call %problem%_%author%.exe || 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
    if exist check.exe check %%i %%i.a %%i.a
	if exist Check.class java -Xmx256M -Xss64M -cp ../../../lib/testlib4j.jar;. ru.ifmo.testlib.CheckerFramework Check %%i %%i.a %%i.a
  
)


echo Cleaning up

del /Q %problem%.in
del /Q %problem%.out
del /Q /S main.exe validate.exe *.class

