#!/bin/ksh -p
# netlp: http://www.bolthole.com/solaris/netlp Feb 2011
# Version 0.3
#
# simple wrapper to "directly" print to a networked postscript printer,
#  taking advantage of SunOS/Solaris  /usr/lib/lp/bin/netpr
# Meant to mimic /bin/lp functionality.
# Currently expects PRINTER or LPDEST to be set to name of netprinter.
# Alternatively, use -d flag, like lp.
#
# Useful future enhancements:
#  - get the -c flag *fully* working
#  - autoconvert text to postscript if needed
#  - respect /etc/printers.conf "_default" entry
#  - have some kind of "lpstat" functionality.
#


#############################################################
# Internal notes below.
#
# Note that we basically take /usr/lib/lp/model/netstandard, take the
# useful bits, and move it up one layer in the user API.
# So, no more logger -p lpr.debug spam. Although you could always add your own.
#
# Lots of other intresting things in /usr/lib/lp/bin.
# like lp.tell, lp.cat, lp.set, 

#Note: we use "PRINTER"  variable, to define destination.
# But most networked printers, or other smart hosts, are actually servers,
# with virtual "printer" addresses we can address. eg:
#  net-laser!ps, netlaser!auto, netlaser!text
#
# So, use "target" for that. Or, LPTARGET, in our case.
# For a command-line option, use.... erm.. 
#  we dont have an "lp" compatible option that I can see.
# unless we parse "DEST". yeah, thats it. old-school bang-style.
#  LPDEST=server!printername

PATH=/usr/lib/lp/bin:$PATH

USER=${USER:-$LOGNAME}

usage(){
	print 'netlp: A program to print directly to a net printer'
	print ''
	print 'Usage: netlp {options} filename(s)'
	print 'Currently recognied arguments are:'
	print '   -d printer.name  -T type -p sub-printer' 
	print 'Arguments are meant to shadow /bin/lp arguments'
	print 'It even respects $PRINTER and $LPDEST'
	print 'Subtle differences:'
	print 'It presumes a postscript printer, and does not autoconvert.'
	print 'If you want to print text directly, and your printer supports'
	print 'a text subtarget, then for a unix text file, use'
	print '  netlp -T text   ....'

	exit 1
}


if [[ "$LPDEST" != "" ]] ; then PRINTER="$LPDEST"; fi
while getopts "d:p:T:scmh" flag ; do
	case $flag in
	d)
		PRINTER="$OPTARG"
	;;
	p|T)
		LPTARGET="$OPTARG"
	;;

	s) : ;; #sounds useful: suppress output from printing...

	c) COPYFLAG="-c"
	 print WARNING: passing in -c to netpr. Which SHOULD work, but seems to break it
	;;
	m) print "sorry, -$flag not implemented yet";;
	*) usage;;
	esac
done
shift $(($OPTIND - 1 ))

# netstandard expects: printer as $0, requestid, username, title, copies, options
# All other args are filenames
# We will ignore the 0, and use the "dest=" option. I hope.
##netstandard  nobanner

#Orrr, use netpr. Which takes as arguments:
# netpr -I request_id -p printer -d destination
#  -U username [ -f type ] [ -T title ] [ -P protocol ]
#  [-t timeout] [ -c ] [ -b ]
#           files

#netstandard calls it with
#eval ${NETPR} ${printfile} | ${LPTELL} ${LPTELL_OPTS} ${printer}
#which is really
#/netpr ${banner_flag} ${data_file_flag} \
#        -I ${request_id} -U ${user_name} \
#        -p ${printer} ${dest} -T \"${title}\"  \
#        ${timeout}  ${protocol} ${controlfile} 
#
#and gets implemeneted sometimes as
# NETPR= /usr/lib/lp/bin/netpr
#     -I testlp-27 -U user@host
#     -p testlp -d some.addr.here -T "27-1" 
#     -P bsd  


case "$PRINTER" in
	"")
		print ERROR: no printer specified
		exit 1
		;;
	*!*)
		# parse "server!printername" syntax
		LPTARGET=${PRINTER#*!}
		PRINTER=${PRINTER%!*}
		;;
esac
LPTARGET=${LPTARGET:-auto}

queueid=`date '+%Y%M%d%H%M%S'`

CMD="/usr/lib/lp/bin/netpr -I $PRINTER-$queueid -U $USER -p $LPTARGET -d $PRINTER -T $USER@$queueid -P bsd $COPYFLAG"
##CMD="echo fake $CMD"

for file in "$*" ; do
	if [[ ! -f $file ]] ; then
		print ERROR on $file: we only handle regular files right now
	else
		$CMD $file
	fi
	shift
done

