#!/bin/sh

# PROVIDE: moonshine
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# moonshine_enable (bool):	Set to NO by default.
#				Set it to YES to enable moonshine.
# moonshine_user (user):	User to run moonshine as.
#				Default is "root".
# moonshine_config (path):	Path to the configuration file.
#				Default: /usr/local/etc/moonshine/moonshine.toml
# moonshine_runtime (path):	XDG runtime directory.
#				Default: /var/run/moonshine
# moonshine_log_file (path):	Log file.
#				Default: /var/log/moonshine.log
# moonshine_log_level (str):	Log verbosity (MOONSHINE_LOG env filter).
#				Default: "info".  Moonshine's own default is
#				"error", which logs nothing in normal operation
#				(including the pairing-PIN URL banner).
# moonshine_args (str):		Extra arguments.

. /etc/rc.subr

name=moonshine
rcvar=moonshine_enable

load_rc_config $name

: ${moonshine_enable:="NO"}
: ${moonshine_user:="root"}
: ${moonshine_config="/usr/local/etc/moonshine/moonshine.toml"}
: ${moonshine_runtime="/var/run/moonshine"}
: ${moonshine_log_file="/var/log/moonshine.log"}
: ${moonshine_log_level="info"}

moonshine_home=$(pw usershow -n "${moonshine_user}" 2>/dev/null | cut -d: -f9)
: ${moonshine_home:="/root"}

# daemon(8) supervises moonshine and restarts it (-R) if it exits, so a
# transient crash no longer leaves the host unreachable until a manual
# restart.  Under supervision rc.subr tracks daemon(8) itself: its pid goes
# in ${pidfile} (via -P) and the moonshine child pid in ${child_pidfile}
# (via -p).  ${procname} must name the process that ${pidfile} holds -- the
# daemon(8) supervisor, not the child -- or "service moonshine status"
# would never match it.  "service moonshine stop" signals the supervisor,
# which stops respawning and shuts the child down.
pidfile=/var/run/moonshine.pid
child_pidfile=/var/run/moonshine_child.pid
procname="/usr/sbin/daemon"
moonshine_bin="/usr/local/bin/moonshine"
command="/usr/sbin/daemon"
command_args="-f -R 5 -P ${pidfile} -p ${child_pidfile} -o ${moonshine_log_file} \
	/usr/bin/env PATH=/usr/local/bin:/usr/bin:/bin \
	HOME=${moonshine_home} XDG_RUNTIME_DIR=${moonshine_runtime} \
	MOONSHINE_LOG=${moonshine_log_level} \
	${moonshine_bin} ${moonshine_config} ${moonshine_args}"

start_precmd=moonshine_startprecmd

moonshine_startprecmd()
{
	if [ ! -e ${pidfile} ]; then
		install -o ${moonshine_user} /dev/null ${pidfile}
	fi

	if [ ! -e ${child_pidfile} ]; then
		install -o ${moonshine_user} /dev/null ${child_pidfile}
	fi

	if [ ! -e ${moonshine_log_file} ]; then
		install -o ${moonshine_user} /dev/null ${moonshine_log_file}
	fi

	# moonshine needs an XDG runtime directory for its Wayland socket;
	# it is passed to the daemon via env(1) in command_args.
	if [ ! -d ${moonshine_runtime} ]; then
		install -d -o ${moonshine_user} -m 0700 ${moonshine_runtime}
	fi

	# moonshine stores its TLS certificate under $HOME/.config/moonshine
	# (see the sample config); create it owned by moonshine_user so the
	# daemon can write cert.pem/key.pem on first start.
	if [ ! -d "${moonshine_home}/.config/moonshine" ]; then
		install -d -o ${moonshine_user} -m 0700 \
			"${moonshine_home}/.config/moonshine"
	fi
}

run_rc_command "$1"
