#!/bin/sh

# PROVIDE: paperless-webui
# REQUIRE: LOGIN
# BEFORE:  paperless-worker
# KEYWORD: shutdown
#
# Add the following line(s) to /etc/rc.conf to enable the paperless
# celery webui process:
#
# paperless_webui_enable (bool):	Set to "NO" by default.
#					Set it to "YES" to enable
#					paperless_webui
# paperless_webui_user (str):		Set to "paperless" by default.
#					User to run webui.
# paperless_webui_log_facility (str):	Set to "daemon" by default.
#					WebUI logging syslog facility.
# paperless_webui_log_priority (str):	Set to "notice" by default.
#					WebUI logging syslog priority.
# paperless_webui_log_tag (str):	Set to "paperless-webui" by default.
#					WebUI logging syslog tag.
# paperless_webui_listen_address (str):	Set to "127.0.0.1" by default.
#					IP address to listen on.
# paperless_webui_listen_port (int):	Set to "8000" by default.
#					Port to listen on.
# paperless_webui_restart_delay (int):	Set to "5" by default.
#					WebUI automatic restart delay in
#					seconds.
# paperless_webui_secret_key (str):	Set to
#					"%%PREFIX%%/etc/paperless_secret_key"
#					by default. Load PAPERLESS_SECRET_KEY
#					from this file from start, in case it
#					is not in the environment already.
#					Will be created on start in case it
#					does not exist.
#					Content of PAPERLESS_SECRET_KEY can be
#					overridden in
#					%%PREFIX%%/etc/paperless.conf.
# paperless_webui_tmpdir (str):		If set, overrides TMPDIR

. /etc/rc.subr

name="paperless_webui"
rcvar="${name}_enable"

# read configuration and set defaults
load_rc_config "$name"
paperless_webui_enable=${paperless_webui_enable:-"NO"}
paperless_webui_daemon_user=${paperless_webui_user:-"paperless"}
paperless_webui_log_facility=${paperless_webui_log_facility:-"daemon"}
paperless_webui_log_priority=${paperless_webui_log_priority:-"notice"}
paperless_webui_log_tag=${paperless_webui_log_tag:-"paperless-webui"}
paperless_webui_listen_address=${paperless_webui_listen_address:-"127.0.0.1"}
paperless_webui_listen_port=${paperless_webui_listen_port:-"8000"}
paperless_webui_restart_delay=${paperless_webui_restart_delay:-"5"}
paperless_webui_secret_key=${\
paperless_webui_secret_key:-"%%PREFIX%%/etc/paperless_secret_key"}
paperless_webui_tmpdir=${paperless_webui_tmpdir:-"$TMPDIR"}

paperless_webui_env="LANG=en_US.UTF-8 PATH=$PATH:%%PREFIX%%/bin\
  PYTHONPATH=%%PYTHONPREFIX_SITELIBDIR%%/paperless \
  PAPERLESS_BIND_ADDR=${paperless_webui_listen_address} \
  PAPERLESS_PORT=${paperless_webui_listen_port}"

if [ -n "$paperless_webui_tmpdir" ]; then
	paperless_webui_env="$paperless_webui_env TMPDIR=\
$paperless_webui_tmpdir"
fi

start_precmd="paperless_webui_prestart"
pidfile="/var/run/paperless/webui.pid"
command="/usr/sbin/daemon"
command_args="-cS -u ${paperless_webui_daemon_user} \
	-P ${pidfile} \
	-t paperless-webui \
	-l ${paperless_webui_log_facility} \
	-s ${paperless_webui_log_priority} \
	-T ${paperless_webui_log_tag} \
	-R ${paperless_webui_restart_delay} \
	%%PREFIX%%/bin/gunicorn \
	-c %%PYTHONPREFIX_SITELIBDIR%%/paperless/gunicorn.conf.py \
	paperless.asgi:application"

paperless_webui_prestart()
{
	if [ -z "$PAPERLESS_SECRET_KEY" ]; then
		if [ ! -s "$paperless_webui_secret_key" ]; then
			echo "Creating $paperless_webui_secret_key"
			(
				set -eo pipefail; umask 077
				uuidgen -crn 10 | xargs | tr ' ' 'x' \
				>"$paperless_webui_secret_key"
			) || return 1
		fi
		PAPERLESS_SECRET_KEY=$(cat "$paperless_webui_secret_key")
		export PAPERLESS_SECRET_KEY
	fi
	if [ "${#PAPERLESS_SECRET_KEY}" -lt "128" ]; then
		>&2 echo "PAPERLESS_SECRET_KEY is shorter than 128 chars"
		return 1
	fi
}

run_rc_command "$1"
