@echo off
@rem * compile translations
@rem * bind them to the executable
@rem This script is called from postbuild.cmd

echo * %0
@echo * Params: %*
echo * running in %CD%

if not "%2"=="" goto GetProjectDir
rem determine the project directory that is the one in which
rem the directory buildtools is located
call :GetDrvAndDir %0
set MyDir=%result%
call :GetDrvAndDir %MyDir%
set ProjectDir=%result%
goto AfterGetProjectDir

@rem The project directory is in second parameter 
:GetProjectDir
set ProjectDir=%~dp1
set ProjectDir=%ProjectDir:~0,-1%

:AfterGetProjectDir
set translationsDir=%ProjectDir%\translations
set localeDir=%ProjectDir%\locale

if exist %translationsDir% goto locexists
if exist %localeDir% goto locexists
echo Hint: directory locale/translation does not exist, no translation added
goto :eof

:locexists

@call :HandLng de
@call :HandLng fr
@call :HandLng en
@call :HandLng pt

@echo bind all translations to the executable
@rem echo %~dp0assemble --localebase="%ProjectDir%" --dxgettext "%1.exe"
%~dp0assemble --localebase="%ProjectDir%" --dxgettext "%1.exe"
echo off

echo * %0 exiting
goto :eof

@rem subroutine for handling a language
:HandLng

set LNG=%1
@echo ** handling language %LNG% **
if exist %translationsDir%\%LNG%_off.txt goto :eof
if exist %localeDir%\%LNG%\lc_messages goto lcmsgexists
echo *** Hint: subdir %localeDir%\%LNG%\lc_messages does not exist skipping language %LNG% (to turn this message of create %translationsDir%\%LNG%_off.txt) ***
goto :eof
:lcmsgexists

if not exist %ProjectDir%\libs goto nolibs
set LibsDir=%ProjectDir%\libs
goto libsset

:nolibs
if not exist %ProjectDir%\..\libs goto libserror
set LibsDir=%ProjectDir%\..\libs
goto libsset

:libserror
echo libs directory not found, cannot add translations
goto :eof

:libsset

if not exist %LibsDir%\tdbf\translations\%LNG%\tdbf.po goto notdbf
@echo compile %LNG% tdbf.po
%~dp0\msgfmt %LibsDir%\tdbf\translations\%LNG%\tdbf.po -o %localeDir%\%LNG%\lc_messages\tdbf.mo
:notdbf 

if not exist %LibsDir%\dspack\translations\%LNG%\dspack.po goto nodspack
@echo compile %LNG% dspack.po
%~dp0\msgfmt %LibsDir%\dspack\translations\%LNG%\dspack.po -o %localeDir%\%LNG%\lc_messages\dspack.mo
:nodspack

if not exist %LibsDir%\dzlib\translations\%LNG%\dzlib.po goto nodzlib
@echo compile %LNG% dzlib.po
%~dp0\msgfmt %LibsDir%\dzlib\translations\%LNG%\dzlib.po -o %localeDir%\%LNG%\lc_messages\dzlib.mo
:nodzlib

if not exist %LibsDir%\sigunits\translations\%LNG%\sigunits.po goto nosigunits
@echo compile %LNG% sigunits.po
%~dp0\msgfmt %LibsDir%\sigunits\translations\%LNG%\sigunits.po -o %localeDir%\%LNG%\lc_messages\sigunits.mo
:nosigunits

if not exist %LibsDir%\dxgettext\translations\%LNG%\delphi2007.po goto nodelphi
@echo compile %LNG% delphi2007.po
%~dp0\msgfmt %LibsDir%\dxgettext\translations\%LNG%\delphi2007.po -o %localeDir%\%LNG%\lc_messages\delphi2007.mo
:nodelphi

if not exist %LibsDir%\comport\locale\%LNG%\LC_MESSAGES\cport.po goto nocomport
@echo compile %LNG% cport.po
%~dp0\msgfmt %LibsDir%\comport\locale\%LNG%\LC_MESSAGES\cport.po -o %localeDir%\%LNG%\lc_messages\cport.mo
:nocomport

@echo compile %LNG% default.po
call :SetTrDir
%~dp0\msgfmt %TranslDir%\default.po -o %localeDir%\%LNG%\lc_messages\default.mo

goto :eof

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


:GetDrvAndDir
rem extract directory
set directory=%~dp1
rem remove backslash (=last character)
set result=%directory:~0,-1%
goto :eof
