#!/bin/sh
#/*c
# *	JetRoff - DWB 2.0 postprocessor for HP LaserJet Series II
# *
# *	Copyright (c) 1988 PC Research, Inc.  All Rights Reserved.
# *
# *	This source code is supplied to you under the terms of the
# *	contents of the "License" agreement found with this source
# *	distribution.  You must read the "License" before you use
# *	this source code in any way.
# *
#c*/

#
# @(#) $Id: jetroff.sh,v 1.10 89/11/27 03:40:27 rick Exp $
#
# $Log:	jetroff.sh,v $
# Revision 1.10  89/11/27  03:40:27  rick
# Fixed -4
# 
# Revision 1.9  89/07/24  15:18:57  rick
# Set "lp" to NULL for FAX output
# 
# Revision 1.8  89/06/05  15:36:05  rick
# Changes for 2.6
# 
# Revision 1.7  89/04/13  19:26:24  rick
# -T -P cleanup
# 
# Revision 1.6  89/02/04  16:09:38  rick
# Added -T option handling
# 
# Revision 1.5  89/01/21  17:11:40  rick
# Added -M help
# Added /bin/sh csh comment
# Added SCCS id flag
# 
# Revision 1.4  88/11/17  18:20:20  rick
# Fixed -4 -manual
# 
# Revision 1.3  88/11/17  17:31:58  rick
# Fixed typo
# 
# Revision 1.2  88/10/02  19:41:53  rick
# Added -m option
# 
# Revision 1.1  88/08/26  23:10:58  rick
# Initial revision
# 
#

JETSCAN=${JETSCAN:-1}	# Enable scan for needed preprocessors

usage() {
	echo "Usage:	jetroff [-p -t -e -g -b]"
	echo "		[-oLIST -nN -Mlist -Ppaper -sN -mXX -rR# -i -q -z -a]"
	echo "		[-Ffontdir -Rrastdir -Tprinter -cN -dDEST -S] files ..."
	echo
	echo "	-p -e -t -g	Preprocess with pic, eqn, tbl, and/or grap"
	echo "	-b		Include bitmap inclusion macros"
	echo "	-o LIST		Print only pages in LIST"
	echo "	-nN		Number first page with number N"
	echo "	-M LIST		Manual feed pages in LIST"
	echo "	-mXX		Include the XX macros (/usr/lib/tmax/tmac.XX)"
	echo "	-rR#		Set register R (a one character name) to #"
	echo "	-i		Read standard input after all other files"
	echo "	-q		Invoke simultaneous IO mode of the .rd request"
	echo "	-z		Print only messages generated by .tm requests"
	echo "	-a		Send a crude ASCII approximation to terminal"
	echo "	-Ffontdir	Search for the fonts in fontdir"
	echo "	-Rrastdir	Search for the rasters in rastdir"
	echo "	-Tprinter	Set printer options: -Tjet+, -Tjet2, -Tdesk"
	echo "			or trays available: -Tlegal, -Ta4, -Tletter"
	echo "	-Ppaper		Use paper: letter, legal, executive, a4"
	echo "	-cN		Have the printer generate N copies (max 99)"
	echo "	-dDEST		Change printer destination to DEST (- is stdout)"
	echo "	-lN[:W]		Landscape, N pages/sheet, W is sheet width"
	echo "	-S		Print document statistics to stderr"
	echo "More? \c"
	read yn </dev/tty
	if [ "Q$yn" = Qn ]
	then
		return 0;
	fi
	echo
	echo "Example:	jetroff -mm mymemo.mm"
	echo "Example:	jetroff -man manpage.man"
	echo "Example:	jetroff -b -mm bitmaps.mm"
	echo
	echo "The need for the -e, -t, -p, -g, and -b flags are determined"
	echo "automatically by jetroff by scanning the document.  The flags"
	echo "are provided for those cases where jetroff cannot determine"
	echo "a need for these flags, usually because the document includes"
	echo ".so (include file) requests."
}

set -- `getopt h?o:n:s:m:r:iqzaF:P:R:T:c:d:l:M:Sbpteg4% $*`
if [ $? != 0 ]; then usage; exit 2; fi

piped=2
cat=
eqn=
pic=
grap=
tbl=
backup=
backup_opts=		# jetbackup options
troff=troff		# Troff command
troff_device=-Tjet	# Troff device name
troff_opts=		# Troff options
djet=djet		# djet postprocessor
djet_opts=		# djet options
lp=lp			# line printer spooler
lp_opts=-otroff		# spooler options (raw output, no CR/NL mapping)
pid=$$

