#!/sbin/openrc-run

instance_name="${RC_SVCNAME#dnsdist[.-]}"
if [[ "$instance_name" != "$RC_SVCNAME" ]]; then
	config_file="${DNSDIST_CONF:-/etc/dnsdist-$instance_name.conf}"
else
	config_file="${DNSDIST_CONF:-/etc/dnsdist.conf}"
        instance_name="default"
fi

name="dnsdist ($instance_name)"
supervisor="supervise-daemon"
command="/usr/bin/dnsdist"
command_args="--supervised -C $config_file"
extra_commands="checkconfig"

depend() {
	use net
	after firewall
}

start_pre() {
	if [[ "${RC_CMD}" != "restart" ]]; then
		checkconfig
	fi
}

stop_pre() {
	if [[ "${RC_CMD}" == "restart" ]]; then
		checkconfig
	fi
}

checkconfig() {
	ebegin "Checking dnsdist ($instance_name) config $config_file"
	"$command" --check-config -C "$config_file"
	eend "$?"
}
