@echo off
@rem This batch will
@rem * call dxgettext to extract all strings to translate
@rem * compile all .po files and copy them to locale\...
@rem * do the same for the sigunits, dzlib, CPort, tdbf and delphi2007 files in libs

echo * %0
echo * running in %CD%

set MASKS=*.pas *.dfm *.inc *.tpl *.dpr
set POFILES=
set OUTDIR=.

set BASE=.
%~dp0\dxgettext %MASKS% --no-line-numbers -r --no-wrap -b %BASE%\src -o %BASE%

if not exist %BASE%\ignore.po goto noIgnore
%~dp0\msgremove --no-wrap %BASE%\default.po -i %BASE%\ignore.po -o %BASE%\filtered.po
move %BASE%\filtered.po %BASE%\default.po
:noIgnore

set POFILES=%POFILES% %BASE%\default.po
%~dp0\msgcat --no-wrap -o default.po %POFILES%

if "%SKIPDE%"=="1" goto skipde
@rem German:
set LNG=de
call :HandLng
:skipde

if "%SKIPFR%"=="1" goto skipfr
@rem French:
set LNG=fr
call :HandLng
:skipfr

if "%SKIPPT%"=="1" goto skippt
@rem Portugese:
set LNG=pt
call :HandLng
:skippt

if "%SKIPEN%"=="1" goto skipen
@rem English:
set LNG=en
call :HandLng
:skipen

echo * %0 exiting
goto :eof

@rem subroutine to determine the directory containing the translations
:SetTrDir
if exist translations goto :usetrans
set TranslDir=locale\%LNG%\lc_messages
goto :eof
:usetrans
set TranslDir=translations\%LNG%
goto :eof

@rem subroutine for handling a language
:HandLng
@echo ** handling language %LNG% **

@rem merge translations
call :SetTrDir
if exist %TranslDir%\default.po goto PoExists
echo Hint: %LNG% skipped, %TranslDir%\default.po does not exist.
goto :eof

:PoExists
%~dp0\msgmerge --no-wrap --update %TranslDir%\default.po default.po

@rem compile
%~dp0\msgfmt %TranslDir%\default.po --output-file=%OUTDIR%\locale\%LNG%\lc_messages\default.mo

@rem add Delphi translations
if not exist libs\dxgettext\translations\%LNG%\delphi2007.po goto noDelphi
%~dp0\msgfmt libs\dxgettext\translations\%LNG%\delphi2007.po --output-file=%OUTDIR%\locale\%LNG%\lc_messages\delphi2007.mo
:noDelphi

@rem add sigunits translations
if not exist libs\sigunits\translations\%LNG%\sigunits.po goto noSigUnits
echo Adding sigunits translations
%~dp0\msgfmt libs\sigunits\translations\%LNG%\sigunits.po --output-file=%OUTDIR%\locale\%LNG%\lc_messages\sigunits.mo
:noSigUnits

@rem add dzlib translations
if not exist libs\dzlib\translations\%LNG%\dzlib.po goto noDzLib
echo Adding dzlib translations
%~dp0\msgfmt libs\dzlib\translations\%LNG%\dzlib.po --output-file=%OUTDIR%\locale\%LNG%\lc_messages\dzlib.mo
:noDzLib

@rem add CPort translations
if not exist libs\comport\locale\%LNG%\LC_MESSAGES\cport.po goto noCPort
echo Adding CPort translations
%~dp0\msgfmt libs\comport\locale\%LNG%\LC_MESSAGES\cport.po --output-file=%OUTDIR%\locale\%LNG%\lc_messages\cport.mo
:noCPort

@rem add tdbf translations
if not exist libs\tdbf\locale\%LNG%\LC_MESSAGES\tdbf.po goto notdbf
echo Adding tdbf translations
%~dp0\msgfmt libs\tdbf\locale\%LNG%\LC_MESSAGES\tdbf.po --output-file=%OUTDIR%\locale\%LNG%\lc_messages\tdbf.mo
:notdbf

goto :eof
