rem @echo off
rem

rem  Check that all parameters are supplied
rem
if /I "%1" EQU "" goto :USAGE
if /I "%2" EQU "" goto :USAGE
if /I "%3" EQU "" goto :USAGE
if /I "%4" EQU "" goto :USAGE
if /I "%5" EQU "" goto :USAGE

REM for /F %%i in ('cd') do set DOC1HOSTDIR=%%i
set DOC1HOSTDIR=%1
if exist %DOC1HOSTDIR%\generate\doc1gen.exe goto doc1found
echo DOC1HOSTDIR is set to %DOC1HOSTDIR%.
echo Cannot find %DOC1HOSTDIR%\generate\doc1gen.exe 
echo This script must be run in the DOC1 HOST directory.
exit 1

:doc1found
set DOC1TMPLDIR=%2
set INPUTFILE=%3
set OUTPUTFILE=%4
set INIFILE=%5
if exist %OUTPUTFILE% del /q /f %OUTPUTFILE%

rem
rem Set some environment variables
rem
for /F %%i in ('echo %INPUTFILE%') do set INPUTPATH=%%~dpi
for /F %%i in ('echo %INPUTFILE%') do set INPUTBASENAME=%%~ni
for /F %%i in ('echo %OUTPUTFILE%') do set OUTPUTPATH=%%~dpi
for /F %%i in ('echo %OUTPUTFILE%') do set OUTPUTBASENAME=%%~ni
if exist %OUTPUTPATH% goto assignlog
rem Can only echo because logfile output directory does not exist.
echo Error %0: Directory %OUTPUTPATH% does not exist.
exit 1

rem
rem Assign log file name
rem
:assignlog
set LOGFILE=%OUTPUTPATH%%OUTPUTBASENAME%.log
if exist %LOGFILE% del /q /f %LOGFILE%

echo Starting %0 >> %LOGFILE%
echo Directory is >> %LOGFILE%
cd >> %LOGFILE%
date /t >> %LOGFILE%
time /t >> %LOGFILE%

rem
rem  Ensure that the print extract file is present
rem
if exist %INPUTFILE% goto :removeoldfiles
echo Error %0: Input file %INPUTFILE% does not exist.
echo Error %0: Input file %INPUTFILE% does not exist. >> %LOGFILE%
exit 1

rem
rem  Remove old files
rem
:removeoldfiles
if exist %OUTPUTFILE% del /q /f %OUTPUTFILE%

echo time /t again >> %LOGFILE%

if '%INIFILE%' == '_splfo.ini'  goto :FOP

if '%INIFILE%' == '_splltr.ini' goto :LTR   
             
if '%INIFILE%' == '_splqte.ini' goto :QTE  

if '%INIFILE%' == '_splstmt.ini' goto :STMT  

goto :END

:FOP
call %DOC1TMPLDIR%\splfop_s5_v3_main_batch_pdf.bat
echo time /t back >> %LOGFILE%
goto :END

:LTR
call %DOC1TMPLDIR%\splletr_s5_v3_main_batch_pdf.bat
echo time /t back >> %LOGFILE%
goto :END

:QTE
call %DOC1TMPLDIR%\splquote_s5_v3_main_batch_pdf.bat
echo time /t back >> %LOGFILE%
goto :END

:STMT
call %DOC1TMPLDIR%\splstmt_s5_v3_main_batch_pdf.bat
echo time /t back >> %LOGFILE%
goto :END

:USAGE
echo 
echo Usage: %0 arg1 arg2 arg3 arg4 arg5
echo        where arg1 is the DOC1 Host Directory
echo              arg2 is the DOC1 Template Location
echo              arg3 is the fully qualified extract filename
echo              arg4 is the fully qualified output pdf filename
echo              arg5 is the DOC1 ini filename
echo 
:END
