@echo off
if ""=="%1" goto CONFIG
goto HANDLEDIR

REM -----------------------------------------------------
:CONFIG
REM -----------------------------------------------------

rem * CONFIGURATION BEGIN
rem * configuration settings, adjust as necessary to meet your system setup
set SVNWORKDIR=
set MY_THRIFT_FILES=
set BIN=C:\MSys10\local\bin
set MINGW_BIN=C:\MinGW\bin
set DCC=
set SUBDIR=gen-delphi
rem * CONFIGURATION END


REM -----------------------------------------------------
:START
REM -----------------------------------------------------

rem * configured?
if "%SVNWORKDIR%"=="" goto CONFIG_ERROR

rem * try to find dcc32.exe
echo Looking for dcc32.exe ...
if not exist "%DCC%" set DCC=%ProgramFiles%\Embarcadero\RAD Studio\8.0\bin\dcc32.exe
if not exist "%DCC%" set DCC=%ProgramFiles(x86)%\Embarcadero\RAD Studio\8.0\bin\dcc32.exe
if not exist "%DCC%" goto CONFIG_ERROR
echo Found %DCC%
echo.

rem * some helpers
set PATH=%BIN%;%MINGW_BIN%;%PATH%
set TARGET=%SVNWORKDIR%\..\thrift-testing
set SOURCE=%SVNWORKDIR%
set TESTAPP=TestProject
set UNITSEARCH=%SOURCE%\lib\pas\src;%SOURCE%\lib\delphi\src
set OUTDCU="%TARGET%\dcu"
set LOGFILE=%TARGET%\%SUBDIR%\codegen.log

rem * create and/or empty target dirs
if not exist "%TARGET%"           md "%TARGET%"
if not exist "%TARGET%\%SUBDIR%"  md "%TARGET%\%SUBDIR%"
if not exist "%OUTDCU%" 	      md "%OUTDCU%"
if exist "%TARGET%\*.thrift"      del "%TARGET%\*.thrift"       /Q
if exist "%TARGET%\%SUBDIR%\*.*"  del "%TARGET%\%SUBDIR%\*.*"   /Q
if exist "%OUTDCU%\*.*"           del "%OUTDCU%\*.*"            /Q

rem * recurse through thrift WC and "my thrift files" folder
rem * copies all .thrift files into thrift-testing
call %0 %SOURCE%
if not "%MY_THRIFT_FILES%"=="" call %0 %MY_THRIFT_FILES%

rem * compile all thrift files, generate PAS and C++ code
echo.
echo Generating code, please wait ...
cd "%TARGET%"
for %%a in (*.thrift) do "%BIN%\thrift.exe" -v --gen delphi:ansistr_binary,register_types "%%a" 2>> "%LOGFILE%"
REM * for %%a in (*.thrift) do "%BIN%\thrift.exe" -v --gen cpp "%%a" >> NUL:
cmd /c start notepad "%LOGFILE%"
cd ..

rem * check for special Delphi testcases being processed
if not exist "%TARGET%\%SUBDIR%\ReservedKeywords.pas" goto TESTCASE_MISSING


rem * generate a minimal DPR file that uses all generated pascal units
cd "%TARGET%\%SUBDIR%\"
if exist inherited.*  ren inherited.*  _inherited.*
echo program %TESTAPP%;    					> %TESTAPP%.dpr
echo {$APPTYPE CONSOLE}   					>> %TESTAPP%.dpr
echo.    									>> %TESTAPP%.dpr
echo uses 									>> %TESTAPP%.dpr
for %%a in (*.pas) do echo   %%~na,			>> %TESTAPP%.dpr
echo   Windows, Classes, SysUtils;			>> %TESTAPP%.dpr
echo.    									>> %TESTAPP%.dpr
echo begin 									>> %TESTAPP%.dpr
echo   Writeln('Successfully compiled!');	>> %TESTAPP%.dpr
echo   Writeln('List of units:');	>> %TESTAPP%.dpr
for %%a in (*.pas) do echo   Write('%%~na':30,'':10);  >> %TESTAPP%.dpr
echo   Writeln;	>> %TESTAPP%.dpr
echo end. 									>> %TESTAPP%.dpr
echo.    									>> %TESTAPP%.dpr
cd ..\..

rem * try to compile the DPR
rem * this should not throw any errors, warnings or hints
"%DCC%"  -B "%TARGET%\%SUBDIR%\%TESTAPP%" -U"%UNITSEARCH%" -I"%UNITSEARCH%" -N"%OUTDCU%" -E"%TARGET%\%SUBDIR%" 
dir "%TARGET%\%SUBDIR%\%TESTAPP%.exe"
if not exist "%TARGET%\%SUBDIR%\%TESTAPP%.exe"  goto CODEGEN_FAILED
echo.
echo -----------------------------------------------------------------
echo The compiled program is now executed. If it hangs or crashes, we
echo have a serious problem with the generated code. Expected output 
echo is "Successfully compiled:" followed by a list of generated units.
echo -----------------------------------------------------------------
"%TARGET%\%SUBDIR%\%TESTAPP%.exe"
echo -----------------------------------------------------------------
echo.
pause
GOTO EOF

REM -----------------------------------------------------
:DXE_NOT_FOUND
REM -----------------------------------------------------
echo Delphi Compiler (dcc32.exe) not found. 
echo Please check the "DCC" setting in this batch.
echo.
cmd /c start notepad README.TXT
cmd /c start notepad %0
pause
GOTO EOF


REM -----------------------------------------------------
:CONFIG_ERROR
REM -----------------------------------------------------
echo Missing, incomplete or wrong configuration settings!
cmd /c start notepad README.TXT
cmd /c start notepad %0
pause
GOTO EOF


REM -----------------------------------------------------
:TESTCASE_MISSING
REM -----------------------------------------------------
echo Missing an expected Delphi testcase!
pause
GOTO EOF


REM -----------------------------------------------------
:CODEGEN_FAILED
REM -----------------------------------------------------
echo Code generation FAILED!
pause
GOTO EOF


REM -----------------------------------------------------
:HANDLEDIR
REM -----------------------------------------------------
REM echo %1
for /D %%a in (%1\*) do call %0 %%a
if exist "%1\*.thrift"   copy /b "%1\*.thrift" "%TARGET%\*.*"
GOTO EOF


REM -----------------------------------------------------
:EOF
REM -----------------------------------------------------
