:
#      @(#)rlpconf	5.8 Lachman System V STREAMS TCP  source
#      SCCS IDENTIFICATION
BB=/dev/null

DEFAULT=/usr/spool/lp/default
DODEF=0
WORKDIR=/usr/lpd/remote
PRINTCAP=/etc/printcap
SPOOL=/usr/spool
LPD=/usr/spool/lpd
BIN=/usr/bin
SOURCE=/usr/lib
SYSLOG=/usr/adm/lpd-errs
LPADMIN=/usr/spool/lp/admins/lp/printers
CONF=configuration

CURDIR=`pwd`

local() {
   while true; do
   DEV=
   while [ X$DEV = X ]; do
	   echo "\nPlease enter the the name of the device for $PRINTER: \c"
	   read DEV
   done
   echo "\n\tPrinter $PRINTER uses device $DEV\n"
   RESP=
   while [ X$RESP = X ]; do
	   echo "Is this correct? (y/n) \c"
	   read RESP
   done
   if expr "$RESP" : "^[yY]">$BB 2>$BB; then
	break
   else
     echo "\n\tTry again? (y/n) \c"
     read RESP
     if [ X$RESP = X ]; then
	RESP=y
     fi
     if expr "$RESP" : "^[yY]">$BB 2>$BB; then
	 echo
     else
       exit 1
     fi
   fi
   done

########################################
# change printer description file, etc.  
#######################################
	if [ ! -f $PRINTCAP ]; then
		cp /dev/null $PRINTCAP
		chmod 644 $PRINTCAP; chown root $PRINTCAP
		chgrp daemon $PRINTCAP
	fi
	if /bin/grep $PRINTER $PRINTCAP > /dev/null 2>&1; then
		echo "\n\t$PRINTER already exists in $PRINTCAP.  Skipping."
		sleep 3
	else 
		echo "\nPutting $PRINTER in printer description file and creating spool dir...\c"
		echo "$PRINTER":\\ >> $PRINTCAP
#	echo :lf="$SYSLOG":lp=$DEV:sd="$LPD/$PRINTER": >> $PRINTCAP
		echo "\t":lp=$DEV:sd="$LPD/$PRINTER": >> $PRINTCAP
		if [ ! -d $LPD ]; then
			mkdir $LPD
			chmod 755 $LPD
			chown root $LPD
			chgrp daemon $LPD
		fi
		cd $LPD
		if [ ! -d $PRINTER ] ; then
			   mkdir $PRINTER
			   chmod 775 $PRINTER
			   chown root $PRINTER
			   chgrp daemon $PRINTER
		fi
		sleep 1
		echo "done"
	fi

	sleep 1
}
remote() {
    while true; do
    HOST=
    while [ X$HOST = X ]; do
	   echo "\nPlease enter the the name of the remote host that \c"
	   echo "$PRINTER is attached to: \c"
	   read HOST
   done
   echo "\n\tPrinter $PRINTER is connected to host $HOST\n"
   RESP=
   while [ X$RESP = X ]; do
	   echo "Is this correct? (y/n) \c"
	   read RESP
   done
   if expr "$RESP" : "^[yY]">$BB 2>$BB; then
      echo "Would you like this to be the system default printer? (y/n) \c"
      read RESP
      if [ X$RESP = X ]; then
          RESP= n
      fi
      if expr "$RESP" : "^[yY]">/dev/null 2>&1; then
	DODEF=1
      fi
      echo "\nMake sure your host name appears in $HOST's /etc/hosts.equiv\nor /etc/hosts.lpd file" 
      echo "Make sure that $PRINTER appears in $HOST's /etc/printcap file (BSD format) " 
      echo "Make sure that $PRINTER has a spool directory on $HOST" 
      sleep 4 
      break 
   else
     echo "\n\tTry again? (y/n) \c"
     read RESP
     if [ X$RESP = X ]; then
	RESP=y
     fi
     if expr "$RESP" : "^[yY]">$BB 2>$BB; then
	 echo
     else
       exit 1
     fi
   fi
   done

########################################
# change printer description file, etc.  
#######################################
	if [ ! -f $PRINTCAP ]; then
		cp /dev/null $PRINTCAP
		chmod 644 $PRINTCAP; chown root $PRINTCAP
		chgrp daemon $PRINTCAP
	fi
	if /bin/grep $PRINTER $PRINTCAP > /dev/null 2>&1; then
		echo "\n\t$PRINTER already exists in $PRINTCAP.  Skipping."
		sleep 3
	else 
		echo "\nPutting $PRINTER in printer description file and creating spool dir...\c"
		echo "$PRINTER":\\ >> $PRINTCAP
#	echo :lf="$SYSLOG":lp=:rm="$HOST":rp="$PRINTER":sd="$LPD/$PRINTER": >> $PRINTCAP
		echo "\t":lp=:rm="$HOST":rp="$PRINTER":sd="$LPD/$PRINTER": >> $PRINTCAP
		if [ ! -d $LPD ]; then
			mkdir $LPD
			chmod 755 $LPD
			chown root $LPD
			chgrp daemon $LPD
		fi
		cd $LPD
		if [ ! -d $PRINTER ] ; then
			   mkdir $PRINTER
			   chmod 775 $PRINTER
			   chown root $PRINTER
			   chgrp daemon $PRINTER
		fi
		sleep 1
		echo "done"
		if [ -d $LPADMIN ]; then
			echo "Updating LP information...\c"
			cd $LPADMIN
			if [ ! -d $PRINTER ]; then
				mkdir $PRINTER
				chmod 770 $PRINTER
				chown lp $PRINTER
				chgrp lp $PRINTER
				cd $PRINTER
				echo "#Device: $PRINTER@$HOST" > $CONF
				chmod 660 $CONF
				chown lp $CONF
				chgrp lp $CONF
			fi
			sleep 1
			echo "done"
		fi
		if [ $DODEF = 1 ]; then
			echo "Updating $DEFAULT...\c"
			echo $PRINTER > $DEFAULT
			chmod 644 $DEFAULT
			chown lp $DEFAULT
			chgrp lp $DEFAULT
			sleep 1
			echo "done"
			DODEF=0
		fi
	fi

	sleep 1
}

########################################
#  Get printer name and host name
#  until user enters 'q'
#######################################

while true; do
	tput clear
	echo "\t\t\tRemote Printing Configuration"

	echo "\nEnter information for remote printers or local printers accepting\nremote printing requests"
	echo
	PRINTER=
	while [ X$PRINTER = X ]; do
		echo "Please enter the printer name (q to quit): \c"
		read PRINTER
	done
	if [ $PRINTER = q ]; then
		exit 0
	fi
	while echo "\nIs $PRINTER a remote printer or a local printer (r/l)? \c"; do
		read RM
		case $RM in 
		R|r)
			remote $PRINTER
			break
			;;
		L|l)
			local $PRINTER
			break
			;;
			*)
				echo "\n\tPlease answer 'r' or 'l'"
				;;
			esac
		done
	done
done

cd $CURDIR
exit 0
