#!/usr/bin/ksh
export PATH=$PATH:$DOC1HOSTDIR/generate:$DOC1HOSTDIR/AppFiles
usage() {
	echo 
	echo Usage: $0 arg1 arg2 arg3 arg4
	echo where
	echo	arg1 is the DOC1 Home 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 
	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 DOC1HOSTDIR=$1
export LD_LIBRARY_PATH=$DOC1HOSTDIR/generate:$LD_LIBRARY_PATH
export SHLIB_PATH=$DOC1HOSTDIR/generate:$SHLIB_PATH
export LIBPATH=$DOC1HOSTDIR/generate:$LIBPATH


if [ ! -f $DOC1HOSTDIR/generate/doc1gen ] 
then
	echo DOC1HOSTDIR is export to $DOC1HOSTDIR.
	echo Cannot find $DOC1HOSTDIR/generate/doc1emfe
	echo This script must be run in the DOC1 HOME directory.
	exit 1
fi

export DOC1TMPLDIR=$2
export INPUTFILE=$3
export OUTPUTFILE=$4
if [ -f $OUTPUTFILE ]
then
	rm $OUTPUTFILE
fi


# 
#  Set some environment variables
# 
export INPUTPATH=`dirname $INPUTFILE`
export INPUTBASENAME=`basename $INPUTFILE .dat` 
export OUTPUTPATH=`dirname $OUTPUTFILE`
if [ ! -d $OUTPUTPATH ]
then
	#  Can only echo because logfile output directory does not exist.
	echo Error $0: Directory $OUTPUTPATH does not exist.
	exit 1
fi

# 
#  Assign log file name
# 
export LOGFILE=$INPUTPATH/$INPUTBASENAME.log
if [ -f $LOGFILE ]
then
	rm $LOGFILE
fi
echo Starting $0 >> $LOGFILE
echo Directory is `pwd` >> $LOGFILE
date >> $LOGFILE

# 
#   Ensure that the bill print 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

# 
#   Remove old files
# 
if [ -f $OUTPUTFILE ]
then
	rm $OUTPUTFILE
fi

# 
#  Assign filenames
# 
export SVDFILE=$INPUTPATH/$INPUTBASENAME.svd
if [ -f $SVDFILE ]
then
	rm $SVDFILE
fi
export JRNFILE=$INPUTPATH/$INPUTBASENAME.jrn
if [ -f $JRNFILE ]
then
	rm $JRNFILE
fi

# 
#   Sort bill print extract file
# 
export LC_COLLATE="C"
sort -k 1.5 <$INPUTFILE >$SVDFILE

# 
#   Prepare and run the DOC1 Application which generates a pdf file.
# 
if [ ! -d  $DOC1TMPLDIR ]
then
	echo Error $0: Directory $DOC1TMPLDIR does not exist.
	echo Error $0: Directory $DOC1TMPLDIR does not exist. >> $LOGFILE
exit 1
fi

cd $DOC1TMPLDIR
export MYINI=$INPUTPATH/$INPUTBASENAME.ini

if [ ! -f splbill_s5_v3_doc1gen_pdf.hip ]
then
	echo Error $0: File $DOC1TMPLDIR/splbill_s5_v3_doc1gen_pdf.hip does not exist.
	echo Error $0: File $DOC1TMPLDIR/splbill_s5_v3_doc1gen_pdf.hip does not exist. >> $LOGFILE
	exit 1
fi

set |grep SYSUSER
set |grep 592211693315

acct=`echo $INPUTBASENAME|awk -F. '{printf($2)}'`
user=`echo $INPUTBASENAME|awk -F. '{printf($1)}'`
INIFILE=$5


if [ "$INIFILE" == "_splfo.ini" ]
  then
    echo "Field Order"
    export template=splfop
    s5_v3_main_batch_pdf.sh $acct $DOC1HOSTDIR/AppFiles $user $DOC1HOSTDIR/generate  $SPLAPP $SPLEBASE/splapp/billView
fi

if [ "$INIFILE" == "_splqte.ini" ]
  then
    echo "Quote Print"
    export template=splquote
    s5_v3_main_batch_pdf.sh $acct $DOC1HOSTDIR/AppFiles $user $DOC1HOSTDIR/generate  $SPLAPP $SPLEBASE/splapp/billView
fi
if [ "$INIFILE" == "_splltr.ini" ]
  then
   export template=splletr
    echo "Letter Print"
    s5_v3_main_batch_pdf.sh $acct $DOC1HOSTDIR/AppFiles $user $DOC1HOSTDIR/generate  $SPLAPP $SPLEBASE/splapp/billView
fi
if [ "$INIFILE" == "_splstmt.ini" ]
  then
    echo "Statement Print"
    export template=splstmt
    s5_v3_main_batch_pdf.sh $acct $DOC1HOSTDIR/AppFiles $user $DOC1HOSTDIR/generate  $SPLAPP $SPLEBASE/splapp/billView
fi



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

