set BUILD_WIN32=1
set BUILD_TOOLS=0
set BUILD_NOSSE2=1

rem porting is not complete yet! It doesn't even compile (due to the Imaging third-party library) 
rem Creates an executable with a *built-in* module
set BUILD_WIN64=0

set FPC_VER=2.6.2

set MODNAME=_test016

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

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

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

if "%BUILD_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 "%BUILD_WIN32%" == "1" goto w32yes
goto w32end
:w32yes

mkdir ..\..\..\tmp\%MODNAME%\
rem some units are used by both the mother module and the game modules,
rem but with different conditionals. The compiler doesn't know that,
rem  it finds an existing *.ppu and doesn't recompile it. Then things go boom.
del /Q ..\..\..\tmp\%MODNAME%\chelinfo*
rem del /Q  ..\..\..\tmp\%MODNAME%\*
del /Q ..\%MODNAME%.dll
del /Q ..\%MODNAME%_nosse2.dll
set COPTS=-Ooasmcse  -Sc -gw -dcgemodule -dcge -dnotlaz -Mobjfpc -Sh -Rintel -Fi..\..\chentrah\src -Fu..\..\chentrah\src
rem set COPTS=-OoPeepHole -Ooasmcse -OoUncertain -OoRegVar -OpPentium4 -Sc -O3 -gw -dcgemodule -dcge -dnotlaz -Mobjfpc -Sh -Rintel -Fi../../chentrah/src -Fu../../chentrah/src
fpc _chentrah_module.lpr -Twin32 -CfSSE2 %COPTS%  -FU..\..\..\tmp\%MODNAME%\ -Fu..\..\..\tmp\%MODNAME%\
if errorlevel 1 goto err
incbuild build.h
brutalstrip _chentrah_module.dll
upx -9 --force _chentrah_module.dll
finalizedwrflnfo _chentrah_module.dll
move _chentrah_module.dll ..\%MODNAME%.dll
move _chentrah_module.zd2 ..\%MODNAME%.zd2

if "%BUILD_NOSSE2%" == "1" goto nosse2yes
goto w32end
:nosse2yes



rem A special version of the module dll for those ancient CPUs without SSE2 support like AMD Sempron
mkdir ..\..\..\tmp\%MODNAME%\2\
del /Q ..\..\..\tmp\%MODNAME%\2\chelinfo*
fpc _chentrah_module.lpr -Twin32 %COPTS% -FU..\..\..\tmp\%MODNAME%\2\ -Fu..\..\..\tmp\%MODNAME%\2\
if errorlevel 1 goto err
incbuild build.h
brutalstrip _chentrah_module.dll
upx -9 --force _chentrah_module.dll
finalizedwrflnfo _chentrah_module.dll
move _chentrah_module.dll ..\%MODNAME%_nosse2.dll
move _chentrah_module.zd2 ..\%MODNAME%_nosse2.zd2

:w32end




rem using built-in modules for all platforms except Win32. 
if "%BUILD_WIN64%" == "1" goto w64yes
goto w64end
:w64yes

mkdir ..\..\..\tmp\%MODNAME%64\
del /Q ..\..\..\tmp\%MODNAME%64\chelinfo*

del /Q ..\%MODNAME%_64.exe

set VY_PATHS=-Fu..\..\..\3rdparty\Imaging\Source\  -Fu..\..\..\3rdparty\Imaging\Source\ZLib\  
set COPTS=-Ooasmcse  -Sc -dcgemodule -dcge -dnotlaz -Mobjfpc -Sh -Rintel -Fi..\..\chentrah\src -Fu..\..\chentrah\src
rem set COPTS=-OoPeepHole -Ooasmcse -OoUncertain -OoRegVar -OpPentium4 -Sc -O3 -gw -dcgemodule -dcge -dnotlaz -Mobjfpc -Sh -Rintel -Fi../../chentrah/src -Fu../../chentrah/src
ppcrossx64 -dbuildmein -gl 	..\..\chentrah\src\chentrah.lpr -Twin64 %COPTS% %VY_PATHS% -FU..\..\..\tmp\%MODNAME%64\ -Fu..\..\..\tmp\%MODNAME%64\
if errorlevel 1 goto err
incbuild build.h
x86_64-win64-strip chentrah.exe
upx -9 --force chentrah.exe
move chentrah.exe ..\..\..\%MODNAME%64.exe

:w64end

:err
