#!/bin/bash
#
# eximqd	Start/Stop eximq supervisor
#
# chkconfig:	345 81 29
# description:	eximq - exim queue runners supervisor
#
# processname:	eximq

# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

EXIMQ_CONFIG=/etc/mail/eximq.args

# Get service config
[ -f /etc/sysconfig/exim ] && . /etc/sysconfig/exim
[ -f /etc/sysconfig/eximq ] && . /etc/sysconfig/eximq

if [ -n "$QUEUE" ]; then
	echo "eximq requires QUEUE in /etc/sysconfig/exim to be empty" >&2
	exit 1
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/eximq ]; then
		n=0
		grep -v -e '^[[:space:]]*$' -e '^#' $EXIMQ_CONFIG | while read args; do
			n=$(printf '%02d' $(($n+1)))
			msg_starting "eximq $n daemon"
			daemon --user exim /usr/sbin/eximq.pl --pidfile /var/run/eximq/eximq.$n.pid --daemon $args
			RETVAL=$?
		done
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/eximq
	else
		msg_already_running eximq
	fi
	;;
  stop)
	# Stop daemons.
	if [ -f /var/lock/subsys/eximq ]; then
		n=0
		grep -v -e '^[[:space:]]*$' -e '^#' $EXIMQ_CONFIG | while read args; do
			n=$(printf '%02d' $(($n+1)))
			msg_stopping "eximq $n daemon"
			killproc --pidfile /var/run/eximq/eximq.$n.pid eximq.pl
		done
		rm -f /var/lock/subsys/eximq >/dev/null 2>&1
	else
		msg_not_running eximq
	fi
	;;
  restart)
	$0 stop
	$0 start
	exit $?
	;;
  status)
	status eximq.pl
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|status}"
	exit 3
esac

exit $RETVAL
