# Printer/Terminal Maintenance
#
# Created:	 05-Sep-87
# Revision Date: 09-Nov-87
#
# Created By:	 Sandy Marshall
# Modified By:	 Sandy Marshall
#

# Edit DEFAULT as needed:
DEFAULT="q"

# Edit TITLE as needed:
TITLE="			Printer/Terminal Maintenance"

if [ "`echo -n`" = "-n" ]
then
	NLF1=""
	NLF2="\c"
else
	NLF1="-n"
	NLF2=""
fi

while true
do

t=`pwd`
s=`date '+%a %h %d, 19%y'`
clear=`tput clear`

echo $clear

# SHOW ==================================================

cat << TBLOCK
$TITLE

TBLOCK
echo "Present Directory: $t		Date: $s \n"
cat << SHOWEND
		Printer				Terminal

		a. Add a Printer		h. Enable a Terminal
		b. Remove a Printer		i. Disable a Terminal
		c. Turn on Scheduler
		d. Turn off Scheduler
		e. Enable a Printer
		f. Disable a Printer
		g. Display Printer Status

		q. Return to previous menu	? Help

SHOWEND
#
# Add additional lines before 'SHOWEND' and
# add corresponding entries in 'SELECT' below:
# <tab><letter(s)><tab><description"	# format for additional lines
#
# Use blank lines for visual grouping.
#

# ENTER ==================================================

echo $NLF1 "

			       Select [$DEFAULT] :  $NLF2"
read SELECT

if [ -z "$SELECT" ]
then
	SELECT=$DEFAULT
fi

DEFAULT=$SELECT
RT="0"

echo $clear

# SELECT ==================================================

case $SELECT in
	a)	CDIR=`pwd`
		echo "			Ports Available\n"
		echo "	CUBIX 2				CUBIX 3\n"
		echo "	ttyh1 - ttyh7			ttyh1 - ttyhf\n\n"
		echo "			Current Printers\n"
		lpstat -t
		echo "\n\n			Printer Models Available\n"
		ls -FC /usr/spool/lp/model
		PORT="ttyh7"
		PTR="pr"
		MODEL="dumb"
		echo "\nIs system a CUBIX 2 or 3, enter 2 or 3: \c"
		read CUBE
		if [ -n "$CUBE" ]
		then	if [ "$CUBE" -eq 2 -o "$CUBE" -eq 3 ]
			then	if [ "$CUBE" -eq 3 ]
				then	PORT="ttyhf"
				fi
				echo "\nPort ID to be used for printer <default $PORT>: \c"
				read x
				if [ -n "$x" ]
				then	PORT=$x
				fi
				echo "\nEnter printer name <default pr>: \c"
				read x
				if [ -n "$x" ]
				then	PTR=$x
				fi
				echo "\nEnter printer model <default dumb>: \c"
				read x
				if [ -n "$x" ]
				then	MODEL=$x
				fi
				echo "\nEnter \"yes\" to set printer as default: \c"
				read x
				if [ "$x" = yes ]
				then	ttyoff $PORT
					/usr/lib/lpshut
					/usr/lib/lpadmin -p$PTR -v/dev/$PORT -m$MODEL
					enable $PTR
					/usr/lib/accept $PTR
					/usr/lib/lpadmin -d$PTR
				else 	ttyoff $PORT
					/usr/lib/lpshut
					/usr/lib/lpadmin -p$PTR -v/dev/$PORT -m$MODEL
					enable $PTR
					/usr/lib/accept $PTR
				fi
				/usr/lib/lpsched
				if [ ! -f /etc/rc.d/lp.rc ]
				then	cat >>/etc/rc.d/lp.rc <<!
/usr/lib/lpsched
!
					cd /etc/rc.d
					chmod 744 lp.rc
					cd $CDIR
				fi
				EXERR=0
			else	echo "\nIncorrect response"
				EXERR=0
				RT=1
			fi
		else	EXERR=0
		fi						;;

	b)	echo "			Current Printers\n"
		lpstat -t
		echo "\nPrinter to be removed: \c"
		read PTR
		if [ -n "$PTR" ]
		then	echo "Cancel any request currently printing: \c"
			read x
			if [ "$x" = yes ]
			then	cancel $PTR
			fi
			/usr/lib/lpshut
			/usr/lib/lpadmin -x$PTR
			/usr/lib/lpsched
			EXERR=0
			RT=1
		else	EXERR=0
		fi						;;

	c)	echo "Turning Scheduler On"
		/usr/lib/lpsched
		EXERR=0
		RT=1						;;

	d)	echo "Turning Scheduler Off"
		/usr/lib/lpshut
		EXERR=0
		RT=1						;;

	e)	echo "			Printers Available\n"
		lpstat -v
		echo "\nPrinter to be enabled: \c"
		read x
		if [ -n "$x" ]
		then	enable $x
			EXERR=0
			RT=1
		else	EXERR=0
		fi						;;

	f)	echo "			Printers Available\n"
		lpstat -v
		echo "\nPrinter to be disabled: \c"
		read x
		if [ -n "$x" ]
		then	echo "\nEnter \"yes\" to cancel any requests currently pending: \c"
			read y
			if [ "$y" = yes ]
			then	cancel $x
			fi
			disable $x
			EXERR=0
			RT=1
		else	EXERR=0
		fi						;;

	g)	echo "			Current Printer Status\n"
		lpstat -t
		EXERR=0
		RT=1						;;


	h)	echo "			Ports Available\n"
		echo "	CUBIX 2				CUBIX 3\n"
		echo "	ttyh0 - ttyh7			ttyh0 - ttyhf\n\n"
		echo "Enter ID of terminal port to be enabled: \c"
		read PORT
		if [ -n "$PORT" ]
		then	ttyon $PORT
			EXERR=$?
		else	EXERR=0
		fi						;;
				
	i)	echo "			Ports Available\n"
		echo "	CUBIX 2				CUBIX 3\n"
		echo "	ttyh0 - ttyh7			ttyh0 - ttyhf\n\n"
		echo "Enter ID of terminal port to be disabled: \c"
		read PORT
		if [ -n "$PORT" ]
		then	ttyoff $PORT
			EXERR=$?
		else	EXERR=0
		fi						;;

	\?)	sh /usr/bin/adptr.help
		EXERR=0
		RT=1						;;

	q)	exit $EXERR					;;


esac

if [ "$EXERR" != "0" ]
then
	echo $NLF1 " Exit error: $EXERR	$NLF2	"
	echo $NLF1 "  Press <RETURN> to continue:  $NLF2"
	read OK
else
	if [ "$RT" = "1" ]
	then
		echo $NLF1 " \n Press <RETURN> to continue:  $NLF2"
		read OK
	fi
fi

done
