#!/bin/sh

#
# This script was written and contributed by Markus Mohr, and
# slightly modified by me for version 1.0.6 (which obviously
# broke it some way). I then applied a set of corrections
# by Markus Mohr.
#
# Carsten Block has patched this with some magic so that
# the actual script is generated by configure, and that
# the pdnsd user is determined from pdnsd.conf
# I changed this a little to use the --pdnsd-user option
# of pdnsd to determine the run_as user.
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin

test -x @prefix@/sbin/pdnsd || exit 0

case "$1" in
    start)
    	# Check if cache dir exists and recreate if neccessary
	test -d @cachedir@ || mkdir @cachedir@
	RUNAS=`@prefix@/sbin/pdnsd --pdnsd-user` || echo -n " failed"
	[ -z "$RUNAS" ] && RUNAS=nobody
	chown $RUNAS @cachedir@
	echo -n "Starting domain name service: pdnsd"
	start-stop-daemon --start --quiet --pidfile /var/run/pdnsd.pid --name pdnsd \
	--exec @prefix@/sbin/pdnsd -- --daemon -p /var/run/pdnsd.pid \
	|| echo -n " failed"
	echo "."
    ;;

    stop)
	echo -n "Stopping domain name service: pdnsd"
	start-stop-daemon --stop --quiet  --pidfile /var/run/pdnsd.pid --name pdnsd --exec @prefix@/sbin/pdnsd \
	|| echo -n " failed"
	echo "."
    ;;

    restart)
        $0 stop
	$0 start
    ;;
    
    *)
	echo "Usage: /etc/init.d/pdnsd {start|stop|restart}" >&2
	exit 1
    ;;
esac

exit 0
