@echo off
set dmdVer=2.057
set gtkdRev=949
echo Set your dmd path here and comment this line && pause && goto ExitNoCd
set path=D:\D\dmd%dmdVer%\windows\bin;%path%

setlocal enabledelayedexpansion
echo Using dmd %dmdVer% and GtkD r%gtkdRev%


cd src-r%gtkdRev%
if exist build rmdir /s /q build

if not exist gtk-one-obj-%dmdVer%.lib (
    echo Building gtkD-r%gtkdRev% in a single object file
    echo We will have ~16 MiB library...

    if exist args del args
    for /r %%f in (*.d) do (
        set file=%%f
        set file=!file:%cd%\=!
        echo "!file!" >> args
    )
    dmd @args -lib -ofgtk-one-obj-%dmdVer%.lib
    if errorlevel 1 goto Error
    del args
)

if not exist main-one-obj-%dmdVer%.exe (
    echo Building main.d
    echo We will have ~123 MiB executable with dmd 2.054-2.057 and maybe eralier...
    dmd -map ..\main.d gtk-one-obj-%dmdVer%.lib -ofmain-one-obj-%dmdVer%.exe
    if errorlevel 1 goto Error
)

echo Testing main-one-obj-%dmdVer%.exe...
main-one-obj-%dmdVer%.exe
if errorlevel 1 goto Error

if not exist gtk-sep-obj-%dmdVer%.lib (
    echo Building gtkD-r%gtkdRev% in a separate object files
    echo We will have ~12 MiB library...

    if not exist obj-%dmdVer% mkdir obj-%dmdVer%
    if exist args del args
    for /r %%f in (*.d) do (
        set file=%%f
        set file=!file:%cd%\=!
        set objFile=obj-%dmdVer%\!file:.d=.obj!
        echo "!objFile!" >> args
        if not exist !objFile! echo Compiling !file!... && dmd -c -of"!objFile!" "!file!"
    )

    dmd @args -lib -ofgtk-sep-obj-%dmdVer%.lib
    if errorlevel 1 goto Error
    del args
)

if not exist main-sep-obj-%dmdVer%.exe (
    echo Building main.d
    echo We will have ~2 MiB executable...
    dmd -map ..\main.d gtk-sep-obj-%dmdVer%.lib -ofmain-sep-obj-%dmdVer%.exe
    if errorlevel 1 goto Error
)

echo Testing main-sep-obj-%dmdVer%.exe...
main-one-obj-%dmdVer%.exe
if errorlevel 1 goto Error

if not exist zeroCount-%dmdVer%.exe (
    echo Building zeroCount util...
    dmd ..\zeroCount.d && del zeroCount.obj
    if errorlevel 1 goto Error
)

echo --------------------------------------------------
echo Now let's calculate zero bytes counts:
echo --------------------------------------------------
zeroCount gtk-one-obj-%dmdVer%.lib gtk-sep-obj-%dmdVer%.lib main-one-obj-%dmdVer%.exe main-sep-obj-%dmdVer%.exe

echo Done.
pause
goto Exit
:Error
echo Failed.
pause
:Exit

cd ..

:ExitNoCd
