#!/bin/sh
#############################################################################
##  APPX_PRINT                  Release 3.4             last modified 8/13/98
##
##  This script will submit the Appx report specified on the 
##  the command line to print spooler.
##
##  The format of the command line is expected to be:
##
##	<script-name> [-config=<config_file_name>] [options*] printfiles*
##
##  Where the "-config=<config_file_name>", if present, specifies
##  a configuration file to be used when submitting the given 
##  printfiles to the spooler.
##
##  A configuration file is expected to be a flat ascii file which 
##  should contain a list of spooler options, one per line, in 
##  the format:
##
##	-<option>=<value>
##
##  The options may also be specified on the command line.  Options are
##  processed in order of appearance, whether they come from a configuration
##  file or from the command line.  The value of an option will be the last
##  one processed.
##
##  Where "-<option>" is one of the following:
##
##	-mode=
##	-disposition=
##	-copies= 
##	-notify=
##	-banner=
##	-format=
##	-priority=
##	-date=
##	-user=
##	-printer_name=
##	-printer_id=
##	-printer_ctl=
##	-printer_queue=
##	-form_name=
##	-form_id=
##	-form_ctl=
##
#############################################################################

#############################################################################
##  NAME: PROCESS_CONFIG( config_file_name )
##
##  DESCRIPTION:  This function will read spooler options from the named
## 		  configuration file.
##
##  PARAMETERS:	  $1 = Pathname of configuration file to be parsed
##

PROCESS_CONFIG ()
{

##
##  Open the configuration file as file descriptor #3
##
    exec 3< $1

##
##  Now read through the configuration file, putting each line in $CFG_REC
##
    while read CFG_REC <&3
    do

##
##	Treat lines beginning with a "#" as comments
##
	if [ "$CFG_REC" = "\#*" ]
	then
	    continue
	fi

##
##	Process this line as if it were a command line argument
##
	PROCESS_OPTION $CFG_REC
	
    done

##
##  Close the configuration file
##
    exec 3<&-
}

#############################################################################
##  NAME: PROCESS_OPTION( option )
##
##  DESCRIPTION:  This function will parse the given spooler option
##
##  PARAMETERS:   $1 = Spooler option to be processed
##

