#! /bin/sh
# $Id: rbldnsd.init,v 1.6 2003/05/14 22:23:34 mjt Exp $
# rbldnsd	startup script.
#
# chkconfig:	345 80 30
# description:	rbldnsd is a DNS daemon for DNSBLs.  Configure it in \
#		/etc/default/rbldnsd or /etc/sysconfig/rbldnsd

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=rbldnsd
DESC=$NAME
DAEMON=/usr/sbin/$NAME

test -f $DAEMON || exit 0

set -e

RBLDNSD=

if [ -f /etc/default/$NAME ] ; then
  . /etc/default/$NAME
elif [ -f /etc/sysconfig/$NAME ]; then
  . /etc/sysconfig/$NAME
else
  exit 0
fi

test -n "$RBLDNSD" || exit 0

function forall() {
  echo "$RBLDNSD" |
    while read name args; do
      case "$name" in
        ""|\#*) continue;;
	-) name=rbldnsd; pidfile=/var/run/$name.pid;;
	*) pidfile=/var/run/rbldnsd-$name.pid;;
      esac
      pid=
      if [ -f $pidfile ]; then
        if read p < $pidfile; then
          if [ -d /proc/$p ]; then
            if [ "`readlink /proc/$p/exe`" = $DAEMON ]; then
              pid=$p
            fi
          fi
        fi
      fi
      $1
    done
}

function report() {
  echo "$1 $DESC: $name"
}

function runit() {
  $DAEMON -p $pidfile $args
}

function start() {
  if [ ! "$pid" ]; then
    report Starting
    runit
  fi
}

stop() {
  if [ "$pid" ]; then
    report Stopping
    kill $pid
    rm -f $pidfile
  fi
}

function restart() {
  if [ "$pid" ]; then
    report Restarting 
    kill $pid
    sleep 1
    runit
  else
    start
  fi
}

function reload() {
  if [ "$pid" ]; then
    report Reloading
    kill -HUP $pid
  fi
}

case "$1" in
  start|restart)
	forall $1
	if [ -d /var/lock/subsys ] ; then touch /var/lock/subsys/$NAME; fi
	;;
  stop)
	forall $1
	rm -f /var/lock/subsys/$NAME
	;;
  reload|force-reload)
	forall reload
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
