#!/bin/sh
#
# $NetBSD: dqcache.sh,v 1.1 2022/01/27 15:31:09 schmonz Exp $
#
# dq-20240101 script to control dqcache (caching DNS+DNSCurve resolver).
#

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

name="dqcache"

# User-settable rc.conf variables and their default values:
: ${dqcache_postenv:=""}
: ${dqcache_ip:="127.0.0.1"}
: ${dqcache_size:="10000000"}
: ${dqcache_log:="YES"}
: ${dqcache_logcmd:="logger -t nbdq/cache -p daemon.info"}
: ${dqcache_nologcmd:="/usr/pkg/bin/multilog -*"}

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

rcvar=${name}
required_dirs="/usr/pkg/etc/${name}/servers"
required_files="/usr/pkg/etc/${name}/servers/@"
command="/usr/pkg/sbin/${name}"
procname=nb${name}
start_precmd="dqcache_precmd"

dqcache_precmd() {
	if [ -f /etc/rc.subr ] && ! checkyesno dqcache_log; then
		dqcache_logcmd=${dqcache_nologcmd}
	fi
	command="/usr/pkg/bin/pgrphack /usr/bin/env - ${dqcache_postenv} \
ROOT=/usr/pkg/etc/${name} \
IP=${dqcache_ip} \
CACHESIZE=${dqcache_size} \
/usr/pkg/bin/envuidgid dqcache \
/usr/pkg/bin/argv0 /usr/pkg/sbin/${name} ${procname} \
2>&1 | \
${dqcache_logcmd}"
	command_args="&"
	rc_flags=""
}

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