@echo off

set problem=game
set author=ek
rem you can set lang as dpr, java, cpp or python
set lang=cpp

echo Compiling checker
g++ -std=c++11 -O2 -I..\..\..\lib -o check.exe check.cpp

echo Compiling validator
g++ -O2 -I..\..\..\lib -o validate.exe validate.cpp
g++ -O2 -I..\..\..\lib -o validate_subtask1.exe validate_subtask1.cpp
g++ -O2 -I..\..\..\lib -o validate_subtask2.exe validate_subtask2.cpp
g++ -O2 -I..\..\..\lib -o validate_subtask3.exe validate_subtask3.cpp

echo Generating tests
if exist preliminary rd /s /q preliminary
if exist tests rd /s /q tests
pushd src
python TestGen.py
popd

echo Compiling main solution
pushd main
g++ -O2  -Wall -Wextra -Wconversion -Werror -Wno-unused-result -I..\..\..\lib -o ..\%problem%_%author%.exe %problem%_%author%.cpp
popd

echo Generating answers
for %%i in (preliminary, tests\subtask1, tests\subtask2, tests\subtask3, tests\subtask4) do (
    for %%j in (%%i\??) do (
        echo Copying input for test %%j
        copy %%j %problem%.in > nul

        echo Validating test %%j
        validate < %problem%.in
        if errorlevel 1 (
            echo Validator rejects test %%j
            goto end
        )
        if [%%i]==[tests\subtask1] (
            validate_subtask1 < %problem%.in
            if errorlevel 1 (
                echo Validator rejects test %%j
                goto end
            )
        )
        if [%%i]==[tests\subtask2] (
            validate_subtask2 < %problem%.in
            if errorlevel 1 (
                echo Validator rejects test %%j
                goto end
            )
        )
        if [%%i]==[tests\subtask3] (
            validate_subtask3 < %problem%.in
            if errorlevel 1 (
                echo Validator rejects test %%j
                goto end
            )
        )
        echo Validator accepts test %%j

        echo Running solution on test %%j
        %problem%_%author%.exe
        if errorlevel 1 (
            echo Solution failed on test %%j
            goto end
        )

        echo Running checker on test %%j
        check %%j %problem%.out %problem%.out
        if errorlevel 1 (
            echo Checker rejects main solution output on test %%j
            goto end
        )

        echo Saving answer for test %%j
        copy %problem%.out %%j.a > nul
    )
)


:end
