#!/bin/sh
#
# netface - Interface for network printing 
#
# Copyright (c) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994 Silicon Graphics
#
# $Revision: 1.36 $
#
# Info for lptool:
#
# NAME=Remote via TCP/IP, DEVICE=REMOTE
# 
# Required Configuration:
#
# HOSTNAME should be set to the name of the remote host.
# HOSTPRINTER should be set to the name of the target printer on
# the remote host.
# NETTYPE should be set to the type of network interface to use to
# communicate with HOSTPRINTER@HOSTNAME
#
# Optional Configuration:
#
# HOSTID     may be set to the user id that is used on the remote host.
#	     NOTE: This script will only work if the default shell of
#	     HOSTID is /bin/sh.
#
# LOGFILE    may be set to the name of log file for error and status
#            messages.
#
# VERBOSE    may be set to non-zero to enable logging in $LOGFILE.  The
#            flag may be turned on for individual requests by using the
#            -overbose option on the lp command line.
#
# SYNC       controls the behaviour of lp on the local system.  See note
#            below for more information.
#
# HOSTTMPDIR specifies the temporary directory to be used on the remote
#            machine.
#
# CONVTYPE   specifies a workspace type to which all files will be converted
#            before they are transferred to the remote machine.  If
#            CONVTYPE=PostScriptFile (the default) then the file is converted
#            to PostScript before being sent to the print server.  If CONVTYPE
#            is not set the file is sent 'as is' to the print server.  To
#            send the file 'as is', change "CONVTYPE=PostScriptFile" to 
#            "CONVTYPE=" (below).   If you have Impressario installed you
#            can change CONVTYPE to "ImpressarioPostScriptFile".  This will
#            enable the options such as "landscape", etc. when printing
#            to a BSD/LPD system.
#            
####

TYPE=unknown
NAME=
NETTYPE=

HOSTNAME=
HOSTPRINTER=

HOSTTMPDIR=/usr/tmp; export HOSTTMPDIR
HOSTID=lp; export HOSTID
CONVTYPE=
LOGFILE=log

exec 2>> $LOGFILE

####
# Some flags used to affect ParseOptions.  None of this has any
# effect unless CONVTYPE is non-null.
####
faceup=0				# Does printer stack faceup?
bestfit=1				# Rotate image to best fit?
reversetext=0				# Reverse text document page order?

####
# Set verbose to non-zero to get log info in /var/spool/lp/log
# May also be set by -overbose option on lp command line
####
VERBOSE=0

####
# Maximum number of two minute retries
# A count of 60 gives two hours for the remote host to come up before
# aborting the print job.
####
MAXRETRY=60; export MAXRETRY

####
# Syncronous means to wait for the request to finish on the remote
# machine before returning.  If set to false, then print requests will
# be removed from the requesting machine's queue as soon they are sent
# to the server.  If set to true, then the requesting machine will
# wait for the print job to actually finish on the server before submitting
# another request.
####
SYNC=1

myname=`basename $0`
if [ -z "$HOSTNAME" ]
then
	disable -r "$myname: no remote hostname configured."
fi
printer=$HOSTPRINTER
if [ -z "$printer" ]
then
	disable -r "$myname: no remote printer name configured."
fi

if [ -z "$NETTYPE" ]
then
	disable -r "$myname: no network type configured."
fi

#
# OK, time to process command line arguments:
#

seqid=$1
name=$2
title="$3"
copies=$4
options=$5

SetDebug()
####
# This routine turns on debug mode for all appropriate programs.
####
{
	# Non-printer-specific filters:
	#
	VERBOSE=1
	IMPR_SGI2PSOPTS="$IMPR_SGI2PSOPTS -D"
	IMPR_IMG2STIFFOPTS="$IMPR_IMG2STIFFOPTS -D"
	IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -Q"
}

