:
#      @(#)rlp	5.6.1.3 Lachman System V STREAMS TCP  source
#      SCCS IDENTIFICATION
#
#	Copyright (C) The Santa Cruz Operation, 1991.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and Lachman Associates Inc. 
#	and should be treated as Confidential.
#
#
: ${OK=0} ${FAIL=1}
PATH=/bin:/usr/bin:/etc:/etc/conf/bin

WORKDIR=/usr/lpd/remote
PRINTCAP=/etc/printcap
SPOOL=/usr/spool
LPD=/usr/spool/lpd
SOURCE=/usr/lib/tcprt/rlp
LP_FILES="/usr/bin/lp /usr/bin/cancel /usr/bin/lpstat /usr/lib/lpd"
START=/etc/rc2.d/S86lpd
STOP=/etc/rc0.d/K79lpd

##############################################################################
#
# Prompt with mesg, return non-zero on q. Also allows setting -x and +x,
# shell escapes and the passing of default values
#
# Usage: prompt "message" [default]
# Argument is a quoted message
# Dependencies: expr
# Notes: returns 1 if the user answers q or Q, returns 0 otherwise
#	 if $quit is not set, it is set to "quit"
#	 $cmd is set to whatever is input by the user, and then can
#		subsequently be used in the shell script.
#
prompt() {
	_mesg=$1
	while	echo "${_mesg} [$2]: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		Q|q)	return 1					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is an second argument use it
			# as the default else loop until 'cmd' is set
			[ "$2" ] && { 
				cmd=$2
				return 0
			}
			: continue
			;;
		*)	unset _mesg
			return 0					;;
		esac
	done
}

##############################################################################
#
# Prompt for yes or no answer with message passed as argument - 
# returns non-zero for no
#
getyn() {
	case $2 in
	    [yY])
		default="y"
		mesg="$1 (y/n) [$default] "
		;;

	    [nN])
		default="n"
		mesg="$1 (y/n) [$default] "
		;;

	    *)
		default=""
		mesg="$1 (y/n) "
		;;
	esac

	while	echo "\n$mesg\c">&2
	do	read yn rest

		if [ "$yn" = "" -a "$default" ]
		then
		    yn=$default
		fi

		case $yn in
		+x|-x)	set $yn					;;
		[yY])	return 0 				;;
		[nN])	return 1				;;
		*)	echo "Please answer y or n" >&2		;;
		esac
	done
}

##############################################################################
#
# Install rlp
#
install() {
	[ ! "$_no_prompt" ] && echo "Installing Remote Line Printing\n"     

	if  [  -d "$WORKDIR" ] ; then
		[ "$_no_prompt" ] && exit $FAIL
		echo "Remote line printing working directory already exists.\n"  
		getyn "Do you wish to continue installing the remote line printing system?"
		[ $? -ne 0 ] && exit $OK
	fi

	[ ! "$_no_prompt" ] && echo "Creating directories $WORKDIR and $LPD and file $PRINTCAP."

	if  [ ! -d "$WORKDIR" ] ; then
		mkdir -p $WORKDIR 
	fi
	if  [ ! -d "$SPOOL" ] ; then
		mkdir $SPOOL
		chmod 775 $SPOOL
		chown root $SPOOL
		chgrp daemon $SPOOL
	fi
	if  [ ! -d "$LPD" ] ; then
		mkdir $LPD
		chmod 775 $LPD
		chown root $LPD
		chgrp daemon $LPD
	fi
	if  [ ! -w "$PRINTCAP" ] ; then
		echo "# Remote Line Printer (BSD format)"> $PRINTCAP
		chmod 644 $PRINTCAP
		chown root $PRINTCAP
		chgrp daemon $PRINTCAP
	else
		[ ! "$_no_prompt" ] && echo "The file $PRINTCAP already exists.\n"
	fi

	# Save old rlp commands
	[ ! "$_no_prompt" ] && echo "Saving lp, cancel and lpstat commands to $WORKDIR"
	for i in lp lpstat cancel
	do
		cp /usr/bin/$i $WORKDIR
	done

	# Install new lp commands
	[ ! "$_no_prompt" ] && echo "Installing remote lp, cancel and lpstat commands"
	for i in $LP_FILES
	do
		base=`basename $i`
		cp $SOURCE/$base $i
		chmod 6711 $i
		chown root $i
		chgrp daemon $i
	done
	rm -f /usr/bin/lpr
	ln /usr/bin/lp /usr/bin/lpr

	if [ ! "$_no_prompt" ] ; then
		getyn "Do you wish to change the printer description file $PRINTCAP?" "N"
		if [ $? -eq 0 ] ; then
			/etc/rlpconf
		else
			echo "Run /etc/rlpconf to change the remote printer description file"
		fi
	fi
 
	# Make links to start up scripts
	rm -f $START $STOP /etc/lpd
	cp /usr/lib/tcprt/rlp/S86lpd $START
	ln $START $STOP
	ln $START /etc/lpd

	if [ ! "$_no_prompt" ] ; then
		getyn "Do you want to start remote daemon now?"
		[ $? -eq 0 ] && /usr/lib/lpd start
	fi
}

##############################################################################
#
# Remove rlp
#
remove() {
	# Stop the lpd if running
	[ ! -f /etc/lpd ] && return 0

	/etc/lpd stop

	rm -f $START $STOP /etc/lpd

	for i in lp lpstat cancel
	do
		mv $WORKDIR/$i /usr/bin/$i
	done
	rm -f /usr/bin/lpr /usr/lib/lpd

	for i in $WORKDIR $LPD $PRINTCAP
	do
		if [ ! "$_no_prompt" ] ; then
			getyn "\n\tDo you want to remove $i?" "N"
			[ $? -eq 0 ] && rm -rf $i
		else
			rm -rf $i
		fi
	done
}

##############################################################################
# 
# usage() - Displays usage message (when script in called directly)
#
usage() {
	echo "Usage: rlp [ -i | -r ] [ -n ]\n"
}

##############################################################################
# 
# main()
#
#	Options:
#		-i  ...  Install rlp
#		-r  ...  Remove rlp
#		-n  ...  No prompt mode
#
_no_prompt=
_install=
_remove=

while getopts nir opt
do
	case $opt in

		n) _no_prompt=TRUE
		   ;;

		i) _install=TRUE
		   ;;

		r) _remove=TRUE
		   ;;

		\?) usage
		   exit $FAIL
		   ;;

	esac
done

[ "$_install" -a "$_remove" ] && {
	usage
	exit $FAIL
}

[ "$_install" ] && {
	install
	exit $OK
}
[ "$_remove" ] && {
	remove
	exit $OK
}

# Must assume to be in interactive mode
tput clear
echo "\n\t\tRemote printing configuration"

while prompt "\n\nDo you want to install or remove remote printing (i/r/q)?" "q"
do

	[ $? -eq 1 ] && exit $FAIL

	case $cmd in

		I|i) install
		     exit $OK
		     ;;

		R|r) remove
		     exit $OK
		     ;;

		Q|q) exit $FAIL
		     ;;

		  *) echo "Please answer 'i','r' or 'q'."
		     ;;

	esac
done
