#!/bin/sh
ditroff=ditroff
psdit=psdit
pic=/usr/local/bin/pic
tbl=/bin/tbl
eqn=/bin/eqn
folfil=/usr/local/bin/folfil
printype=ps
rofftype=troff
landscape=0
F= 				# soelim args
TO= macros=-mfb20		# troff options
printer=-P${PRINTER-ps} nospool=0 spoolopt=-Jditroff	# spool options
pro= defpro=-p/psdit.pro.roman8 # psdit options
reduce=-1
filspec=-fpt
for i in $*
do  case "$i" in
	-Tpsc)	;;
	-T*)	echo only -Tpsc is valid 1>&2 ; exit 2 ;;
	-P*)	printer=$i ;;
	-C*|-J*|-#*|-h|-m)
		spoolopt="$spoolopt $i" ;;
	-m*)	macros=$i;;
	-p*)	pro=$i;;
	-t)	nospool=1 ;;
	-troff)	rofftype=cat;;
	-L) TO="$TO -rL1"; landscape=1;;
	-f*)	filspec=$i;;
	-[1248]) reduce=$i;;
	-?*) TO="$TO $i";;
	*) F="$F $i";;
    esac
done

case $printype in
ps)	resolution=-T576;
	sedopt="-f /usr/local/lib/ps/ps.sed"
	TO="$TO -rF0 -rP1"
	case $landscape in
	1)	defpro=-p/psdit.land.roman8;;
	esac ;;
esac

case "$pro" in
"") pro="$defpro"
esac

case $filspec in
*c*)	filter="cat";;
*)	filter="/bin/sed $sedopt"
esac
case $filspec in
*p*)	filter="$filter | $pic $resolution"
esac
case $filspec in
*b*)	filter="$filter | sed '/^$/d'"
esac
case $filspec in
*t*)	filter="$filter | $tbl "
esac
# case $filspec in
# *h*)	filter="$filter | xhyph -c troff"
# esac
case $filspec in
*e*)	filter="$filter | $eqn"
esac
case $filspec in
*a*)	filter="$folfil | $filter"
esac
# case $filspec in
# *O*)	ditroff=ditroff
# esac

case $reduce in
	-1)	spool="lpr $printer $spoolopt";;
	-2)	spool="2up $printer";;
	-4)	spool="4up $printer";;
	-8)	spool="8up $printer";;
esac
dit="$ditroff -Tpsc -t $macros $TO -"
post="$psdit $pro"

/usr/ucb/soelim $F | 
eval $filter |
case $rofftype$printype$nospool in
cat*)	cat;;
troffps1)	$dit | $post;;
troffps0)	$dit | $post | $spool;;
# troffln1)	echo ln -t not supported;;
# troffln0)	$xroff;;
esac
