#!/bin/sh
#
# $NetBSD: vaultwarden.sh,v 1.2 2025/12/30 10:43:22 adam Exp $
#
# PROVIDE: vaultwarden
# REQUIRE: DAEMON
# KEYWORD: shutdown

name="vaultwarden"

command="/usr/pkg/sbin/daemonize"

pidfile="/var/run/${name}/${name}.pid"

task="/usr/pkg/sbin/${name}"
procname="${task}"

command_args="-p ${pidfile} \
    -u vwarden \
    -c /var/vaultwarden \
    -e /var/log/vaultwarden/daemonize.stderr \
    -o /var/log/vaultwarden/daemonize.stdout \
    -E ENV_FILE=/usr/pkg/etc/${name}.env ${task}"

vaultwarden_precmd()
{
        if [ ! -e "/var/run/${name}" ] ; then
                install -d -o vwarden -g vwarden \
		    /var/run/${name};
        fi
}

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

	rcvar=${name}
	required_files="/usr/pkg/etc/${name}.env"
	pidfile="/var/run/${name}/${name}.pid"

	start_precmd="vaultwarden_precmd"

	load_rc_config ${name}
	run_rc_command "$1"
else
        echo -n " ${name}"
	vaultwarden_precmd
	${command} ${command_args}
fi
