#!/sbin/sh
#	Printer installation script using /etc/printcap
#
#	August 2, 1993      	Gert Poletiek (gert@wins.uva.nl)
#				Jan Wortelboer (janw@wins.uva.nl)
#
#
# Changes 1/11/93, Casper
# Make sanity return a value rather than exit
# Use default timeout for clients 0 for s5 servers, n for bsd servers
# 24/11/93
# Use umask 022
#
#
# No disable/reject on shutdown (breaks lpsched?)
# No lpshut/lpsched when configuring.
#

umask 022
state=$1
set -- `who -r`
PATH=/usr/sbin:$PATH
export PATH

debug=0
printcap=/etc/printcap
filterdirs="/etc/lp/fd /usr/local/lp/fd"
configured=/etc/lp/configured

if [ $debug -eq 1 ]; then
	echo=echo
else
	echo=
fi



sanity ()
{
	if [ -z "$pc_pt" -o \
	     -z "$pc_ct" -o \
	     -z "$pc_ro" ]; then
		return 1
	fi

	if [ -z "$pc_rm" ]; then	# local printer
		if [ -z "$pc_af" -o \
		     -z "$pc_if" -o \
		     -z "$pc_lp" -o \
		     -z "$pc_lf" ]; then
			return 1
		fi
	fi
	return 0
}


isenabled ()
{
    case "`lpstat -p $1 2> /dev/null`" in
    *disabled*) return 1;;
    *enabled*) return 0;;
    *) echo "$0: unexpected output from \"lpstat -p $1\"" 1>&2; return 2;;
    esac
}

isaccepting ()
{
    case "`lpstat -a $1 2> /dev/null`" in
    *not\ accepting*) return 1;;
    *accepting*) return 0;;
    *) echo "$0: unexpected output from \"lpstat -a $1\"" 1>&2; return 2;;
    esac
}

#
#	Start or stop a printer
#
startstop ()
{
	if [ $1 = start ]; then
		isenabled $pc_printer || $echo enable $pc_printer
		isaccepting $pc_printer || $echo accept $pc_printer
	elif [ $1 = stop ]; then
		isenabled $pc_printer && $echo disable $pc_printer
		isaccepting $pc_printer && $echo reject $pc_printer
	fi
}

#
#	Install a printer
#
install_printer ()
{
	printer_list="${printer_list:+${printer_list}|}$pc_printer"

	if [ -z "$pc_rm" ]; then	# local printer
		# setup device
		$echo mkdir -p `dirname $pc_lp` 
		$echo touch $pc_lp
		$echo chown lp $pc_lp
		$echo chgrp lp $pc_lp
		$echo chmod 600 $pc_lp
		# setup log file
		$echo touch $pc_lf
		$echo chown lp $pc_lf
		$echo chgrp lp $pc_lf
		$echo chmod 666 $pc_lf
		# setup accounting file
		$echo touch $pc_af
		$echo chown lp $pc_af
		$echo chgrp lp $pc_af
		$echo chmod 666 $pc_af
		# register printer
		$echo lpadmin \
			-p $pc_printer \
			-v $pc_lp \
			-T $pc_pt \
			-I $pc_ct \
			-i $pc_if \
			-D "$pc_pd"
	else				# remote printer
		#$echo lpsystem -r $pc_rm
		$echo lpsystem -t $pc_ro $pc_rm

		if [ "$pc_rp" != "$pc_printer" ]; then
			$echo lpadmin \
				-p $pc_printer \
				-s ${pc_rm}!${pc_rp} \
				-T $pc_pt \
				-I $pc_ct \
				-D "$pc_pd" 
		else
			$echo lpadmin \
				-p $pc_printer \
				-s $pc_rm \
				-T $pc_pt \
				-I $pc_ct \
				-D "$pc_pd" 
		fi
	fi
}


