#!/bin/bash
#
# description: named daemon
# chkconfig: - 25 80
              
pidFile=/usr/local/bind9/var/run/named.pid
lockFile=/var/lock/subsys/named
confFile=/etc/named/named.conf
[ -r /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions
start() {
    if [ -e $lockFile ]; then
        echo "named is already running..."
        exit 0
    fi
    echo -n "Starting named:"
    daemon --pidfile "$pidFile" /usr/local/bind9/sbin/named -u named -c "$confFile"
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
      touch $lockFile
      return $RETVAL
    else
      rm -f $lockFile $pidFile
      return 1
    fi
}
stop() {
    if [ ! -e $lockFile ]; then
      echo "named is stopped."
    fi
    echo -n "Stopping named:"
    killproc named
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ];then
       rm -f $lockFile $pidFile
       return 0
    else
       echo "Cannot stop named."
       failure
       return 1
    fi
}
restart() {
    stop
    sleep 2
    start
}
reload() {
    echo -n "Reloading named: "
    killproc named -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
status() {
    if pidof named &> /dev/null; then
          echo -n "named is running..."
      success
      echo
    else
      echo -n "named is stopped..."
      success
          echo
         fi
}
usage() {
    echo "Usage: named {start|stop|restart|status|reload}"
}
case $1 in
start)
    start ;;
stop)
    stop ;;
restart)
    restart ;;
status)
    status ;;
reload)
    reload ;;
*)
    usage
    exit 4
    ;; 
esac