PROCESS_OPTION ()
# echo "PROCESS_OPTION:  $1" >> $LOGFILE     # turn on to capture each Option
{
    case "$1" in
	-mode*)
	    #  	This option contains the print mode for
	    #   this print job.  The value will be one of the 
	    #	following:
	    #		<blank>
	    #		HOLD   - queue but don't print files
	    #		SPOOL  - queue for printing ASAP
	    #   	KEEP   - do not queue files for printing
	    #		ONLINE - bypass spooler and print directly
	    #			 to device (future)
	    #		
	    #	The -mode option is currently ignored
	    ;;

	-disposition*)
	    # 	This option specifies what to do with the print
	    # 	files after they have been printed.  The following
	    #	values should be expected:
	    #		<blank>
	    #		SCRATCH	- delete the file after printing
	    #		SAVE	- retain the file after printing
	    #		REQUEUE - requeue the file after printing
	    #
	    DISPOSITION=`echo $* | cut -f 2 -d =`
	    ;;		

	-copies*)		
	    #	This option specifies how many copies the 
	    #	user would like to print.  It contains the 
	    #   value of the "PRINT COPIES" PDF.
	    #
	    #
	    COPIES=`echo $* | cut -f 2 -d =`
	    ;;

	-notify*)
	    # 	This option is an interpretation of the 
	    #   "PRINT NOTIFY USER" PDF.  It will contain 
	    #	either "Yes", "No", or blank.
	    #
	    #	The -notify option is currently ignored.
	    ;;

	-banner*)
	    #	This option contains an interpretation of the
	    #	"PRINT BANNER" PDF.  It will contain either "Yes",
	    #	"No", or blank.
	    #	
	    #	The -banner option is currently ignored.
	    ;;

	-format*)
	    #	This option contains the value of the "PRINT
	    # 	FORMAT" PDF.  This option is reserved for future
	    #	use.
	    #
	    #	The -format option is currently ignored.
	    ;;
	
	-priority*)
	    #	This option contains the value of the "PRINT
	    #  	PRIORITY" PDF.  
	    #
	    #	The -priority option is currently ignored.
	    ;;

	-date*)
	    #	This option contains an interpetation of the
	    #	"PRINT SPOOL DATE" PDF.  It will be in the form
	    #	CCYYMMDDhhmmssth.  This date specifies when the 
	    #	user would like the report released to the printer.
	    #	
	    #	The -date option is currently ignored.
	    ;;

	-user*)
	    #	This option contains the Unix user name
	    # 	of the submitter.
	    #
	    #	This script ignores the -user option
	    ;;

	-printer_name*)
	    #	This option contains the value of the "PRINT
	    #	PRINTER ID" PDF, which is the Appx identifier
	    #	for the destination printer.
	    #
	    #	This script ignores the -printer_name option
	    ;;

	-printer_id*)
	    #	This option contains the OS name for the destination
	    #	printer.  This value comes from the Appx System 
	    #	Administration PRINTER file record whose key is 
	    #	determined by the "PRINT PRINTER ID" PDF.
	    #
	    #	The -printer_id option is currently ignored.
	    ;;

	-printer_ctl*)
	    #	This option contains user-defined options for the 
	    #	destination printer.  This value comes from the Appx
	    #	System Administration PRINTER file record whose key
	    #	is determined by the "PRINT PRINTER ID" PDF.
	    #

	    PRTR_CTL=`echo $* | cut -f 2 -d =`
	    ;;

	-printer_queue*)
	    #	This option specifies the name of the queue to which
	    #	the given reports should be spooled.  This value comes
	    #	from the Appx System Administration PRINTER file 
	    #	record whose key is determined by the "PRINT PRINTER
	    #	ID" PDF.
	    #

	    QUEUE_NAME=`echo $* | cut -f 2 -d =`
	    ;;

	-form_name*)
	    #	This option contains the value of the "FORM ID" PDF, 
	    # 	which is the Appx identifier for the desired form.
	    #
	    #	This script ignores the -form_name option
	    ;;

	-form_id*)
	    #	This option contains the OS name for the destination
	    #	form.  This value comes from the Appx System 
	    #	Administration FORM file record whose key is 
	    #	determined by the "FORM ID" PDF.
	    #

	    FORM_ID=`echo $* | cut -f 2 -d =`
	    ;;

	-form_ctl*)
	    #	This option contains user-defined options for the 
	    #	destination form.  This value comes from the Appx
	    #	System Administration FORM file record whose key
	    #	is determined by the "FORM ID" PDF.
	    #

	    FORM_CTL=`echo $* | cut -f 2 -d =`
	    ;;

    esac
}

##############################################################################
##  MAIN
##
##  Process the command line arguments, watching for a configuration file.
##  
##  Stop as soon as we see the first argument which does not begin with a 
##  hyphen.  The remainder of the command line is considered to be a list 
##  of filenames which we pass straight to the spooler.
##

    COPIES=1				# Default number of copies 

##  The option to lp which causes a copy of the printfile to be made in a
##  spooler directory somewhere.  This is necessary if we wish to scratch
##  the appx printfile from within the script.  If you are worried about
##  disk space (if a second copy of your report will exceed your capacity)
##  you may opt not to FREEZE the printfile.  WARNING:  Do NOT (I repeat:
##  Do NOT) SCRATCH if you remove the following declaration.

    FREEZE="-c"

    UMASK=`umask`; umask 00             # store original umask; set to '00' 

#       LOGFILE=/dev/null               # create NO Log file
        LOGFILE=/tmp/appx_print.last    # create a Log file

