#!/bin/sh

### BEGIN INIT INFO
# Provides:          mysqmail-courier-logger
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      mysql
# Should-Stop:       mysql
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: MySQL accouting for courier-imap and courier-pop
# Description:       This is a (ver small) traffic logger for the C courier-imap,
#                    courier-imaps, courier-pop and courier-pops daemons to SQL,
#                    splitting the information by domains and users, so it's then very
#                    easy to count all the pop and imap traffic for a given
#                    domain name.
### END INIT INFO

PATH=$PATH:/bin:/usr/bin:/usr/sbin
DESC="MySQMail courier logger"
NAME=mysqmail-courier-logger
DAEMON=/usr/sbin/${NAME}
PID_FILE=/var/run/${NAME}.pid
PATH_CONF_FILE=/etc/mysqmail.conf

if ! [ -f "${PATH_CONF_FILE}" ] ; then
	echo "${PATH_CONF_FILE} not present: exiting silently"
	exit 0
fi

if [ ! -x ${DAEMON} ] ; then
	echo "${DAEMON} not executable or not present!"
	exit 1
fi

. /lib/lsb/init-functions

RET=0
case "$1" in
start)
	if [ ! -f ${PID_FILE} ] ; then
		log_daemon_msg "Starting ${DESC}" "${NAME}"
		start-stop-daemon -S --quiet -p ${PID_FILE} --exec ${DAEMON}
		RET=$?
		log_end_msg $?
	else
		echo "${PID_FILE} already exists: not starting ${DAEMON}"
	fi
	exit ${RET}
;;
stop)
	if [ -f ${PID_FILE} ] ; then
		log_daemon_msg "Stopping ${DESC}" "${NAME}"
		start-stop-daemon -K --quiet -p ${PID_FILE}
		RET=$?
		log_end_msg $?
	else
		echo "${PID_FILE} doesn't exist: not stoping ${DAEMON}"
	fi
;;
restart|reload|force-reload)
	$0 stop
	$0 start
;;
*)
	echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
