#! /bin/sh
# rbldnsd	startup script.
#
# chkconfig:	345 80 30
# description:	rbldnsd is a DNS daemon for DNSBLs. \
#				Configure it in /etc/sysconfig/rbldnsd

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

# Get network config
. /etc/sysconfig/network

# Check that networking is up
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
		msg_network_down rbldnsd
		exit 1
	fi
else
	exit 0
fi

NAME=rbldnsd
RBLDNSD=

if [ -f /etc/sysconfig/rbldnsd ]; then
  . /etc/sysconfig/rbldnsd
else
  exit 0
fi

test -n "$RBLDNSD" || exit 0

forall() {
	RETVAL=0

	# FIXME: get rid of subshell to have exit codes proper for $RETVAL
	echo "$RBLDNSD" |
	while read name args; do
		case "$name" in
		''|'#'*)
			continue
			;;
		-)
			name=$NAME; pidfile=/var/run/$name.pid
			;;
		*)
			pidfile=/var/run/rbldnsd-$name.pid;;
		esac

		pid=
		if [ -f $pidfile ]; then
			read p < $pidfile
			if [ -n "$p" -a -f /proc/$p/cmdline ]; then
				case "`cat /proc/$p/cmdline 2>/dev/null`" in
				*$NAME*)
					pid=$p
					;;
				esac
			fi
		fi
		# call subroutine
		$1
		RETVAL=$?
		[ $? != 0 ] && RETVAL=$?
	done

	return $RETVAL
}

runit() {
	/usr/sbin/rbldnsd -q -p $pidfile $args
}

start() {
	if [ ! "$pid" ]; then
		msg_starting "rbldnsd $name"
		runit
		RETVAL=$?
		[ "$RETVAL" = 0 ] && ok || fail
	else
		msg_already_running "rbldnsd $name"
	fi

	return $RETVAL
}

stop() {
	if [ "$pid" ]; then
		msg_stopping "rbldnsd $name"
		kill $pid
		rm -f $pidfile
		RETVAL=$?
		[ "$RETVAL" = 0 ] && ok || fail
	else
		msg_not_running "rbldnsd $name"
	fi

	return $RETVAL
}

restart() {
	if [ "$pid" ]; then
		msg_stopping "rbldnsd $name"
		kill $pid
		[ "$RETVAL" = 0 ] && ok || fail
		RETVAL=$?
		msg_starting "rbldnsd $name"
		runit
		[ "$RETVAL" != 0 ] && RETVAL=$?
		[ "$RETVAL" = 0 ] && ok || fail
	else
		start
	fi

	return $RETVAL
}

reload() {
	if [ "$pid" ]; then
		msg_reloading "rbldnsd $name"
		kill -HUP $pid
		RETVAL=$?
		[ "$RETVAL" = 0 ] && ok || fail
	fi

	return $RETVAL
}

RETVAL=0
# See how we were called.
case "$1" in
start|restart)
	forall $1
	RETVAL=$?
	[ "$RETVAL" = 0 ] && touch /var/lock/subsys/rbldnsd
	;;
stop)
	forall $1
	RETVAL=$?
	[ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/rbldnsd
	;;
reload|force-reload)
	forall reload
	RETVAL=$?
	;;
status)
	status rbldnsd
	RETVAL=$?
	;;
*)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
	;;
esac

exit $RETVAL
# vim:ts=4:sw=4
