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

echo 'step 1'

REM for /F %%i in ('cd') do set DOCHOSTDIR=%%i
set DOCHOSTDIR=%1
if exist %DOCHOSTDIR%\FAP\DLL\gendaw32.exe goto docfound
echo DOCHOSTDIR is set to %DOCHOSTDIR%.
echo Cannot find %DOCHOSTDIR%\FAP\DLL\gendaw32.exe 
echo This script must be run in the DOC HOST directory.
exit 1

:docfound
set DOCTMPLDIR=%2
set INPUTFILE=%3
set OUTPUTFILE=%4
set INPUTFILEOPTION=%3
set OUTPUTFILEOPTION=%4
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%
echo Input File %3 >> %LOGFILE%
echo Output File %4 >> %LOGFILE%
cd >> %LOGFILE%
date /t >> %LOGFILE%
time /t >> %LOGFILE%

rem
rem  Ensure that the bill 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%

call %DOCTMPLDIR%\BillRun.BAT

echo time /t back >> %LOGFILE%

goto :END

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