#!/bin/sh

# PROVIDE: immichframe
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# immichframe_enable (bool):	Set to NO by default.
# immichframe_user (str):	User to run as, default immich.
# immichframe_group (str):	Group to run as, default immich.
# immichframe_urls (str):	Addresses to listen on,
#				default http://0.0.0.0:8080.
# immichframe_config (str):	Directory holding immichframe.db,
#				default /var/db/immichframe.

. /etc/rc.subr

name=immichframe
rcvar=immichframe_enable
desc="Immich digital photo frame"

load_rc_config $name

: ${immichframe_enable:="NO"}
: ${immichframe_user:="immich"}
: ${immichframe_group:="immich"}
: ${immichframe_urls:="http://0.0.0.0:8080"}
: ${immichframe_config:="/var/db/immichframe"}
: ${immichframe_env:="ASPNETCORE_URLS=${immichframe_urls} \
	IMMICHFRAME_CONFIG_PATH=${immichframe_config}"}

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

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

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

run_rc_command "$1"
