#!/bin/sh
#
# Shell script to add the network postscript file identifier string 
# %! to dos lpt port output and send the entire print stream to
# the network printer program lp(1)
#
# This utility is called via the lp printer emulation
#
# usage: Postscript <lp(1) flags>
#
#     Copyright (c) 1991, Sun Microsystems, Inc.  All Rights Reserved
#     Sun considers its source code as an unpublished, proprietary
#     trade secret, and it is available only under strict license
#     provisions.  This copyright notice is placed here only to protect
#     Sun in the event the source is deemed a published work.  Dissassembly,
#     decompilation, or other means of reducing the object code to human
#     readable form is prohibited by the license agreement under which
#     this code is provided to the user or company in possession of this
#     copy.
 
#     RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
#     Government is subject to restrictions as set forth in subparagraph
#     (c)(1)(ii) of the Rights in Technical Data and Computer Software
#     clause at DFARS 52.227-7013 and in similar clauses in the FAR and
#     NASA FAR Supplement.

# This script DOES rely on external environment variables. 
# It assumes that:
# SUNPCHOME has been set and that the following utility program can be found:
#
#       $SUNPCHOME/bin/prepend_ps
#	/usr/bin/lp(1) - SunOS network printer program lp(1)
#	/usr/bin/lpstat(1) - SunOS network printer program lpstat(1)
#
# In addition it assumes:
#	all flags are for lp and are correct in their usage.
#
# Parse off the arguments and make sure we have everything we need
#

ARGS=$*

#
# check to make sure we have everything that we need

if test -x $SUNPCHOME/bin/prepend_ps
then
	:
else
	echo "Postscript:  error: $SUNPCHOME/bin/prepend_ps: does not exist or is not executable"
	exit 1
fi

if test -x /usr/bin/lp
then
	:
else
	echo "Postscript:  error: /usr/bin/lp: does not exist or is not executable"
	exit 1
fi

if test -x /usr/bin/lpstat
then
	:
else
	echo "Postscript:  error: /usr/bin/lpstat: does not exist or is not executable"
	exit 1
fi

# Check for -d <printer>
while [ $# -gt 0 ]; do
	#echo $0 $*
	if [ $1 = "-d" ] 
	then
		lpstat $1 $2 1> /dev/null 2>&1
		if [ $? -ne 0 ] 
		then
			echo "Postscript: unknown printer $1 $2"
			exit 1
		fi
	fi
	shift
done

#
# Add Postscript Header %! to the first line if the output starts with
# '%' or Ctrl D
# Send the SunOS Network compatible Postscript or ASCII output text
# to the SunOS printer of choice.
#
$SUNPCHOME/bin/prepend_ps | /usr/bin/lp $ARGS

