#!/bin/sh
## **************************************************************************
## For copyright and licensing terms, see the file named COPYING.
## **************************************************************************

# PROVIDE: system-control-normal
# REQUIRE: CRITLOCALMOUNTED service-manager
# BEFORE: DAEMON
# KEYWORD: nojail shutdown

. /etc/rc.subr

name='system_control_normal'
rcvar='system_control_normal_enable'		# FreeBSD style, with "_enable" suffix.
pidfile='/run/system-manager/system-control-normal.pid'
procname='system-control'
start_precmd='system_control_normal_start_precmd'
start_cmd='system_control_normal_start_cmd'
case "`uname`" in
NetBSD)	pnpmanager=devpubd;;
*BSD)	pnpmanager=devd;;
*)	pnpmanager=udev;;
esac

system_control_normal_start_precmd()
{
	# The system manager normally mounts and populates /run, and more besides.
	install -d -m 0755 /run/system-manager/log /run/service-bundles/early-supervise
}

system_control_normal_start_cmd()
{
	# We have to log to a different log than the normal one, because we cannot funnel into a single logger under Mewburn rc.
	/package/admin/nosh/command/prependpath PATH /package/admin/nosh/command:/package/admin/djbwares/command \
	fdredir --non-blocking --write 2 /dev/console \
	setsid \
	pipe \
		fdmove -c 2 1 \
		hardlimit -o 16384 \
		softlimit -o hard \
		foreground system-control start --verbose basic \; \
		foreground system-control reset --verbose "${pnpmanager}" "${pnpmanager}"-log \; \
		foreground system-control start --verbose workstation server \; \
		system-control start --verbose normal \
	\| \
		pipe \
			fdmove -c 2 1 \
			cyclog /run/system-manager/log \
		\| \
			logger -c -t system-control \
	&
	command printf '%s\n' $! > "${pidfile}"
}

load_rc_config $name
run_rc_command "$1"
