#!/bin/sh
#
# unbound		This shell script takes care of starting and stopping
#		unbound (DNS server).
#
# chkconfig:	345 14 89
#
# description:	unbound (BIND) 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

UNBOUND_OPT=""

# Try get config..
[ -f /etc/sysconfig/unbound ] && . /etc/sysconfig/unbound

# 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 "Unbound"
		exit 1
	fi
else
	exit 0
fi

# Sanity check
[ -e /etc/unbound/unbound.conf ] || exit 0

start() {
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/unbound ]; then
		msg_starting "Unbound"

		# prepare the root key file
		/usr/sbin/unbound-anchor -v -a /var/lib/unbound/root.key > /dev/null

		daemon /usr/sbin/unbound \
			-c /etc/unbound/unbound.conf $UNBOUND_OPT </dev/null
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/unbound
	else
		msg_already_running "Unbound"
	fi
}

stop() {
	if [ -f /var/lock/subsys/unbound ]; then
		msg_stopping "Unbound"
		killproc unbound
		rm -f /var/lock/subsys/unbound >/dev/null 2>&1
	else
		msg_not_running "Unbound"
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  status)
	status unbound
	;;
  reload|force-reload)
	if [ -f /var/lock/subsys/unbound ]; then
		msg_reloading "Unbound"
		killproc unbound -HUP
	else
		msg_not_running "Unbound"
		exit 7
	fi
	;;
  restart)
	stop
	start
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL
