#!/bin/sh
#
# $NetBSD: arti.in,v 1.3 2023/03/09 18:19:40 nikita Exp $
#
# PROVIDE: arti
# REQUIRE: DAEMON NETWORKING
# KEYWORD: shutdown
#
# You will need to set some variables in /etc/rc.conf to start arti:
#
# ARTI=YES

if test -f /etc/rc.subr
then
	. /etc/rc.subr
fi

name="arti"
rcvar=${name}
command="/usr/pkg/sbin/daemonize"
procname="/usr/pkg/bin/${name}"
proc_args="proxy --config /usr/pkg/etc/arti/${name}.toml"
required_files="/usr/pkg/etc/arti/${name}.toml"
pidfile="/var/chroot/arti/${name}.pid"
start_precmd="arti_precmd"
command_args="${command_args} -p ${pidfile}"
command_args="${command_args} -o /var/chroot/arti/log/stdout.log"
command_args="${command_args} -e /var/chroot/arti/log/stderr.log"
command_args="${command_args} -u arti"
command_args="${command_args} -E HOME=/var/chroot/arti"
command_args="${command_args} ${procname} ${proc_args}"

arti_precmd()
{
	if [ ! -d /var/chroot/arti/log ]; then
		/bin/mkdir -p /var/chroot/arti/log
		/bin/chmod 0750 /var/chroot/arti/log
		/usr/sbin/chown arti:arti /var/chroot/arti/log
	fi
	if [ -f /var/chroot/arti/log/stdout.log ]; then
		/usr/sbin/chown arti:arti /var/chroot/arti/log/stdout.log
	fi
	if [ -f /var/chroot/arti/log/stderr.log ]; then
		/usr/sbin/chown arti:arti /var/chroot/arti/log/stderr.log
	fi
}

load_rc_config $name
run_rc_command "$1"
