#!/usr/bin/ksh 

# Delete previous "junk" files
rm -f $PRODPATH/junk.log

# ***********************************************************************************
#                                     W A R N I N G                                 *
#                                     W A R N I N G                                 *
#                                     W A R N I N G                                 *
# ==================================================================================*
#  DO NOT EXECUTE THIS FILE DIRECTLY FROM THE UNIX COMMAND LINE.                    *
#                                                                                   *
#  Instead, this bat file is meant to be executed by being called from another      *
#  Unix shell script.                                                               *
# ==================================================================================*

# ***********************************************************************************
#  Platform:      Unix - AIX                                                        *
#  Printstream:   Postscript or PDF                                                 *
#  Installation:  Base Template Package                                             *
# ==================================================================================*
#  Command File called to execute integrated Sort, DOC1 Generate and PCE,           *
#  and Print Files for SPL Field Order Print                                        *
# ==================================================================================*
#  This Command File should NOT be run from the command line; instead, a Master     *
#  Command file is run on the command line; the Master Command File will call       *
#  this command file.                                                               *
# ==================================================================================*
#  The Master Command File must set the following symbols prior to calling this     *
#  command file:                                                                    *
#                                                                                   *
#    G E N E R A L   S Y M B O L S                                                  *
#    =============================                                                  *
#      LOGFILE                                                                      *
#              - Name of a log file to be created by this process                   *
#              - Previous version of file will be replaced                          *
#              - File will be written to $PRODPATH directory                        *
#              - Sample coding:  LOGFILE=$templatelog.log                                *
#      SPLFOP_CALLED_SWITCH                                                         *
#              - Must be set to "Y"                                                 *
#              - This process will reset to "N"                                     *
#              - Used by to insure that this process was invoked by a Master        *
#                Command File                                                       *
#      PRODPATH                                                                     *
#              - Names directory for output files from Sort and DOC1.               *
#              - Sample coding: PRODPATH=/DOC1/Prodfiles                            *
#                                                                                   *
#    D O C 1   S Y M B O L S                                                        *
#    =======================                                                        *
#      D1HOST                                                                       *
#              - Names root directory where the DOC1 Generate was installed         *
#              - Typical value:                                                     *
#                D1HOST=/Group1/Doc1 Suite                                          *
#      D1APPATH                                                                     *
#              - Names directory containing HIP, INI, CTL, SH, ETS                  *
#              - Sample coding:                                                     *
#                D1APPATH=/Group1/DOC1 Suite/resource/fop                           *
#      GEN_OPS                                                                      *
#              - Names DOC1 Generate Initialization file for Format Field Orders    *
#              - Sample value: GEN_OPS=spl$templates5_v3_gen.ops                         *
#              - File must be present in the $D1APPATH directory                    *
#      GEN_HIP                                                                      *
#              - Name of DOC1 Generate Application Rules (.HIP)                     *
#              - Sample value: GEN_HIP=spl$templates5_v3_doc1gen.hip                     *
#              - File must be present in the $D1APPATH directory                    *
#      GEN_ETS                                                                      *
#              - Names Look-Up file                                                 *
#              - Sample value: GEN_ETS=spl$templates5_v3_lookup.ets                      *
#              - File must be present in the $D1APPATH directory                    *
#      FOP_PCE_INI                                                                  *
#              - Names DOC1PCE Initialization file                                  *
#              - Sample value: FOP_PCE_INI=spl$templates5_v3_pce.ini                     *
#              - File must be present in the $D1APPATH directory                    *
#      FOP_PCE_CTL                                                                  *
#              - Names DOC1PCE Rules file for Post Process step                     *
#              - Sample value: POST_PCE_CTL=spl$templates5_v3.ctl                        *
#              - File must be present in the $D1APPATH directory                    *
#      TIMESTAMP                                                                    * 
#              - Time of day and date to appeneded to the final print file          * 
#              - Sample coding :                                                    * 
#                 PRTOUT_01=print_group_01_final_print_$TIMESTAMP.$PRINT;           * 
#                                                                                   *
#    S P L   S Y M B O L S                                                          *
#    =====================                                                          *
#      XTRCTPTH                                                                     *
#              - Names directory containing SPL Field Order Print Extract           *
#              - Sample coding: XTRCTPTH=/SPL/data                                  *
#      SPL_UNSORTED_EXTRACT                                                         *
#              - Names SPL Field Order Print Extract                                *
#              - Typical coding SPL_UNSORTED_EXTRACT=fopxtr01.dat                   *
#                                                                                   *
#    P R I N T   S Y M B O L S                                                      *
#    =========================                                                      *
#      PRINTFILES                                                                   *
#              - May be:  Y or N                                                    *
#              - "N" prevents files from being sent to PADDR printer                *
#      PSTREAM                                                                      *
#              - May be PDF or PostScript                                           *
#              - Controls type of print description language for output             *
#                                                                                   *
#    D E L E T E    S Y M B O L S                                                   *
#    ============================                                                   *
#      DELETEFILES                                                                  *
#              - May be:  Y or N                                                    *
#              - "N" prevents work files from being deleted                         *
# ==================================================================================*
#    Step 01:  Sort Field Order extract to re-arrange records within document       *
#    Step 02:  Run DOC1GEN to format documents                                      *
# ==================================================================================*

