#!/bin/sh
# pdns		This is controller stript for PowerDNS name server.
#
# chkconfig:	345 85 15
#
# description:	pdns is is a Domain Name Server (DNS) \
#		that is used to resolve host names to IP addresses.

# Source function library
. /etc/rc.d/init.d/functions

# Source networking configuration
. /etc/sysconfig/network

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "PowerDNS"
		exit 1
	fi
else
	exit 0
fi

BINARYPATH=/usr/sbin
SBINARYPATH=/usr/sbin
SOCKETPATH=/var/run

cd /var/run
suffix=`basename $0 | awk -F- '{print $2}'`
if [ $suffix ]; then
	EXTRAOPTS=--config-name=$suffix
	PROGNAME=pdns-$suffix
else
	PROGNAME=pdns
fi

pdns_server="/usr/sbin/pdns_server $EXTRAOPTS"

doPC() {
	ret=$(/usr/bin/pdns_control $EXTRAOPTS $1 $2 2> /dev/null)
}

doPC ping
NOTRUNNING=$?

start() {
	if test "$NOTRUNNING" = "0"; then
		msg_already_running "PowerDNS"
		return
	fi

	msg_starting "PowerDNS (Powered DNS server)"
	daemon $pdns_server --daemon --guardian=yes
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/pdns
}

stop() {
	if test "$NOTRUNNING" != "0"; then
		msg_not_running "PowerDNS"
		return
	fi

	msg_stopping "PowerDNS"
	doPC quit 2>&1 >/dev/null
	[ $? -eq 0 ] && ok || fail
	rm /var/lock/subsys/pdns
}

restart() {
	stop
	run_cmd "PowerDNS (3 sec. timeout)" sleep 3
	start
}

reload() {
	if test "$NOTRUNNING" != "0"; then
		msg_not_running "PowerDNS" >&2
		RETVAL=7
		return
	fi

	show "Reloading PowerDNS"
	doPC cycle 2>&1 >/dev/null
	if [ $? -eq 0 ]; then
		ok
	else
		fail
		RETVAL=1
	fi
}

monitor() {
	if test "$NOTRUNNING" = "0"; then
		msg_already_running "PowerDNS"
		return
	fi

	show "PowerDNS in foreground (testing mode)"
	$pdns_server --daemon=no --guardian=no --control-console --loglevel=9
}

dump() {
	if test "$NOTRUNNING" != "0"; then
		msg_not_running "PowerDNS"
		return
	fi

	doPC list
	echo $ret
}

show() {
	if [ $# -lt 2 ]; then
		msg_usage "$0 {show|mrtg|cricket} <variable_name_from_pdns_dump>"
		RETVAL=2
		return
	fi

	if test "$NOTRUNNING" = "0"; then
		echo -n "$2="
		doPC show $2 ; echo $ret
	else
		msg_not_running "PowerDNS"
	fi
}

mrtg() {
	if [ $# -lt 2 ]; then
		msg_usage "$0 {show|mrtg|cricket} <variable_name_from_pdns_dump>"
		RETVAL=2
	fi

	if test "$NOTRUNNING" = "0"; then
		doPC show $2 ; echo $ret
		if [ "$3x" != "x" ]; then
			doPC show $3 ; echo $ret
		else
			echo 0
		fi
		doPC uptime ; echo $ret
		echo PowerDNS daemon
	else
		msg_not_running "PowerDNS" >&2
		exit 7
	fi
}

cricket() {
	if [ $# -lt 2 ]; then
		msg_usage "$0 {show|mrtg|cricket} <variable_name_from_pdns_dump>"
		RETVAL=2
	fi

	if test "$NOTRUNNING" = "0"; then
		doPC show $2 ; echo $ret
	else
		msg_not_running "PowerDNS" >&2
		exit 7
	fi
}

RETVAL=0
case "$1" in
  stop)
	stop
	;;
  force-stop)
	run_cmd "Killing PowerDNS without grace" killall -v -9 pdns_server
	;;
  start)
	start
	;;
  restart)
	restart
	;;
  reload|force-reload)
	reload
	;;
  monitor)
	monitor
	;;
  dump)
	dump
	;;
  show)
	show "$@"
	;;
  mrtg)
	mrtg "$@"
	;;
  cricket)
	cricket "$@"
	;;
  status)
	status pdns_server
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status|dump|show|mrtg|cricket|monitor} [...]"
	exit 3
esac

exit $RETVAL