for i in $*
do
	case $i in
	-m)
		if [ "Q$2" = Qanual ]
		then
			# Special help for -manual (PCR local)
			if [ "Q$backup" = Q ]
			then
				djet_opts="$djet_opts -l2 -Plegal";
			fi
			troff_opts="$troff_opts -rs1";
		fi
		troff_opts="$troff_opts $i$2";
		shift 2;
		;;
	-4)
		# Special help for 4 on 1 output (PCR local)
		djet_opts="$djet_opts -l1 -Plegal";
		backup="jetbackup -b";
		backup_opts="$backup_opts -Plegal";
		shift;
		;;
	-o|-n|-s|-r)
		troff_opts="$troff_opts $i$2"; shift 2;;
	-i|-q|-z)
		troff_opts="$troff_opts $i"; shift;;
	-a)
		troff_opts="$troff_opts $i"; shift; djet=; lp=;;
	-F)
		troff_opts="$troff_opts $i$2";
		djet_opts="$djet_opts $i$2/devjet";
		shift 2;;
	-M|-R|-c|-l)
		djet_opts="$djet_opts $i$2"; shift 2;;
	-T)
		case $2 in
		fax)
			lp=;
			lp_opts=;
			troff_device=-Tfax;;
		esac
		djet_opts="$djet_opts $i$2";
		backup_opts="$backup_opts $i$2"; shift 2;;
	-P)
		djet_opts="$djet_opts $i$2";
		backup_opts="$backup_opts $i$2"; shift 2;;
	-d)
		if [ "Q$2" = Q- ]; then lp=;
		else lp_opts="$lp_opts $i$2"; fi
		shift 2;;
	-S)
		djet_opts="$djet_opts $i"; shift;;
	-b)
		troff_opts="$troff_opts -mjetroff"; shift;;
	-p)
		pic=pic; cat=cat; shift;;
	-e)
		eqn=eqn; cat=cat; shift;;
	-t)
		tbl=tbl; cat=cat; shift;;
	-g)
		grap=grap; pic=pic; cat=cat; shift;;
	-%)
		piped=`expr $piped - 1`; shift;;
	--)
		shift; break;;
	-h|-?)
		usage; exit 0;;
	esac
done
files="$*"

if [ "Q$JETSCAN" = Q1 ]
then
	for i in `jetcheck $files`
	do
		case $i in
		-p)
			pic=pic; cat=cat;;
		-e)
			eqn=eqn; cat=cat;;
		-t)
			tbl=tbl; cat=cat;;
		-g)
			grap=grap; pic=pic; cat=cat;;
		-b)
			troff_opts="$troff_opts -mjetroff"; shift;;
		esac
	done
fi

cmd=
if [ $piped = 2 ]
then
	if [ "$cat" != "" ]; then cmd="$cat $files | "; files=; fi
	if [ "$grap" != "" ]; then cmd="$cmd $grap | "; fi
	if [ "$pic" != "" ]; then cmd="$cmd $pic | "; fi
	if [ "$tbl" != "" ]; then cmd="$cmd $tbl | "; fi
	if [ "$eqn" != "" ]; then cmd="$cmd $eqn | "; fi
	cmd="$cmd $troff $troff_device $troff_opts $files"
	if [ "$djet" != "" ]; then cmd="$cmd | $djet $djet_opts"; fi
	if [ "$backup" != "" ]; then cmd="$cmd | $backup $backup_opts"; fi
	if [ "$lp" != "" ]; then cmd="$cmd | $lp $lp_opts"; fi
else
	if [ $piped = 0 ]; then pid=; fi
	to=
	fn=0
	if [ "$cat" != "" ]; then cmd="$cat $files | "; files=; fi
	if [ "$grap" != "" ]
	then
		if [ "$to" != "" ]; then ti="<$to"; else ti=; fi
		fn=`expr $fn + 1`; to="/tmp/JET$pid.$fn"
		cmd="$cmd $grap $ti >$to;";
	fi
	if [ "$pic" != "" ]
	then
		if [ "$to" != "" ]; then ti="<$to"; else ti=; fi
		fn=`expr $fn + 1`; to="/tmp/JET$pid.$fn"
		cmd="$cmd $pic $ti >$to;";
	fi
	if [ "$tbl" != "" ]
	then
		if [ "$to" != "" ]; then ti="<$to"; else ti=; fi
		fn=`expr $fn + 1`; to="/tmp/JET$pid.$fn"
		cmd="$cmd $tbl $ti >$to;";
	fi
	if [ "$eqn" != "" ]
	then
		if [ "$to" != "" ]; then ti="<$to"; else ti=; fi
		fn=`expr $fn + 1`; to="/tmp/JET$pid.$fn"
		cmd="$cmd $eqn $ti >$to;";
	fi
	if [ "$to" != "" ]; then ti="<$to"; else ti=; fi
	fn=`expr $fn + 1`; to="/tmp/JET$pid.$fn"
	cmd="$cmd $troff $troff_device $troff_opts $files $ti >$to"
	if [ "$djet" != "" ]
	then
		ti="<$to"
		cmd="$cmd; $djet $djet_opts $ti"
	fi
	if [ "$backup" != "" ]; then cmd="$cmd | $backup $backup_opts"; fi
	if [ "$lp" != "" ]; then cmd="$cmd | $lp $lp_opts"; fi
fi
eval $cmd
if [ $piped = 1 ]
then
	i=1
	while [ $i -le $fn ]
	do
		rm /tmp/JET$pid.$i
		i=`expr $i + 1`
	done
fi
exit 0
#	undocumented -% (and -% -%) option for 386/ix floating point
#	bug (can't have two procs doing fp at same time).
