#!/bin/sh
#
# $NetBSD: dnscache.sh,v 1.12 2019/01/08 18:19:27 schmonz Exp $
#
# djbdns-run-20250402 script to control dnscache (caching DNS resolver).
#

# PROVIDE: dnscache named
# REQUIRE: NETWORKING mountcritremote syslogd
# BEFORE:  DAEMON

name="dnscache"

# User-settable rc.conf variables and their default values:
: ${dnscache_postenv:=""}
: ${dnscache_ip:="127.0.0.1"}
: ${dnscache_ipsend:="0.0.0.0"}
: ${dnscache_size:="1000000"}
: ${dnscache_datalimit:="30000000"}
: ${dnscache_log:="YES"}
: ${dnscache_logcmd:="logger -t nbdjbdns/cache -p daemon.info"}
: ${dnscache_nologcmd:="/usr/pkg/bin/multilog -*"}

if [ -f /etc/rc.subr ]; then
	. /etc/rc.subr
fi

rcvar=${name}
required_dirs="/usr/pkg/etc/${name}/ip /usr/pkg/etc/${name}/servers"
required_files="/usr/pkg/etc/${name}/servers/@"
command="/usr/pkg/bin/${name}"
procname=nb${name}
start_precmd="dnscache_precmd"

dnscache_precmd() {
	if [ -f /etc/rc.subr ] && ! checkyesno dnscache_log; then
		dnscache_logcmd=${dnscache_nologcmd}
	fi
	command="/usr/pkg/bin/pgrphack /usr/bin/env - ${dnscache_postenv} \
ROOT=/usr/pkg/etc/${name} \
IP=${dnscache_ip} \
IPSEND=${dnscache_ipsend} \
CACHESIZE=${dnscache_size} \
/usr/pkg/bin/envuidgid dnscache \
/usr/pkg/bin/softlimit -o250 -d ${dnscache_datalimit} \
/usr/pkg/bin/argv0 /usr/pkg/bin/${name} ${procname} \
</dev/urandom 2>&1 | \
/usr/pkg/bin/pgrphack /usr/pkg/bin/setuidgid dnslog ${dnscache_logcmd}"
	command_args="&"
	rc_flags=""
}

if [ -f /etc/rc.subr ]; then
	load_rc_config $name
	run_rc_command "$1"
else
	echo -n " ${name}"
	dnscache_precmd
	eval ${command} ${dnscache_flags} ${command_args}
fi
