#!/bin/sh
#
# knot         Knot DNS server daemon
#
# chkconfig:   - 56 24
# description: Knot DNS is a high-performance authoritative DNS server implementation.
# processname: knotd
# config:      /etc/knot/knot.conf
# pidfile:     /var/run/knot/knot.pid
#

### BEGIN INIT INFO
# Provides: knot knotd
# Required-Start: $network $local_fs
# Required-Stop: $network $local_fs
# Should-Start:
# Should-Stop:
# Default-Start:
# Default-Stop:
# Short-Description: Knot DNS server daemon
# Description:
### END INIT INFO

. /etc/init.d/functions

exec_knotd="/usr/sbin/knotd"
exec_knotc="/usr/sbin/knotc"
prog="knot"
config="/etc/knot/knot.conf"
pidfile="/var/run/knot/knot.pid"

#[ -f /etc/sysconfig/knot ] && . /etc/sysconfig/knot

lockfile="/var/lock/subsys/$prog"

# check installation
[ -x $exec_knotc ] || exit 5
[ -x $exec_knotd ] || exit 5
[ -f $config ] || exit 6

checkconf_silent () {
    check_output=`$exec_knotc -c $config checkconf 2>&1`
    if [ $? -ne 0 ]; then
        failure; echo
        echo "$check_output"
        exit 6
    fi
}

checkconf () {
    printf $"Checking configuration file for %s: " $prog
    checkconf_silent
    success; echo
}

start () {
    printf $"Starting %s: " $prog
    checkconf_silent
    daemon $exec_knotd -c $config -d
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop () {
    printf $"Stopping %s: " $prog
    killproc $exec_knotd
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
}

restart() {
    stop
    start
}

reload() {
    printf $"Reloading %s: " $prog
    checkconf_silent
    $exec_knotc -c $config reload &>/dev/null
    retval=$?
    [ $retval -eq 0 ] && success || failure
    echo
    return $retval
}

rh_status() {
    status -p $pidfile $prog
}

rh_status_q() {
    rh_status &>/dev/null
}

usage() {
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|checkconf}"
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        restart
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    checkconf)
        $1
        ;;
    usage)
        usage
        exit 0
        ;;
    *)
        usage
        exit 2
esac
exit $?
