#!/bin/sh
#
# $NetBSD: mimir.sh,v 1.1 2024/07/25 16:58:13 tnn Exp $
#
# PROVIDE: mimir
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Consider installing pkgtools/rc.subr in unprivileged.
#
# You will need to set some variables in /etc/rc.conf to start mimir:
#
# mimir=YES

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

name="mimir"
rcvar=$name
mimir_chdir="/var/lib/mimir"
mimir_user="mimir"
mimir_group="mimir"
command="/usr/pkg/bin/mimir"
command_args="--config.file=/usr/pkg/etc/mimir.yaml >> /var/log/mimir/mimir.log 2>&1 &"

if [ -f /etc/rc.subr -a -d /etc/rc.d -a -f /etc/rc.d/DAEMON ]; then
	load_rc_config $name
	run_rc_command "$1"
else
	if [ -f /etc/rc.conf ]; then
		. /etc/rc.conf
	fi
	case "$1" in
	start)
		if [ -r "${pidfile}" ]; then
			echo "Already running ${name}."
		else
			echo "Starting ${name}."
			eval ${command} ${command_args}
		fi
		;;
	stop)
		if [ -r "${pidfile}" ]; then
			echo "Stopping ${name}."
			kill `/bin/cat "${pidfile}"` && /bin/rm "${pidfile}"
		fi
		;;
	*)
		echo "Usage: $0 {start|stop}" 1>&2
		exit 10
		;;
	esac
fi
