@echo off
REM Windows Makefile, by Neil Mitchell
REM <http://www.cs.york.ac.uk/~ndm/>

set CALL=call %0 JUMP
set DEFINE= -DFILEVERSION=\"2.04\" -DVERSION=\"2.06\"
set CPPHS=-cpp -pgmPcpphs -optP--cpp

set MKDIRS=%CALL% mkdirs
set COMPILE_C=%CALL% compile_c
set COMPILE_HS=%CALL% compile_hs

if "%1" == "JUMP" goto jump

set ONLY=
if not "%1" == "" set ONLY=%1


REM Create Directories

%MKDIRS% obj obj\lib obj\trans
%MKDIRS% windows windows\c

REM HatTrans
if not "%ONLY%HatTrans" == "HatTrans%ONLY%" goto skip_hat_trans
ghc --make src\trans\HatTrans.hs -isrc\trans;src\compiler98 %CPPHS% -o windows\hat-trans.exe -odir obj\trans -hidir obj\trans
:skip_hat_trans

REM C
copy include\*.h windows\c
copy src\lib\*.h windows\c
copy src\lib\*.c windows\c
copy src\tools\ntohl.c windows\c
copy src\tools\hatgui.glade windows

if not "%ONLY%" == "" goto skip_c
gcc -c windows\c\hat-c.c %DEFINE% -o windows\c\hat-c.o
gcc -c windows\c\ntohl.c %DEFINE% -o windows\c\ntohl.o
:skip_c

REM Hs - todo
REM Hx - todo

REM Compile all the Hat tools
%COMPILE_C% artutils pathutils observeutils finitemap ntohl hat-names nontermutils detectutils parentset
%COMPILE_HS% HatMake hat-make

if not "%ONLY%HatCheck" == "HatCheck%ONLY%" goto skip_hat_check
gcc src\tools\hat-check.c -Iinclude %DEFINE% -o windows\hat-check.exe obj\lib\ntohl.o
:skip_hat_check

%COMPILE_HS% HatCoverText hat-cover
%COMPILE_HS% HatStackText hat-stack
%COMPILE_HS% HatObserve hat-observe
%COMPILE_HS% HatNonTerm hat-nonterm
%COMPILE_HS% BlackHat black-hat
%COMPILE_HS% HatDelta hat-delta
%COMPILE_HS% HatGui hat-gui gtk
REM %COMPILE_HS% HatTrail hat-trail # does not work because of escape codes
REM %COMPILE_HS% HatAnim hat-anim # does not work because of escape codes
REM %COMPILE_HS% HatDetect hat-detect # requires xterm to be a program
REM %COMPILE_HS% HatExplore hat-explore # does not work because of escape codes

goto exit


REM ROUTINES

:jump
set jump=%2
shift
shift
goto %jump%


:mkdirs
if not exist %1 mkdir %1
shift
if not "%1" == "" goto mkdirs
goto exit


:compile_c
if not "%ONLY%" == "" goto exit
gcc -c src\tools\%1.c -o obj\lib\%1.o -Iinclude %DEFINE%
shift
if not "%1" == "" goto compile_c
goto exit


:compile_hs
if not "%ONLY%%2" == "%2%ONLY%" goto exit
if not exist obj\%2 mkdir obj\%2
set PKG=
if not "%3" == "" set PKG=-package %3
ghc --make src\tools\%1 -o windows\%2.exe -isrc\tools %CPPHS% -ffi -fglasgow-exts obj\lib\*.o -odir obj\%2 -hidir obj\%2 %PKG%
goto exit


:exit
