#! /bin/sh
#
# Debian SysV init script for printbill - put it in /etc/init.d/ and make a
# symbolic link as follows: 
#
# cd /etc/rc2.d ; ln -sf ../init.d/printbilld S19printbilld
#
# On Debian systems this will start it just before lprng
# so the socket is read and waiting when lprng starts.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/printbilld
NAME=printbilld
DESC="printbill daemon"
CONF=/etc/printbill/printbillrc
PRINTCAP=/etc/printcap

test -x $DAEMON || exit 0

if [ ! -e $CONF ] ; then
	echo "error: printbill not configured yet - run printbill_configure"
	exit 0
fi

if [ ! -e $PRINTCAP ] ; then
	echo "warning: printcap not found, but starting printbill daemon anyway"
else
	grep 'printbill_scheduler|printbill_accountonly|printbill_quote|printbill_lazybill' $PRINTCAP &> /dev/null

	if [ $? = 0 ] ; then
		echo "warning: obsolete entry in $PRINTCAP"
	fi
	
	grep 'printbill --type' $PRINTCAP &> /dev/null
	
	if [ $? != 0 ] ; then
		echo "warning: no printbill lines in $PRINTCAP, starting printbill daemon anyway"
	fi
fi

DB_HOME=`grep db_home $CONF | head -1 | awk '{print $2}'`

if [ ! -d $DB_HOME ] ; then
	echo "error: no $DB_HOME - run pqm --init to set this up"
	exit 0;
fi

set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start-stop-daemon --stop --quiet --name printbilld || true
	start-stop-daemon --start --quiet --name printbilld --exec $DAEMON
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --quiet --name printbilld || true
	echo "$NAME."
	;;
  reload|force-reload)
	echo "Reloading $DESC configuration files."
	start-stop-daemon --stop --signal 1 --quiet --name printbilld  --exec $DAEMON
  	;;
  restart)
	echo -n "Restarting $DESC: "
	start-stop-daemon --stop --quiet --name printbilld --exec $DAEMON
	sleep 1
	start-stop-daemon --start --quiet --name printbilld --exec $DAEMON
	echo "$NAME."
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
