#	@(#)add.prt.FTS.sh	13.1 (RC International) 91/08/23
#
#	add.prt.FTS: Add an FTS printer.
#
#	/usr/sadm/sysadm/bin/add.prt.FTS
#		<printer> <remote host> <remote printer> <vpid>
#

echo
echo "Checking if the printer is running, please wait ...";
if /usr/bin/lpstat -r | grep not > /dev/null
then
	echo "Printer scheduler is not running."
else
	echo "Stopping the printer scheduler, please wait ..."
	/usr/lib/lpshut > /dev/null
fi

echo "Adding printer $1, please wait ..."
intf_file=/tmp/lp.intf.$4

cat > $intf_file << \EOT
#!/bin/sh
#
# FTS NETWORK printer
#
# $0: The printer device
# $1: Request id
# $2: Userid
# $3: The extra title for the front page
# $4: Number of copies of the files
# $5: If h|H no header is wanted
# $6 .... $n are filenames
#
copies=${4:-0}
shift 5
for F in $*
do
    N=$copies
    while test $N -gt 0
    do
EOT
/bin/echo "        wr -s$2 -t -p$3 \$F 2>/dev/null" >> $intf_file
cat >> $intf_file << \EOT
        N=`expr $N - 1`
    done
done
exit 0
EOT

error=/tmp/lp.error.$4
/usr/lib/lpadmin -p$1 -v/dev/null -i$intf_file > /dev/null 2> $error

if [ "$?" != "0" ]
then
	echo "When executing the command:"
	echo
	echo "    /usr/lib/lpadmin -p$1 -v/dev/null -i$intf_file"
	echo
	echo "the following error occurred:"
	echo
	cat $error
	echo
	echo "Interface file $intf_file not removed."
	echo
	rm -f $error
fi

echo  "Printer $1 added. Starting the scheduler, please wait ...";
rm -f /usr/spool/lp/SCHEDLOCK
/usr/lib/lpsched > /dev/null
echo "Enabling printer $1, please wait ..."
/usr/bin/enable $1 > /dev/null
echo "Start $1 to accept requests, please wait ..."
/usr/lib/accept $1 > /dev/null

echo
echo "Printer $1 added and ready to accept requsts.";

echo
echo 'Please note that users can only use the following'
echo "options to lp for the printer '$1':"
echo '    -d (destination) and'
echo '    -n (number of copies)'

rm -f $error $intf_file