# use either of the following to initialize or accumulate LOGFILEs 
 echo "============================================================" >  $LOGFILE
#echo "============================================================" >> $LOGFILE
                                                                                
    umask $UMASK                        # restore original umask                
                                                                                
 echo "All of <`whoami`> <$LOGIN> <$LOGNAME> must be the same." >> $LOGFILE
# set >> $LOGFILE        # dump current user's environment into LOGFILE         
 echo >> $LOGFILE                                                               
 echo "............. Parameters passed to 'appx_print' ..........." >> $LOGFILE

    echo $* >> $LOGFILE                 # record '$*' parameters into LogFile
    echo    >> $LOGFILE

    CONFIG_FILE=`echo $1 | cut -f 2 -d =`
    	echo "configuration file:	$CONFIG_FILE"  >> $LOGFILE
    PRINT_FILE=$2
    	echo "print file:	   	$PRINT_FILE"   >> $LOGFILE

    PROCESS_CONFIG "$CONFIG_FILE"
    rm $CONFIG_FILE		# once used, delete configuration file

##
##  Now that we have processed all of the command line arguments and 
##  any configuration files, assemble the device name.  In keeping with
##  Appx releases prior to 2.0, we use the system form id followed by
##  the queue name (I have no idea why!).
##

    if [ -n "$FORM_ID" -a -n "$QUEUE_NAME" ]
    then
##
##  	We have both a form id and a queue name, separate them
## 	with a colon since that's what "lp" prefers
##
	DEVICE="-d$FORM_ID:$QUEUE_NAME"

    elif [ -n "$FORM_ID" ]
    then
##
##	We have only a form id
##
	DEVICE="-d$FORM_ID"

    elif [ -n "$QUEUE_NAME" ]
    then
##
##	We have only a queue name
##	
	DEVICE="-d$QUEUE_NAME"
    else
##
##	We weren't given a device, let "lp" pick the default
##
	DEVICE=""
    fi

## 
##  Now assemble the command:  note that this "lp" command should
##  be identical to those produced by Appx prior to release 2.0
##

    while [ $COPIES -gt 0 ]
    do
	 echo >> $LOGFILE; echo "## Final Invocation of 'lp':">> $LOGFILE
    echo "lp $FREEZE $DEVICE $FORM_CTL $PRTR_CTL $PRINT_FILE" >> $LOGFILE
          lp $FREEZE $DEVICE $FORM_CTL $PRTR_CTL $PRINT_FILE  >> $LOGFILE 2>&1

	COPIES=`expr $COPIES - 1`
    done
    if [ "$DISPOSITION" = "SCRATCH" ]
    then
        rm -f $PRINT_FILE 		# if user specified SCRATCH, delete it
    fi
    echo >> $LOGFILE                    # blank line at end of LOGFILE entry    
  
#############################################################################
##  Modification History
##
##  DATE      BY   Description
##  --------  ---  ---------------------------------------------------------
##  11/13/93  KAD  Initial Creation
##  07/26/94  BHW  Modified to run under Bourne shell (instead of Korn
##		   Shell) for portability.  No loss of functionality, but
##		   some of the readability was sacrificed.  Many external
##		   utilities had to be used to compensate for the shell
##		   "downgrade."
##  07/26/94  BHW  Forced shell functions to spawn subshells to maintain
##		   positional parameters under HP-UX 8.0.  Now returning
##		   values from subshells through a clever (yet awkward)
##		   system of echos and evals.
##  09/20/94  BHW  First pass at multiple copies and scratching. ECR 2495
##  01/09/96  PAT  removed "eval `(...)`" construction, for latest hp & AIX. 
##                 releases.   Currently accepts only two input parameters:
##		      configuration filename, followed by print filename.
##   6/17/96  PAT  disabled default logging; delete configuration file.
##   8/01/96  PAT  reenabled /tmp/appx_print.last, with umask of 00; line 277   
##   8/13/98  PAT  additional logfile tracking  
##
#############################################################################
