#!/bin/sh

# PROVIDE: immich_power_tools
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# immich_power_tools_enable (bool):	Set to NO by default.
# immich_power_tools_user (str):	User to run as, default immich.
# immich_power_tools_group (str):	Group to run as, default immich.
# immich_power_tools_host (str):	Address to listen on, default
#					0.0.0.0.
# immich_power_tools_port (int):	Port to listen on, default 3000.
# immich_power_tools_db (str):		Path of the settings database,
#					default
#					/var/db/immich-power-tools/app.db.

. /etc/rc.subr

name=immich_power_tools
rcvar=immich_power_tools_enable
desc="Immich Power Tools"

load_rc_config $name

: ${immich_power_tools_enable:="NO"}
: ${immich_power_tools_user:="immich"}
: ${immich_power_tools_group:="immich"}
: ${immich_power_tools_host:="0.0.0.0"}
: ${immich_power_tools_port:="3000"}
: ${immich_power_tools_db:="/var/db/immich-power-tools/app.db"}
: ${immich_power_tools_env:="HOST=${immich_power_tools_host} \
	PORT=${immich_power_tools_port} \
	APP_DB_PATH=${immich_power_tools_db}"}

pidfile=/var/run/${name}/${name}.pid
childpidfile=/var/run/${name}/${name}_child.pid
logfile=/var/log/${name}.log
start_precmd=immich_power_tools_precmd
command=/usr/sbin/daemon
command_args="-f -H -t ${name} -P ${pidfile} -p ${childpidfile} \
	-o ${logfile} /usr/local/bin/immich-power-tools"

immich_power_tools_precmd()
{
	# Keep what the service creates itself out of other users' reach
	umask 027

	install -d -m 750 -o ${immich_power_tools_user} \
		-g ${immich_power_tools_group} /var/run/${name}
	[ -f ${logfile} ] || install -m 640 -o ${immich_power_tools_user} \
		-g ${immich_power_tools_group} /dev/null ${logfile}
}

run_rc_command "$1"
