:
#	@(#) oaprint 23.2 91/08/29 
#
#	Copyright (C) 1990-1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
# Normally, the path is /usr/lib/oa2, but it may
# be adjusted for certain installations.

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

# For print debugging.
: ${LOGFILE:=/dev/null}
# dump ALL output to the log, otherwise it goes to the screen
exec >>$LOGFILE 2>&1  

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
if [ ! -r $PRINTDIR/$printername ] ; then
	echo "oafprint: cannot open $PRINTDIR/$printername"
	exit 1
fi
. $PRINTDIR/$printername 	

# 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
else
	# Make sure the file really exists
	if [ ! -r $file ] ; then
		echo "oafprint: cannot open $file"
		exit 1 
	fi
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


if [ "$spooler" ]; then

	# Print to a spooled printer
	if [ "$tmpfile" = "y" ]; then
		($putil $putil_flags < $file | $OALIB/oafprint \
		-p$printername | $spooler $spool_flags && rm -f $file) &
	else
		$putil $putil_flags < $file | $OALIB/oafprint \
		-p$printername |  $spooler $spool_flags &
	fi
	# end of spooled printing

elif [ "$local" ]; then

	if [ "$tmpfile" = "y" ]; then
		($putil $putil_flags < $file | $OALIB/oafprint -l \
		-p$printername > $local  && rm -f $file) &
	else
		$putil $putil_flags < $file | $OALIB/oafprint -l \
		-p$printername > $local  &
	fi
	# end of local printing

else
	# not spooled or local, so must be direct
test  "$interactive" = "" 
case $? in
0)
	# direct non-interactive printer
    LCK=/tmp/`basename $device`.lock
    while :
    do
	(umask 777;> $LCK) 2> /dev/null
	case $? in
	0)	: Ok
	    trap 'rm -f $LCK; exit 2' 1 2 3 15
	    break
	    ;;
	*)  sleep 10		# wait for lock to be removed
            continue
	    ;;
	esac
    done
    ;;
esac

    (
	case $interface in
	    S*) stty $baud_rate $stty < $device 
		;;
	esac

	$putil $putil_flags < $file | $OALIB/oafprint -p$printername
    ) > $device

    rm -f $LCK
    # end of direct printing
fi
