#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/lpd
NAME=lprng
DESC="LPRNG printer spooler"
CONF=/etc/printcap
DEFAULT=/etc/default/lprng
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: Start lpd to allow printing
# Description: lpd is the print daemon required for lpr to work properly.
#   It is basically a server that arbitrates print jobs to printer(s).
### END INIT INFO
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#

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

# 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

# Since /var/run can be a tmpfs, we need to make sure that /var/run/lprng exists
[ ! -d /var/run/lprng ] && mkdir /var/run/lprng

# 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

# Source gLSB script
. /lib/lsb/init-functions


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
}

# Dont start if there is no lpd or that lib dir is not there (uninstalled but
# not purged
test -f $DAEMON -a -d /usr/lib/lprng || exit 0

set -e

case "$1" in
  start)
	log_daemon_msg "Starting $DESC" lpd
	if start-stop-daemon --start --quiet --pidfile "${PIDFILE}" \
		--exec $DAEMON ; then
		initialize
		log_end_msg 0
	else
		log_end_msg 1
		exit 1
	fi
	;;
  stop)
	log_daemon_msg "Stopping $DESC" lpd
	if start-stop-daemon --stop --quiet --pidfile "${PIDFILE}" ; then
		cleanup
		log_end_msg 0
	else
		log_end_msg 1
		exit 1
	fi
	;;
  reload)
	 log_daemon_msg "Reloading $DESC configuration files..."
	 if start-stop-daemon --stop --signal 1 --quiet --pidfile \
		"${PIDFILE}" --oknodo ; then
	 	log_end_msg 0
	else
		log_end_msg 1
		exit 1
	fi
  	;;
  restart|force-reload)
	log_daemon_msg "Restarting $DESC" lpd
	start-stop-daemon --stop --quiet --pidfile "${PIDFILE}" 
	sleep 1
	start-stop-daemon --start --quiet --pidfile "${PIDFILE}" \
	    --exec $DAEMON
	initialize
	log_end_msg 0
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
