#
# Normally, the path is /usr/lib/oa, but it may
# be adjusted for certain installations.

: ${OALIB:=/usr/lib/oa}
: ${SCOLIB:=/usr/lib/sco}
: ${PRINTDIR:=$SCOLIB/printers}

# For print debuging, hess.
: ${LOGFILE:=/dev/null}

printername=$1
file=$2
shift
shift

#
# 'check for -ol and -r'
#
putil=/bin/cat
tmpfile="n"
while  [ -n "$1" ]
do
	case "$1" in
	'-ol')	land=`expr $printername : ".*\.\(.*\)"`
		if [ "$land" != "land" ]; then
			landname=$printername.land
			if [ -r $PRINTDIR/$landname ]; then
				printername=$landname
			else
				putil=$OALIB/pagesize
			fi
		fi
		;;
	'-r')	tmpfile="y"
		;;
	*)	echo "unknown" $1 >idebug
		echo "usage: oaprint printer file [-ol] [-r]"
		exit
		;;
	esac
	shift
done

# source the printer file to set all the variables
. $PRINTDIR/$printername 	>>$LOGFILE 2>&1

# set unset variables to reasonable

: ${printer=default}
: ${margin=0}
: ${form_length=66}
: ${form_width=80}
: ${reformat=""}
: ${interactive=""}
: ${local=""}
: ${device=""}
: ${spooled=""}
: ${serial=""}
: ${stty_opts=""}
: ${baud_rate=""}
: ${spooler=""}
: ${spool_flags=""}
: ${background=""}

# We cannot pass stdin to cat, so use a tmp file. 
if [ "$file" = "-" ]; then
	file=/tmp/$$
	tmpfile="y"
	cat - >$file
fi

# If using pagesize, pass form length & width
if [ "$putil" = "$OALIB/pagesize" ]; then
	putil_flags="-l$form_length -c$form_width"
else
	putil_flags=""
fi


# Do the printing 
if [ "$tmpfile" = "y" ]; then
	($putil $putil_flags < $file | $OALIB/oafprint -p$printername | \
		$spooler $spool_flags >>$LOGFILE 2>&1 && rm -f $file) &
else
	$putil $putil_flags < $file | $OALIB/oafprint -p$printername | \
		$spooler $spool_flags >>$LOGFILE 2>&1 &
fi
