#!/bin/sh 
#
# /etc/init.d/dns
# ln -s /etc/init.d/dns /etc/rc2.d/S50dns
# ln -s /etc/init.d/dns /etc/rc2.d/K50dns
#
# Start and stop chroot'ed BIND DNS server
#                                                   Sean / 16.Jun.02


# Paths to key files:
root='/dns';
named="/usr/local/sbin/named";   # relative to $root
conf="$root/etc/named.conf";
pid="$root/var/run/named.pid";

case $1 in

'start')
        # Mount device on loopback filesystem
	umount /dns/dev/random 2>/dev/null
        mkdir /dns/dev/random 2>/dev/null
	mount -F lofs /dev/random /dns/dev/random

        # make sure it's dead first
        [ -f $pid ] && kill `cat $pid` >/dev/null 2>&1

        echo "Starting BIND dns name server `date`. . . \c"
	if [ -f $root/$named -a -f $conf ]; then
          (umask 027; /usr/sbin/chroot $root $named -u named)
	  sleep 1
          if [ "$?" -ne 0 ]; then
            echo "Warning: BIND has not started"
	  elif [ ! -f $pid ]; then
            echo "Warning: BIND pid file $pid missing."
          else
            echo "running with pid `cat $pid`".
          fi
        fi
        ;;
'stop')
        echo "Stopping BIND `date`. . .\c"
        kill `cat $pid`
	if [ "$?" -ne 0 ]; then
          echo "Warning: BIND not killed"
        else
          echo "done."
        fi
        ;;
'restart')
        echo "Restarting BIND `date`. . .\c" 
	$0 stop
	sleep 1
	$0 start
        ;;
'reload')
        echo "Reloading BIND `date`. . .\c" 
        kill -1 `cat $pid`
	if [ "$?" -ne 0 ]; then
          echo "Warning: BIND not reloaded"
        else
          echo "HUP send to PID `cat $pid`".
        fi
        ;;
*)
        echo "Usage: $0 { start | stop | restart | reload}"
        ;;
esac

