#! /bin/bash
#
# chkconfig: 2345 20 50
# description: unbound
# processname: unbound
# config: /etc/unbound/unbound.conf

### BEGIN INIT INFO
# Provides:          unbound
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: unbound
### END INIT INFO

UNBOUND_BIN=/usr/sbin/unbound

[ -x $UNBOUND_BIN ] || exit 0

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

RETVAL=0
prog=unbound
pidfile=/var/run/unbound.pid

start()
{
	gprintf "Starting %s: " "$prog"
	if [ -e "$pidfile" ]; then
		gprintf "already running.";
		success "%s is already running." "$prog";
		echo
		return 0
	fi
	daemon $UNBOUND_BIN -c /etc/unbound/unbound.conf
	RETVAL=$?
        touch /var/lock/subsys/unbound
        touch $pidfile
	echo
	return $RETVAL
}

stop()
{
	gprintf "Shutting down %s: " "$prog"
	killall $UNBOUND_BIN
	RETVAL=$?
	echo
	rm -f $pidfile
	rm -f /var/lock/subsys/unbound
	return $RETVAL
}


reload()
{
	gprintf "Reloading %s configuration: " "$prog"
	killproc $UNBOUND_BIN -HUP
	RETVAL=$?
	echo
	return $RETVAL
}

force_reload()
{
	pid=`pidofproc unbound`
	if [ -n "$pid" ]; then
		reload
	else
		start
	fi
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		stop
		start
		;;
	try-restart)
		if [ -f $pidfile ]; then
			stop
			start
		fi
		;;
	force-reload)
		force_reload
		;;
	status)
		status $prog
		RETVAL=$?
		;;
	*)
		gprintf "Usage: %s {start|stop|reload|force-reload|restart|try-restart|status}\n" "$0"
		RETVAL=3
esac

exit $RETVAL
