#! /bin/sh
#
#  lprng	Script to start and stop lprng print daemon
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
#
# This file was automatically customized by dh-make on Fri,  9 Jul 1999 09:47:22 +1000

# Have to cd to / because lpd doesn't Bug #42068
cd /

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/lpd
NAME=lprng
DESC="printer spooler"
CONF=/etc/printcap
DEFAULT=/etc/default/lprng

# Check for printcap file, if there isn't one there bomb out
test -f $CONF || exit 0

# Check for default file, if it is not there or it says no start
# then bomb out
test -f $DEFAULT || exit 0
grep -q 'START_LPD=yes' $DEFAULT || exit 0
#
# The pidfile is made up of <lockfile>.<lpd_port>
# lockfile defaults to /var/run/lprng/lpd
# lpd_port defaults to printer

# Work out what port we are running on, Bug #97272
LPD_PORT=$(grep "^[[:space:]]*lpd_port" /etc/lprng/lpd.conf | cut -d "=" -f 2)
if [ -z $LPD_PORT ]
then
  LPD_PORT=515
fi

# Check lpd.conf for lockfile, Bug #44953, now in /etc/lprng Bug #66568
LOCKFILE=$(grep "^[[:space:]]*lockfile" /etc/lprng/lpd.conf | cut -d "=" -f 2)
if [ -z $LOCKFILE ]
then
  LOCKFILE=/var/run/lprng/lpd
fi

PIDFILE=$LOCKFILE.$LPD_PORT



cleanup()
# description:
#   Removes all lock and control files on this host.
{
  rm -f "${PIDFILE}"

  for dir in $(find /var/spool/lpd/* -type d -print)
  do
    printer=${dir##*/}

    rm -f ${dir}/${printer}
    rm -f ${dir}/control.${printer}
    rm -f ${dir}/unspooler.${printer}
  done
}

initialize()
{
   checkpc -f > /dev/null
}
			
test -f $DAEMON -a -d /usr/share/doc/lprng || exit 0

set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start-stop-daemon --start --quiet --oknodo --pidfile "${PIDFILE}" \
		--exec $DAEMON
	initialize
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --quiet --oknodo --pidfile "${PIDFILE}"
	cleanup
	echo "$NAME."
	;;
  reload)
	#
	#	If the daemon can reload its config files on the fly
	#	for example by sending it SIGHUP, do it here.
	#
	#	If the daemon responds to changes in its config file
	#	directly anyway, make this a do-nothing entry.
	#
	 echo "Reloading $DESC configuration files."
	 start-stop-daemon --stop --signal 1 --quiet --pidfile \
		"${PIDFILE}" --oknodo 
  	;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: "
	start-stop-daemon --stop --quiet --pidfile "${PIDFILE}" 
	sleep 1
	start-stop-daemon --start --quiet --pidfile "${PIDFILE}" \
	    --exec $DAEMON
	initialize
	echo "$NAME."
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
