#!/bin/sh
# print -- group of operations on the local printers
#
# SYNOPSIS: this group of operations understands how to drive the
# local printers and what the name of the default printer is.
# The default printer used can also be set by the shell variable PRINTER.
#
# AUTHOR:	Gary T. Leavens

# default printer
PRINTER=${PRINTER-'lwleavens'}


# set duplex command
case "$1" in
duplex)
    shift
    ASCIIDUPLEX=-Dduplex:true
    PSPRINTERDUPLEX=-Zduplex
    ;;
*)
    ASCIIDUPLEX=''
    PSPRINTERDUPLEX=''
    ;;
esac

# printer command and flags
LPRCOMMAND='/usr/local/bin/lpr'  # use lp for HP-UX
PRINTERFLAG='-P'
ENSPRINTERFLAG='-d'
# dvi to postscript translation
PRINTDVI=${PRINTDVI-"dvips -q $PRINTERFLAG $PRINTER"}


printGroup()
{
USAGE="Usage: 
$0 duplex ...           # duplex printing when possible
$0 amstex file ...	# AMSTeX first, then print
$0 ascii [file ...]	# default style of printing for ascii files
$0 bigSlides [file ...]	# bigger font for slides (good idea!)
$0 cancel		# cancel user's print jobs on default printer
$0 dvi file ...		# prints TeX and LaTeX output
$0 landscape [file ...]	# like ascii, but in landscape mode
$0 landscapeSlides [file ...] # landscape slides, ? lines long, 57 chars wide
$0 latex file ...	# LaTeX first, then print
$0 letter file ...	# dvi formatted isuletter
$0 noHead [file ...]	# ascii style without any headings
$0 outline [-n section] [file ...] # ascii, emacs outline to std
$0 pdf file ...  	# prints PDF files
$0 postscript file ...	# prints postscript files
$0 queue		# look at the queue for the default printer
$0 slides [file ...]	# ascii slides, 31 lines long, 42 chars wide
$0 twoup [file ...]     # like ascii, but 2 pages per page
$0 tex file ...		# TeX first, then print
"

case "$1" in
ascii)
	shift
	enscript "${ENSPRINTERFLAG}$PRINTER" -i3 -k -fCourier11 "$@"
	;;

twoup)
	shift
	enscript -2r $ASCIIDUPLEX "${ENSPRINTERFLAG}$PRINTER" "$@"
	;;


no[Hh]ead)
	shift
	printGroup ascii -B "$@"
	;;

landscape)
	shift
	printGroup ascii -r -fCourier8 "$@"
	;;

outline)
	shift
	# get -n argument for emacs_std_outline
	NARG=""
	case "$1" in
	-n)
		NARG="$1$2"
		shift; shift
		;;
	-n*)
		NARG="$1"
		shift
		;;
	esac
	pr -l61 "$@" | emacs_std_outline $NARG | printGroup noHead
	;;

dvi)
	shift
	RET=0
	for f in "$@"
	do
		$PRINTDVI $f || RET=1
	done
	exit $RET
	;;

pdf|PDF)   # PDF
	shift
	RET=0
	for f in $*
	do
	   # pdf2ps -dLanguageLevel=2 $f - | $DUPLEX | $LPRCOMMAND "${PRINTERFLAG}$PRINTER" || RET=1
           acroread -toPostScript < $f | $LPRCOMMAND "${PRINTERFLAG}${PRINTER}" $PSPRINTERDUPLEX
	done
	exit $RET
	;;

[Pp]ost[Ss]cript)   # postscript
	shift
	RET=0
	for f in "$@"
	do
		$LPRCOMMAND "${PRINTERFLAG}$PRINTER" $f $PSPRINTERDUPLEX || RET=1
	done
	exit $RET
	;;

letter)
	shift
	RET=0
	for f in "$@"
	do
		$PRINTDVI -O -1.0in,0in $f || RET=1
	done
	exit $RET
	;;

latex|tex|amstex)
	formatter=$1
	shift
	RET=0
	for f in "$@"
	do
		case $f in
		*.tex)
			prefix=`expr $f : '\(.*\).tex'`
			;;
		*)
			prefix=$f
			;;
		esac
		$formatter $f && $PRINTDVI ${prefix}.dvi || RET=1
	done
	exit $RET
	;;

queue)
	shift
	/usr/local/bin/lpq "${PRINTERFLAG}" "$PRINTER" "$@"
	;;

cancel)
	shift
	/usr/local/bin/lprm "${PRINTERFLAG}$PRINTER" clean all "$@"
	;;

slides)
	shift
	enscript "${ENSPRINTERFLAG}$PRINTER" -Bk -fCourier-Bold21 "$@"
	;;

landscape[Ss]lides|lslides)
	shift
	enscript "${ENSPRINTERFLAG}$PRINTER" -r -Bk -fCourier-Bold21 -c "$@"
	;;

big[Ss]lides)
	shift
	enscript "${ENSPRINTERFLAG}$PRINTER" -Bk -fHelvetica-Bold32 "$@"
	;;

*)
	echo "$USAGE" >&2
	exit 2
	;;
esac
}

[ -n "$echo" ] && set -x

printGroup "$@"