# ***********************************************************************************
#    Step 01:  Sort SPL extract to re-arrange records within document               *
# ==================================================================================*
#  The SPL Field Order Print Extract must be sorted to re-arrange records           *
#  within each Field Order.                                                         *
# ==================================================================================*
#   Input Files                                                                     *
#      SPL_UNSORTED_EXTRACT                                                         *
#              - Name of the Field Order data extracted from SPL                    *
#              - This file must be in the XTRCTPTH directory prior to running       *
#                this .SH process                                                   *
#   Output Files                                                                    *
#      SPL_SORTED_EXTRACT                                                           *
#              - Name of Sorted SPL Extract containing Field Order data             *
#              - This file will be written to the PRODPATH directory                *
#              - This .SH process will delete previous versions of this file        *
#   Configuration Files                                                             *
#      -- none --                                                                   *
# ***********************************************************************************
SPL_SORTED_EXTRACT=fop-sorted.svd;                                                      export SPL_SORTED_EXTRACT

echo "Step 01:  Sort SPL extract to re-arrange records within document"
echo "Step 01:  Sort SPL extract to re-arrange records within document"                 >> $PRODPATH/$LOGFILE
echo "          Directories:"                                                           >> $PRODPATH/$LOGFILE
echo "             XTRCTPTH = $XTRCTPTH"                                                >> $PRODPATH/$LOGFILE
echo "             PRODPATH = $PRODPATH"                                                >> $PRODPATH/$LOGFILE
echo "          Input Files:"                                                           >> $PRODPATH/$LOGFILE
echo "             SPL_UNSORTED_EXTRACT = $SPL_UNSORTED_EXTRACT"                        >> $PRODPATH/$LOGFILE
echo "          Output Files:"                                                          >> $PRODPATH/$LOGFILE
echo "             SPL_SORTED_EXTRACT = $SPL_SORTED_EXTRACT"                            >> $PRODPATH/$LOGFILE

# Delete Sort output files
rm -f $PRODPATH/$SPL_SORTED_EXTRACT

$D1APPATH/s5_v3_called_svd_sort.sh

if [ $? -ne 0 ]
   then     
     echo           Sort Error - Process Halting
     echo           Sort Error - Process Halting             				                    >> $PRODPATH/$LOGFILE
     exit
fi

echo "Step 01:  Sort Complete - SPL Extract Sorted"                        				      >> $PRODPATH/$LOGFILE
echo "======================================================================== "	      >> $PRODPATH/$LOGFILE

