#!/bin/sh
#
# powerdns This shell script takes care of starting and stopping powerdns.
#
# chkconfig: 345 80 75
# description: PowerDNS is a versatile high performance \
# authoritative nameserver
# probe: true
# processname: powerdns
# pidfile: /var/run/powerdns/powerdns.pid
# config: /etc/powerdns/pdns.conf

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

# Source networking configuration.
. /etc/sysconfig/network

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

[ -f /etc/powerdns/pdns.conf ] || exit 0

[ -f /usr/sbin/pdns_server ] || exit 0

prefix=/usr
exec_prefix=/usr
BINARYPATH=/usr/bin
SBINARYPATH=/usr/sbin
SOCKETPATH=/var/run/powerdns

[ -f "$SBINARYPATH/pdns_server" ] || exit 0

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

pdns_server="$SBINARYPATH/pdns_server $EXTRAOPTS"

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

doPC ping
NOTRUNNING=$?

SERVICE="PowerDNS"

case "$1" in
	status)
		if test "$NOTRUNNING" = "0"
		then
			doPC status
			echo $ret
		else
			msg_not_running $SERVICE
			passed
			printf "\n"
		fi 
	;;

	stop)
		msg_stopping $SERVICE
		if test "$NOTRUNNING" = "0"
		then
			doPC quit
			if test "$?" = "0"
			then
				success
			else
				failture
			fi
			printf "\n"
			echo $ret
		else
			msg_not_running $SERVICE
			passed
			printf "\n"
		fi
	;;


	force-stop)
		killall -v -9 pdns_server
		if test "$?" = "0"
		then
			msg_stopping $SERVICE
			success
		else
			msg_stopping $SERVICE
			failure
		fi
		printf "\n"
	;;

	start)
		msg_starting $SERVICE
		if test "$NOTRUNNING" = "0"
		then
			msg_already_running $SERVICE
			passed
		else
			$pdns_server --daemon --guardian=yes
			if test "$?" = "0"
			then
				success
			else
				failure
			fi
		fi
		printf "\n"
	;;

	force-reload | restart)
		echo -n "Restarting PowerDNS service: "
		echo -n stopping and waiting...
		doPC quit
		sleep 3
		success
		printf "\n"
		$0 start
	;;

	reload)
		msg_reloading $SERVICE
		if test "$NOTRUNNING" = "0"
		then
			doPC cycle
			if test "$?" = "0"
			then
				success
			else
				failure
			fi
			printf "\n"
		else
			msg_not_running $SERVICE
			passed
			printf "\n"
			$0 start
		fi
	;;

	condstop)
		test "$NOTRUNNING" = "0" && $0 stop ;;

	condrestart)
		test "$NOTRUNNING" = "0" && $0 restart ;;

	condreload)
		test "$NOTRUNNING" = "0" && $0 reload ;;

	monitor)
		if test "$NOTRUNNING" = "0"
		then
			msg_already_running $SERVICE
			passed
		else
			$pdns_server --daemon=no --guardian=no --control-console --loglevel=9
			if test "$?" = "0"
			then
				success
			else
				failure
			fi
		fi
		printf "\n"
	;;

	dump)
		if test "$NOTRUNNING" = "0"
		then
			doPC list
			echo $ret
		else
			msg_not_running $SERVICE
			passed
			printf "\n"
		fi
	;;

	show)
		if [ $# -lt 2 ]
		then
			echo -n "Insufficient parameters"
			passed
			printf "\n"
			exit
		fi
		if test "$NOTRUNNING" = "0"
		then
			echo -n "$2="
			doPC show $2 ; echo $ret
		else
			msg_not_running $SERVICE
			passed
			printf "\n"
		fi
	;;

	mrtg)
		if [ $# -lt 2 ]
		then
			echo Insufficient parameters
			exit
		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 $SERVICE
			passed
			printf "\n"
		fi
	;;

	cricket)
		if [ $# -lt 2 ]
		then
			echo Insufficient parameters
			exit
		fi
		if test "$NOTRUNNING" = "0"
		then
			doPC show $2 ; echo $ret
		else
			msg_not_running $SERVICE
			passed
			printf "\n"
		fi
	;;



	*)
	echo pdns [start\|stop\|force-reload\|restart\|status\|dump\|show\|mrtg\|cricket\|monitor]

	;;
esac


