echo off

set BUILTIN=bryed3dev

rem set to 1 to recompile accompanying tools (incbuild, brutalstrip and the like)
set COMPILE_TOOLS=1

set COMPILE_WIN32=1

set PACK_RELEASE=0
set RELEASE_DIR=..\..\..\tmp\chentrah\
set RTDIR=..\..\..\

set FPC_VER=2.6.2

set PATH=%PATH%;c:\FPC\%FPC_VER%\bin\i386-win32\;..\..\..\bin\

set CODE_OPTIONS=-OpPentiumM -O3
set CGE_OPTIONS=-gw2 -Xi -XX -dcge -dnotlaz -dcge_rc_directly -Mobjfpc -Sh -Sc -Rintel %CODE_OPTIONS%
set CGE_PATHS=-FE..\..\..\ -Fi..\..\%BUILTIN%\src\ -Fu..\..\%BUILTIN%\src\

set VY_PATHS=-Fu..\..\..\3rdparty\Imaging\Source\  -Fu..\..\..\3rdparty\Imaging\Source\ZLib\  -Fi..\..\..\3rdparty\Imaging\Source\ -Fu..\..\..\3rdparty\Imaging\Source\JpegLib\"

mkdir ..\..\..\tmp\
mkdir ..\..\..\tmp\main
mkdir ..\..\..\tmp\tools

del /Q .\*.ppu
del /Q .\*.o

bin2obj -o cl_builtin_font.inc -c builtin_font font.png
bin2obj -o cl_console_font.inc -c iim_console consolefont.png
bin2obj -o cl_bgimg_error.inc -c error_bg_img cl_background_error.png
bin2obj -o cl_cursor_cyan.inc -c cursor_cyan_img cl_cursor_normal.png
bin2obj -o cl_cursor_yellow.inc -c cursor_yellow_img cl_cursor_error.png
bin2obj -o cl_cursor_hourglass.inc -c cursor_hourglass_img cl_cursor_hourglass.png
bin2obj -o cl_minifont.inc -c minifont_img cl_minifont.png
bin2obj -o cl_errbutton.inc -c errbutton_img cl_errbutton.png
bin2obj -o cl_erricon.inc -c erricon_img cl_erricon.png

if "%COMPILE_TOOLS%" == "1" goto tooyes
goto tooend
:tooyes
fpc incbuild.pp -FE..\..\..\bin\ -FU..\..\..\tmp\tools
fpc brutalstrip.pp -FE..\..\..\bin\ -FU..\..\..\tmp\tools
fpc extractdwrflnfo.pp -FE..\..\..\bin\ -FU..\..\..\tmp\tools
fpc finalizedwrflnfo.pp -FE..\..\..\bin\ -FU..\..\..\tmp\tools
:tooend

if "%COMPILE_WIN32%" == "1" goto w32yes
goto w32end
:w32yes
del /Q ..\..\..\chentrah.exe
fpc chentrah.lpr %CGE_OPTIONS% -CX -Twin32 -FU..\..\..\tmp\main\ %CGE_PATHS% %VY_PATHS%
if errorlevel 1 goto err
incbuild build.h
brutalstrip ..\..\..\chentrah.exe
rem UPX is mandatory, it fixes the executable broken by brutalstrip.
rem  (how does all of this work I have no slightest clue.
rem    I guess I missed some vital detail when I was writing brutalstrip)
upx -9 --force ..\..\..\chentrah.exe
finalizedwrflnfo ..\..\..\chentrah.exe
rem move ..\..\..\chentrah.zd2 ..\bin\
:w32end

:err
