#!/bin/sh
#
# $FreeBSD$
#
# unbound freebsd startup rc.d script, modified from the named script.
# uses the default unbound installation path and pidfile location.
# copy this to /etc/rc.d/unbound
# and put unbound_enable="YES" into rc.conf
#
# unbound_anchorflags can be used to allow you to pass a custom flags to
# unbound-anchor.  Examples include a custom resolv.conf (-f) or a custom
# root.hints (-r).  Useful for when /etc/resolv.conf only contains 127.0.0.1

# PROVIDE: unbound
# REQUIRE: SERVERS cleanvar
# KEYWORD: shutdown

. /etc/rc.subr

name="unbound"
rcvar=unbound_enable

command="/usr/local/sbin/unbound"
extra_commands="reload"
start_precmd="start_precmd"
reload_precmd="/usr/local/sbin/unbound-checkconf ${unbound_conf} >/dev/null"

load_rc_config $name

pidfile=${unbound_pidfile:-"/usr/local/etc/unbound/unbound.pid"}
unbound_enable=${unbound_enable:-"NO"}
unbound_anchorflags=${unbound_anchorflags:-""}
unbound_conf=${unbound_conf:-"/usr/local/etc/unbound/unbound.conf"}
unbound_flags=${unbound_flags:-" -c ${unbound_conf}"}

start_precmd()
{
	echo -n "Obtaining a trust anchor:"
	if [ "${unbound_anchorflags}T" = "T" ]; then
		su -m unbound -c /usr/local/sbin/unbound-anchor
	else
		su -m unbound -c "/usr/local/sbin/unbound-anchor ${unbound_anchorflags}"
	fi
	echo .
	/usr/local/sbin/unbound-checkconf ${unbound_conf} > /dev/null
	return $?
}

run_rc_command "$1"
