#!/bin/sh
#
# $NetBSD: svscan.sh,v 1.6 2023/09/04 17:51:11 schmonz Exp $
#
# daemontools-run-20231129 script to control svscan (service supervisor).
#

# PROVIDE: svscan
# REQUIRE: NETWORKING mountcritremote syslogd
# BEFORE:  DAEMON

name="svscan"

# User-settable rc.conf variables and their default values:
: ${svscan_postenv:="PATH=/usr/pkg/sbin:/usr/pkg/bin:$PATH"}
: ${svscan_datalimit:="3000000"}
: ${svscan_servicedir:="/service"}
: ${svscan_log:="YES"}
: ${svscan_logcmd:="logger -t nb${name} -p daemon.info"}
: ${svscan_nologcmd:="/usr/pkg/bin/multilog -*"}

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

rcvar=${name}
required_dirs="${svscan_servicedir}"
command="/usr/pkg/bin/${name}"
procname=nb${name}
start_precmd="svscan_precmd"

svscan_precmd()
{
	if [ -f /etc/rc.subr ] && ! checkyesno svscan_log; then
		svscan_logcmd=${svscan_nologcmd}
	fi
	# There might be more than one svscan(8) on a system: for instance,
	# non-root users might run their own svscans. We want to signal only
	# the svscan process responsible for this service. Use argv0(1) to
	# set procname to "nbsvscan".
	command="/usr/pkg/bin/pgrphack /usr/bin/env - ${svscan_postenv}
/usr/pkg/bin/argv0 /usr/pkg/bin/${name} ${procname}
${svscan_servicedir}
2>&1 |
/usr/pkg/bin/pgrphack /usr/pkg/bin/setuidgid multilog ${svscan_logcmd}"
	command_args="&"
	rc_flags=""
}

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