#! /bin/csh -f
#
#	%W%	(Berkeley)	%G%
#
#		-grn	runs through grn
#		-grap	runs through grap
#		-pic	runs through pic
#		-tbl	runs through tbl
#		-eqn	runs through eqn
#		-psfig	runs through psfig
#
#		-legal		for legal paper (implies manualfeed in psdit)
#		-landscape	for landscape mode
#		-manualfeed	for manual feed on Apple LaserWriter
#
#		-ps	to produce PostScript Output
#
#
#	note that the default device is 5a, unless $PRINTER is defined
#	
umask 0
set flags=() noglob files=() garbage=()
unset t
unset legal
unset landscape
unset manualfeed
unset cmd
unset grn
unset grap
unset pic
unset tbl
unset eqn
unset psfig
unset psfile
if ($?PRINTER) then
    set printer = -P$PRINTER
else
    set printer = (-P5a)
endif
set dest = (psc)
set grnprog = (/usr/misc/grn)
set grapprog = (/usr/misc/grap)
set picprog = (/usr/misc/pic)
set tblprog = (/usr/misc/tbl)
set eqnprog = (/usr/misc/eqn)
set psfigprog = (/usr/misc/psfig)
set troff = (/usr/misc/troff)
set psdit = (/usr/misc/psdit)
set lpr = (/usr/ucb/lpr)
set family = Times
set fontdir = /usr/misc/lib/ps/ditroff.font

top:
	if ($#argv > 0) then
		switch ($argv[1])
		case -z:
		case -a:
			set flags = ($flags $argv[1])
		case -t:
			set t
			shift argv
			goto top
		case -legal:
		case -G:
			set legal
			shift argv
			goto top
		case -landscape:
		case -L:
			set landscape
			shift argv
			goto top
		case -manualfeed:
		case -M:
			set manualfeed
			shift argv
			goto top
		case -ps:
			set psfile
			shift argv
			goto top
		case -grn:
			set grn
			shift argv
			goto top
		case -grap:
			set grap
			set pic
			shift argv
			goto top
		case -pic:
			set pic
			shift argv
			goto top
		case -tbl:
			set tbl
			shift argv
			goto top
		case -eqn:
			set eqn
			shift argv
			goto top
		case -psfig:
			set psfig
			set flags = ($flags -mpsfig)
			shift argv
			goto top
		case -F
			if ($#argv < 2) then
				echo "-F takes following font family name"
				exit(1)
			endif
			set family = $argv[2]
			shift argv
			shift argv
			goto top
		case -F*
			echo "use -F familyname"
			exit(1)
		case -D
			if ($#argv < 2) then
				echo "-D takes following font directory"
				exit(1)
			endif
			set fontdir = $argv[2]
			shift argv
			shift argv
			goto top
		case -D*
			echo "use -D fontdirectory"
			exit(1)
		case -TPostscript:
		case -PPostscript:
		case -Tps:
		case -Pps:
		case -Pla:
		case -Tla:
		case -Tsa:
		case -Psa:
		case -Tba:
		case -Pba:
		case -T1a:
		case -P1a:
		case -T2a:
		case -P2a:
		case -T3a:
		case -P3a:
		case -T5a:
		case -P5a:
		case -T6a:
		case -P6a:
		case -T7a:
		case -P7a:
		case -T7364:
		case -P7364:
		case -Tta:
		case -Pta:
		case -Tpsc:
		case -Ppsc:
		case -TLaserWriter:
		case -PLaserWrite:
		case -Tlw:
		case -Plw:
		case -Tece:
		case -Pece:
		case -Tlps1:
		case -Plps1:
			set dest = (psc)
			set printer = $argv[1]
			shift argv
			goto top
		case -P*: 
			echo "undefined -P typesetter"
			exit(1)
		case -T*:
			echo "undefined -T typesetter"
			exit(1)
		case -?*:
			set flags = ($flags $argv[1])
			shift argv
			goto top
		default:
			set files = ($files $argv[1])
			shift argv
			goto top
		endsw
	endif
set name = (ditroff)
if ($#files > 0) then
	set name = ($files[1])
endif
# New code to take care of soelim, grn, tbl, eqn
#
# Soelim is used because neither eqn nor tbl knows about .so
# or about mixing files and stdin.
if ($?grn || $?grap || $?pic || $?tbl ||$?eqn ||  $?psfig) then
	set cmd=( '(' soelim $files)
	set files=(/tmp/preprocess$$)
	set garbage=(/tmp/preprocess$$)
	onintr cleanup
endif
if ($?grn) then
	set cmd=($cmd "|" $grnprog "-P$dest")
endif
if ($?grap) then
	set cmd=($cmd "|" $grapprog)
endif
if ($?pic) then
	set cmd=($cmd "|" $picprog "-P$dest")
endif
if ($?tbl) then
	set cmd=($cmd "|" $tblprog)
endif
if ($?eqn) then
	set cmd=($cmd "|" $eqnprog "-P$dest")
endif
if ($?psfig) then
	set cmd=($cmd "|" $psfigprog)
endif
if ($?cmd) then
	eval $cmd ')' > $files
#	echo $cmd ')'
endif
# End of new code

if ($?legal) then
	set psdit=($psdit -G)
	set troff=($troff -G)
endif
if ($?landscape) then
	set psdit=($psdit -L)
	set troff=($troff -L)
endif
if ($?manualfeed) then
	set psdit=($psdit -M)
endif

# ok, time to print it, somewhere.

#
# are we sending this stuff to stdout?
#
if ($?t) then
	$troff -T$dest -F$fontdir/$family $flags $files
#
# postscript printer
#
else if ($?psfile) then
# evaluate filter and issue the command in a subshell
	set pscexec=('(' $troff "-T$dest" "-F$fontdir/$family" "$flags" "$files" | $psdit "-F$fontdir/$family" ')')
	eval $pscexec
#	echo $pscexec
#
else if ($dest == psc) then
# evaluate filter and issue the command in a subshell
	set pscexec=('(' $troff "-T$dest" "-F$fontdir/$family" "$flags" "$files" | $psdit "-F$fontdir/$family" | $lpr "-h" "$printer" ')')
	eval $pscexec
#	echo $pscexec
#
else
#
# ok, just normal. send it to lpr -P$dest
#
	$troff -T$dest -F$fontdir/$family $flags $files | $lpr -P$dest -n -J$name
endif

cleanup:
if ($#garbage > 0) then
	rm -f $garbage
endif