# ***********************************************************************************
#    Step 02:  Format Field Orders (DOC1GEN)                                        *
# ==================================================================================*
#  The DOC1 Generate module (DOC1GEN) applies the DOC1 Application Rules against    *
#  the Sorted SPL Field Order Print Extract (output from Step 01) to create an      *
#  Print File of all Field Orders contained in the Field Order Print Extract.       *
#                                                                                   *
#  For Batch field order print, the Intermediate Print File will be post-processed  *
#  and divided into different print files according to operational processing       *
#  requirements.                                                                    *
#                                                                                   *
#  For Online Viewing, this process will end with this step; SPL will invoke        *
#  Adobe Acrobat Reader to view the single field order produced by this step        *
# ==================================================================================*
#   Input Files                                                                     *
#      SPL_SORTED_EXTRACT                                                           *
#              - Name of Sorted SPL Extract containing Field Order data             *
#              - This file was output into the PRODPATH directory by Step 01        *
#   Configuration Files                                                             *
#      GEN_OPS                                                                      *
#              - Name of the DOC1GEN Initialization File                            *
#              - This file must be in the D1APPATH directory prior to running       *
#                this .SH process                                                   *
#      GEN_HIP                                                                      *
#              - Name of the DOC1GEN Application Rules                              *
#              - This file must be in the D1APPATH directory prior to running       *
#                this .SH process                                                   *
#      GEN_ETS                                                                      *
#              - Name of the Look-Up File                                           *
#              - This file must be in the D1APPATH directory prior to running       *
#                this .SH process                                                   *
#      LOGOXML                                                                      *
#              - Name of XML file containing attributes of external images          *
#              - This file must be in the D1APPATH directory prior to running       *
#                this .sh process                                                   *
#   Output Files                                                                    *
#      DISPATCH_JOURNAL                                                             *
#              - Name of the Dispatch Journal file containing print routing data    *
#                and Output determination data triggers                             *
#              - This file will be written to the PRODPATH directory                *
#              - This .SH process will delete previous versions of this file        *
#      INTERMEDIATE_PRINT                                                           *
#              - Name of the file containing all formatted Field Orders             *
#              - This file will be written to the PRODPATH directory                *
#              - This .SH process will delete previous versions of this file        *
#      GENDIJ                                                                       *
#              - Name of the DOC1 Interchange Journal containing Index data         *
#              - This file will be written to the PRODPATH directory                *
#              - This .SH process will delete previous versions of this file        *
# ***********************************************************************************
DISPATCH_JOURNAL=$template\_dispatch_journal.jrn;                                              export DISPATCH_JOURNAL
INTERMEDIATE_PRINT=$template\_intermediate_print.$PRINT;                                       export INTERMEDIATE_PRINT
GENDIJ=$template\_e2_journal.dij;                                                              export GENDIJ
echo "Step 02:  Format Field Orders (DOC1GEN)" 
echo "Step 02:  Format Field Orders (DOC1GEN)"                                          >> $PRODPATH/$LOGFILE
echo "Directories:"                                                                     >> $PRODPATH/$LOGFILE
echo "   D1HOST = $D1HOST"                                                              >> $PRODPATH/$LOGFILE
echo "   D1APPATH = $D1APPATH"                                                          >> $PRODPATH/$LOGFILE
echo "   PRODPATH = $PRODPATH"                                                          >> $PRODPATH/$LOGFILE
echo "Input Files:"                                                                     >> $PRODPATH/$LOGFILE
echo "   SPL_SORTED_EXTRACT = $SPL_SORTED_EXTRACT"                                      >> $PRODPATH/$LOGFILE
echo "Output Files:"                                                                    >> $PRODPATH/$LOGFILE
echo "   INTERMEDIATE_PRINT = $INTERMEDIATE_PRINT"                                      >> $PRODPATH/$LOGFILE
echo "   DISPATCH_JOURNAL = $DISPATCH_JOURNAL"                                          >> $PRODPATH/$LOGFILE
echo "   GENDIJ = $GENDIJ"                                                              >> $PRODPATH/$LOGFILE
echo "Configuration Files:"                                                             >> $PRODPATH/$LOGFILE
echo "   GEN_OPS = $GEN_OPS"                                                            >> $PRODPATH/$LOGFILE
echo "   GEN_HIP = $GEN_HIP"                                                            >> $PRODPATH/$LOGFILE
echo "   GEN_ETS = $GEN_ETS"                                                            >> $PRODPATH/$LOGFILE
echo "------------------------------------------------------------------------------"   >> $PRODPATH/$LOGFILE

# delete Step 02 Output Files from previous run
rm -f $PRODPATH/$INTERMEDIATE_PRINT
rm -f $PRODPATH/$DISPATCH_JOURNAL
rm -f $PRODPATH/$GENDIJ

$D1APPATH/s5_v3_called_doc1gen.sh
if [ $? -ne 0 ]
   then     
     echo "DOC1GEN Error (Format Field Orders) - Process Halting"
     echo "DOC1GEN Error (Format Field Orders) - Process Halting"                       >> $PRODPATH/$LOGFILE
     exit 16
fi

echo "Step 02:  DOC1GEN Complete - Field Orders Formatted"                              >> $PRODPATH/$LOGFILE
echo "=============================================================================="   >> $PRODPATH/$LOGFILE

if [ $RUN_TYPE = ONLINE ]
   then
     echo "-------- Copying PDF $PRODPATH/$INTERMEDIATE_PRINT to $ONLINE_PDF"           >> $PRODPATH/$LOGFILE
     cp  $PRODPATH/$INTERMEDIATE_PRINT $ONLINE_PDF
     echo "cp  $PRODPATH/$INTERMEDIATE_PRINT $ONLINE_PDF "                              >> $PRODPATH/$LOGFILE
     if [ $DELETEFILES = Y ]
       then
          rm -f $PRODPATH/$SPL_SORTED_EXTRACT
          rm -f $PRODPATH/$INTERMEDIATE_PRINT
          rm -f $PRODPATH/$DISPATCH_JOURNAL
     fi
     echo "--------------------------Process Completed-----------------------------------"
     exit
fi

