#!/bin/bash
#
# mydns           This shell script takes care of starting and stopping
#                 mydns (DNS server).
#
# chkconfig: 2345 43 43
# description: MyDNS is a Domain Name Server (DNS) \
# that is used to resolve host names to IP addresses.
# probe: true

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

# Source networking configuration.
[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

RETVAL=0
prog="mydns"

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

[ -x /usr/local/cpanel/3rdparty/sbin/mydns ] || exit 0

[ -r /etc/mydns.conf ] || exit 0

start() {
        # Start daemons.
        if [ -n "`/sbin/pidof mydns`" ]; then
                echo -n $"$prog: already running"
                return 1
        fi
        echo -n $"Starting $prog: "
        /usr/local/cpanel/3rdparty/sbin/mydns --background
        RETVAL=$?;
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mydns
        echo
        return $RETVAL
}
stop() {
        # Stop daemons.
        echo -n $"Stopping $prog: "
        killproc mydns
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mydns
        echo
        return $RETVAL
}
rhstatus() {
        echo -n $"Reloading $prog: "
        p=`/sbin/pidof -o %PPID mydns`
        RETVAL=$?
        if [ "$RETVAL" -eq 0 ]; then
            /usr/bin/kill -USR1 $p;
            RETVAL=$?
            if [ "$RETVAL" -eq 0 ]; then
		/usr/bin/kill -USR2 $p;
		RETVAL=$?
	    fi
        fi
        echo
        return $RETVAL
}
restart() {
        stop
        # wait a couple of seconds for the named to finish closing down
        sleep 2
        start
}
reload() {
        echo -n $"Reloading $prog: "
        p=`/sbin/pidof -o %PPID mydns`
        RETVAL=$?
        if [ "$RETVAL" -eq 0 ]; then
            /usr/bin/kill -HUP $p;
            RETVAL=$?
        fi
        [ "$RETVAL" -eq 0 ] && success $"$prog reload" || failure $"$prog reload"
        echo
        return $RETVAL
}
probe() {
        # named knows how to reload intelligently; we don't want linuxconf
        # to offer to restart every time
        p=`/sbin/pidof -o %PPID mydns`
        RETVAL=$?
        if [ "$RETVAL" -eq 0 ]; then
            /usr/bin/kill -HUP $p;
            RETVAL=$?
        fi
        return $RETVAL
}

# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        status)
                rhstatus
                ;;
        restart)
                restart
                ;;
        condrestart)
                if [ -e /var/lock/subsys/mydns ]; then restart; fi
                ;;
        reload)
                reload
                ;;
        probe)
                probe
                ;;
        *)
                echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|probe}"
                exit 1
esac

exit $?
