#!/usr/bin/ksh
usage() {
	echo 
	echo Usage: $0 arg1 arg2 arg3 arg4 arg5
	echo where
	echo	arg1 is the Documaker Home Directory
	echo 	arg2 is the Documaker Template Location
	echo	arg3 is the fully qualified extract filename
	echo  arg4 is the fully qualified output pdf filename
	echo  arg5 is the document type - FIELDORDER/LETTER/QUOTE/STATEMENET
   	echo 
	exit 1
}

# set -xv 
# 
#   Check that all parameters are supplied
# 
# if [ "$1" == "" -o "$2" == "" -o "$3" == "" -o "$4" == "" ]
if [ "$1" = "" ]||[ "$2" = "" ]||[ "$3" = "" ]||[ "$4" = "" ]
then
	usage
fi

# if /I "$2" EQU "" usage
# if /I "$3" EQU "" usage
# if /I "$4" EQU "" usage

export DOCHOSTDIR=$1
export DOCTMPLDIR=$2
export OUTPUTFILEORIG=$4
export OUTPUTPATHORIG=`dirname $OUTPUTFILEORIG`/
export PATH=$DOCHOSTDIR/FAP/bin:$DOCHOSTDIR/CCB:$PATH
export LD_LIBRARY_PATH=$DOCHOSTDIR/FAP/bin:$LD_LIBRARY_PATH
export SHLIB_PATH=$DOCHOSTDIR/FAP/bin:$SHLIB_PATH
export LIBPATH=$DOCHOSTDIR/FAP/bin:$LIBPATH

if [ ! -f $DOCHOSTDIR/FAP/bin/gendata  ] 
then
	echo DOCHOSTDIR is export to $DOCHOSTDIR.
	echo Cannot find $DOCHOSTDIR/FAP/bin/gendata
	echo This script must be run in the DOCUMAKER HOME directory.
	exit 1
fi

# 
#  Set some environment variables
# 
export INPUTBASENAMEORIG=`basename $3 .DAT` 
export OUTPUTBASENAMEORIG=`basename $4 .PDF`
export INPUTBASENAME=`echo $INPUTBASENAMEORIG|tr '[A-Z]' '[a-z]'`
export OUTPUTBASENAME=`echo $OUTPUTBASENAMEORIG|tr '[A-Z]' '[a-z]'`
export INPUTFILE=${DOCHOSTDIR}/CCB/data/$INPUTBASENAME.dat
export OUTPUTFILE=${DOCHOSTDIR}/CCB/data/$OUTPUTBASENAME.pdf
export INPUTFILEOPTION=data/$INPUTBASENAME.dat
export OUTPUTFILEOPTION=data/$OUTPUTBASENAME.pdf


# 
#  Copy the original input file to $DOCHOSTDIR/CCB/data/$INPUTBASENAME.dat
#  as Documaker on Unix reads the parameters from ini files in lower case
#  and hence we cannot have a complete path with directory names not in lower
#  case specified in the input file
# 
cat "$3" > $INPUTFILE

if [ -f $OUTPUTFILE ]
then
        rm $OUTPUTFILE
fi

if [ ! -d $OUTPUTPATHORIG ]
then
	#  Can only echo because logfile output directory does not exist.
	echo Error $0: Directory $OUTPUTPATHORIG does not exist.
	exit 1
fi

# 
#  Assign log file name
# 
export LOGFILE=$OUTPUTPATHORIG$OUTPUTBASENAMEORIG.log
if [ -f $LOGFILE ]
then
	rm $LOGFILE
fi

echo Starting $0 >> $LOGFILE
echo Directory is `pwd` >> $LOGFILE
date >> $LOGFILE
echo DOCHOSTDIR $DOCHOSTDIR >> $LOGFILE
echo DOCTMPLDIR $DOCTMPLDIR >> $LOGFILE
echo Input File $3 >> $LOGFILE
echo Output File $OUTPUTFILEORIG >> $LOGFILE
echo Intermediate Input File $INPUTFILE >> $LOGFILE
echo Intermediate Output File $OUTPUTFILE >> $LOGFILE

# 
#   Ensure that the extract file is present
# 
if [ ! -f $INPUTFILE ]
then 
	echo Error $0: Input file $INPUTFILE does not exist.
	echo Error $0: Input file $INPUTFILE does not exist. >> $LOGFILE
	exit 1
fi

INIFILE=$5


if [ "$INIFILE" == "FIELDORDER" ]
  then
    echo "Field Order"
    . $DOCHOSTDIR/CCB/forun.sh 
fi

if [ "$INIFILE" == "QUOTE" ]
  then
    echo "Quote Print"
    . $DOCHOSTDIR/CCB/qterun.sh 
fi

if [ "$INIFILE" == "LETTER" ]
  then
    echo "Letter Print"
    . $DOCHOSTDIR/CCB/ltrrun.sh 
fi
if [ "$INIFILE" == "STATEMENT" ]
  then
    echo "Statement Print"
    . $DOCHOSTDIR/CCB/stmrun.sh 
fi

date >> $LOGFILE
echo Ending $0 >> $LOGFILE