setup_network_access() {

	# check if the tcp port monitor is already configured and enabled
	sacadm -l | egrep -s "^tcp[ 	]"
	if [ $? -ne 0 ]; then
		# create the tcp port monitor using the Service Access Facility
		$echo sacadm -a -p tcp -t listen -c "/usr/lib/saf/listen tcp" \
			-v `nlsadmin -V` -n 999
	fi

	# check to see which services are registered with the tcp port monitor

	pmadm -l | egrep -s listenS5 
	if [ $? -ne 0 ]; then
		# create listener service for System 5
		$echo pmadm -a -p tcp -s lp -i root -m `nlsadmin \
			-o /var/spool/lp/fifos/listenS5` -v `nlsadmin -V`
	fi

	# get universal address
	u_addr=`lpsystem -A`

	pmadm -l | egrep -s listenBSD 
	if [ $? -ne 0 ]; then
		# create listener service for BSD
		$echo pmadm -a -p tcp -s lpd -i root -m \
		`nlsadmin -o /var/spool/lp/fifos/listenBSD -A "\\x${u_addr}"` \
			-v `nlsadmin -V`
	fi

	pmadm -l | egrep -s nlps_server 
	if [ $? -ne 0 ]; then
		# create listener service for Service 0

		# massage the universal address by replacing port 0203
		# with port 0ace
		new_addr=`lpsystem -A | cut -b1-4`
		tail=`lpsystem -A | awk '{pos = index($0, "0203")+4
				print substr($0, pos, length($0)-pos+1)}'`
		new_addr=`echo ${new_addr}0ACE${tail}`

		$echo pmadm -a -p tcp -s 0 -i root -m \
		`nlsadmin -c /usr/lib/saf/nlps_server -A "\\x${new_addr}"` \
			-v `nlsadmin -V`
	fi
}



install_filters ()
{
	for dir in $filterdirs; do
		for fd in $dir/*.fd; do
			$echo lpfilter -f `basename $fd .fd` -F $fd
		done
	done
}



#
#	actual start of script
#
case $state in

'start')
	if [ $9 = "2" -o $9 = "3" ]; then
		exit
	fi
	if [ -f /usr/lib/lpsched ]; then
		if  [ "`/usr/bin/lpstat -r`" != "scheduler is running" ]; then
			echo $0: no lpsched running printers not installed
			exit
		fi
	fi
	;;

# Stopping the printers seems to cuase lpsched to crash at restart.
'halt')
	if  [ "`/usr/bin/lpstat -r`" != "scheduler is running" ]; then
		echo $0: no lpsched running printers not stopped
		exit
	fi
	state=stop
	;;
*)
	exit
	;;
esac


if [ ! -f $configured ]; then
	chown lp /var/lp/logs/*
	lpsystem -r +
	install_filters
	setup_network_access
	#sh /etc/init.d/lp stop
	#sleep 15
	#sh /etc/init.d/lp start
fi


# Strange redirections because we don't want the loop to run in
# a sub shell (for printer_list).
rm -f /tmp/printcap.$$
egrep -h -v '^#|^[       ]*$' $printcap > /tmp/printcap.$$
exec 3<&0 < /tmp/printcap.$$
rm -f /tmp/printcap.$$
while read line; do
	oldIFS=$IFS
	IFS=:
	for i in $line; do
		pc_printer=$i
		break
	done
	for i in BEGIN $line END; do
		IFS=$oldIFS
		case $i in
			*=*)
				eval pc_$i
				;;
			*#*)
				eval `echo pc_$i|sed 's/#/=/'`
				;;
			??)
				eval pc_$i=true
				;;
			END)
				if sanity; then 
					if [ ! -f $configured ]; then
						install_printer
					fi
					startstop $state 
				else
					echo configuration for printer \
					$pc_printer is incorrect. NOT \
					installed
				fi
				;;
			BEGIN)
				pc_pd=
				pc_rp=$pc_printer
				pc_pt=
				pc_ct=
				pc_ro=bsd
				pc_rm=
				pc_af=
				pc_if=
				pc_lp=
				pc_mc=1
				pc_pl=66
				pc_pw=80

				;;
			*)
				;;
		esac
	done
done
exec 0<&3 3<&-
if [ ! -f $configured ]; then
    #sh /etc/init.d/lp stop
    #sleep 15
    #sh /etc/init.d/lp start
    # Removing outdated printers.
    if [ -n "$printer_list" ]
    then
	eval "not_printer () { case \$1 in $printer_list) return 1;; *) return 0;; esac }"
	for p in `lpstat -p all | awk '{print $2}'`
	do
	    if not_printer $p
	    then
		$echo lpadmin -x $p
	    fi
	done

    fi
    $echo touch $configured
fi