ParseOptions()
####
# Parses -o command line options and sets appropriate global variables.
#
# These global variables are set to affect the pipeline constructed by
# the fileconvert command.  /usr/lib/print/netprint runs fileconvert on
# each file before sending it the remote system if it is given the "-t"
# flag.  Otherwise, most of this options will have no affect on the local
# system (they will be dealt with by the remote spooler).
####
{
	####
	# Loop through the options, shifting off those read at end of loop.
	####
	count=$#
	while [ "$count" -gt 0 ]
	do
		case $1 in
		####
		# General model file options
		####
	
			# Print debug (verbose) messages into spooler log file?
			# 
			-debug | debug)	        argcnt=1; SetDebug	;;
			-verbose | verbose)    	argcnt=1; SetDebug	;;
			-noverbose | noverbose) argcnt=1;         	;;
	
		####
		# PostScript page order filter options
		####

			####
			# Print the specified page range, two integer
			# page numbers
			####
			-pspagerange | pspagerange) argcnt=3;
				IMPR_PSFILTOPTS="$IMPR_PSFILTOPTS -p$2-$3" ;;
	
			####
			# Reverse the PostScript page order
			####
			-psreversepage | psreversepage) argcnt=1;
				 IMPR_PSFILTOPTS="$IMPR_PSFILTOPTS -r" ;;
	
			####
			# Select odd pages only
			####
			-psoddpage | psoddpage) argcnt=1; 
				IMPR_PSFILTOPTS="$IMPR_PSFILTOPTS -o" ;;
	
			####
			# Select even pages only
			####
			-psevenpage | psevenpage) argcnt=1; 
				IMPR_PSFILTOPTS="$IMPR_PSFILTOPTS -e" ;;
	
		####
		# Image filter options:
		#####
			
			####
			# Rotate the image in integer degrees
			####
			-rotate | rotate)      	argcnt=2; 	
				IMPR_SGI2PSOPTS="$IMPR_SGI2PSOPTS -r $2";
				bestfit=0			     	;;
			####
			# Rotate the final image to the best fit orientation
			####
			-bestfit | bestfit)	argcnt=1;  bestfit=1 	;;
			####
			# Flip the image, producing a mirror image.
			####
			-flip   | flip)		argcnt=1; 
				IMPR_SGI2PSOPTS="$IMPR_SGI2PSOPTS -f"	;;
			####
			# Scale the image to fit the page.
			# Takes a float argument, see appropriate man pages.
			####
			-zoom   | zoom)		argcnt=2; 
				IMPR_SGI2PSOPTS="$IMPR_SGI2PSOPTS -z $2";;
			####
			# Scale the final image size to match the
			#  specified input resolution, in pixels per inch.
			####
			-ppi    | ppi)  	argcnt=2; 
				IMPR_SGI2PSOPTS="$IMPR_SGI2PSOPTS -p $2";;
			####
			# Set the amount of gamma warping for the image
			####
			-gamma  | gamma) 	argcnt=2;
				IMPR_SGI2PSOPTS="$IMPR_SGI2PSOPTS -G $2";;
						
		####
		# Text filter options	
		####
	
			####
			# Use PostScript for text processing.
			####
			-pstext | pstext)  	argcnt=1; ;;
	
			####
			# Page orientation
			####
			-landscape | landscape) argcnt=1;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -H"  ;;
			-portrait  | portrait)  argcnt=1;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -V"  ;;
	
			####
			# Font name, argument is font name as specified in
			# man page for the filter associated with $TEXT2PS.
			####
			-fontname  | fontname)  argcnt=2;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -F$2";;
	
			####
			# Font size, in points, a positive integer.
			####
			-fontsize  | fontsize)  argcnt=2;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -P$2bp";;
	
			####
			# Margin sizes: top, bottom, left and right margins
			# specified as <float_value> [in | cm | mm]
			####
			-topmargin | topmargin) argcnt=3;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -T$2$3";;
			-bottommargin | bottommargin) argcnt=3;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -B$2$3";;
			-leftmargin | leftmargin) argcnt=3;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -L$2$3";;
			-rightmargin | rightmargin) argcnt=3;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -R$2$3";;
			
			####
			# Number of columns on page, a non-negative integer
			####
			-columns   | columns) 	argcnt=2;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -M$2";;
			
			####
			# Number each page : [tb][lcr] [number]
			####
			-numberpages | numberpages)  argcnt=3;
				IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -N$2$3";;
	
			####
			# Reverse the page order?
			####
			-reversepages | reversepages) argcnt=1;
				reversetext=1 ;;
			-noreversepages | noreversepages) argcnt=1;
				reversetext=0 ;;
	
			-gaudy | gaudy)     argcnt=1; gaudymode=1   ;;
			-nogaudy | nogaudy) argcnt=1; gaudymode=0   ;;
				
		#
		# All other options are discarded with an appropriate message
		#
			*)  argcnt=1; echo $myname: unknown option: $1 ;;
		esac
		shift $argcnt
		count=`expr $count - $argcnt`
	done
	
	####
	# Now do post-processing on the parsed arguments. 
	####
	
	####
	# Set the gaudy mode for the text filter:
	####
	if [ "$gaudymode" = 1 ]; then
	   IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -G -O 0.4pt" ;
	fi
	
	####
	# Turn off page reversal for printers that print facedown. 
	####
	if [ $faceup = 0 -a $reversetext = 0 ]; then 
		 IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -U";
	elif [ $faceup != 0 -a $reversetext != 0 ]; then
		 IMPR_TEXT2PSOPTS="$IMPR_TEXT2PSOPTS -U";
	fi     
	
	####
	# Check whether BestFit be turned on in sgi2ps (see sgi2ps man page)
	####
	if [ $bestfit != 0 ]; then 
		 IMPR_SGI2PSOPTS="$IMPR_SGI2PSOPTS -F";
	fi     
} # End of ParseOptions

####
# The only options that are relevant are options that affect the VERBOSE
# variable, unless CONVTYPE is set.  In this case, the options will affect
# the conversion of the files to a format for the remote destination.
####
ParseOptions $options

if [ $VERBOSE -gt 1 ]
then
	LOGFILE=/dev/console
fi

vflag=
if [ $VERBOSE != 0 ]
then
	vflag=-v
	echo "$myname `date +%T`: Start $seqid" >>  $LOGFILE
fi

convert=
if [ "$CONVTYPE" != "" ]
then
	convert="-t $CONVTYPE"
fi

wait=
if [ $SYNC != 0 ]
then
	wait=-w
fi

export IMPR_SGI2PSOPTS
export IMPR_IMG2STIFFOPTS
export IMPR_TEXT2PSOPTS

####
# Run netprint to transfer the request to the remote host and, if the
# -w flag is set, to wait for the job to complete on the remote host.
####
/usr/lib/print/netprint -h $HOSTNAME -p $HOSTPRINTER -n $NETTYPE \
	$convert $wait $vflag $seqid

status=$?

if [ $VERBOSE != 0 ]
then
	echo "$myname `date +%T`: End $seqid" >>  $LOGFILE
fi

exit $status
