#!/bin/sh
#
# rbldnsd      This starts and stops rbldnsd.
#
# chkconfig: - 80 30
# description: rbldnsd is a DNS daemon for DNSBLs. Configure it in \
#              /etc/sysconfig/rbldnsd
#
# processname: /usr/sbin/rbldnsd
# config: /etc/sysconfig/network
# config: /etc/sysconfig/rbldnsd
# pidfile: /var/run/rbldnsd*.pid
#
### BEGIN INIT INFO
# Provides: rbldnsd
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: rbldnsd is a DNS daemon for DNSBLs.
# Short-Description: start and stop rbldnsd
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

prog=rbldnsd
DAEMON=/usr/sbin/$prog
PIDFILE=/var/run/$prog.pid
LOCKFILE=/var/lock/subsys/$prog

# Get config.
SourceIfNotEmpty /etc/sysconfig/rbldnsd

# Check that the rbldnsd binary is available
[ -f $DAEMON ] || exit 5

# Check that configuration has been set up (RBLDNSD set in /etc/sysconfig/rbldnsd)
[ -n "$RBLDNSD" ] || exit 6

RETVAL=0

# Process multiple instances of the daemon (see /etc/sysconfig/rbldnsd)
for_all_daemons() {
	local ret=0
	name=
	args=
	echo "$RBLDNSD" |
		while read name args; do
			# generate pidfile name from key
			case "$name" in
				""|\#*)	continue;;
				-) name=$prog; pidfile=/var/run/$name.pid;;
				*) pidfile=/var/run/rbldnsd-$name.pid;;
			esac
			# if pidfile exists, get pid and check for running rbldnsd
			pid=
			if [ -f $pidfile ]; then
				read p < $pidfile
				if [ -n "$p" -a -f /proc/$p/cmdline ]; then
					case "$(tr -d '\0' /proc/$p/cmdline 2>/dev/null)" in
						*$prog*) pid=$p;;
					esac
				fi
			fi
			# Start/stop this daemon
			$1
		done
}

start_one_daemon() 
{
	if [ ! "$pid" ]; then
		[ x"$name" != x"$prog" ] && echo -n "[$name] "
		start_daemon --pidfile "$pidfile" --lockfile "$LOCKFILE" --expect-user rbldns -- $DAEMON -p "$pidfile" "$args"
		RETVAL=$?
		return $RETVAL
	fi
}

stop_one_daemon() {
	if [ "$pid" ]; then
		stop_daemon --pidfile "$pidfile" --lockfile "$LOCKFILE" --expect-user rbldns -- $prog
		usleep 500000
		RETVAL=$?
		return $RETVAL
	fi
}

reload_one_daemon() {
	if [ "$pid" ]; then
		msg_reloading $prog
		stop_daemon --pidfile "$pidfile" --expect-user rbldns -HUP -- $prog
		RETVAL=$?
		return $RETVAL
	else
		# Not running
		return 7
	fi
}

check_one_daemon() {
	echo -n "$prog "
	[ x"$name" != x"$prog" ] && echo -n "[$name] "
	if [ "$pid" ]; then
		echo $"($pid) is running..."
		return 0
	fi
	if [ -f "$pidfile" ]; then
		echo $"dead but pid file exists"
		return 1
	fi
	if [ -f /var/lock/subsys/$prog ]; then
		echo $"dead but subsys locked"
		return 2
	fi
	echo $"is stopped"
	return 3
}

restart_one_daemon() {
	stop_one_daemon
	pid=
	start_one_daemon
}

condrestart_one_daemon() {
	if [ -f /var/lock/subsys/$prog ]; then
		restart_one_daemon
	else
		return 0
	fi
}

condstart_one_daemon() {
	if [ ! -f /var/lock/subsys/$prog ]; then
		start_one_daemon
	else
		return 0
	fi
}

condstop_one_daemon() {
	if [ -f /var/lock/subsys/$prog ]; then
		stop_one_daemon
	else
		return 0
	fi
}

# See how we were called.
case "$1" in
	start)
		for_all_daemons start_one_daemon
		;;
	restart)
		for_all_daemons restart_one_daemon
		;;
	force-reload|reload)
		for_all_daemons reload_one_daemon
		;;
	stop)
		for_all_daemons stop_one_daemon
		;;
	status)
		for_all_daemons check_one_daemon
		RETVAL=$?
		;;
	condstop)
		for_all_daemons condstop_one_daemon
		;;
	condstart)
		for_all_daemons condstart_one_daemon
		;;
	condrestart|try-restart)
		for_all_daemons condrestart_one_daemon
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|try-restart|reload|force-reload|status}" >&2
		RETVAL=1
		;;
esac

exit $RETVAL
