#!/bin/sh

# PROVIDE: forgejo
# REQUIRE: NETWORKING SYSLOG
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable forgejo:
#
#forgejo_enable="YES"

. /etc/rc.subr

name="forgejo"
rcvar="forgejo_enable"

load_rc_config $name

: ${forgejo_user:="%%GITUSER%%"}
: ${forgejo_enable:="NO"}
: ${forgejo_configcheck_enable:="YES"}
: ${forgejo_facility:="daemon"}
: ${forgejo_priority:="info"}
: ${forgejo_custom:="%%PREFIX%%/etc/${name}"}

command="%%PREFIX%%/sbin/${name} web"
procname="%%PREFIX%%/sbin/${name}"
githome="$(eval echo ~${forgejo_user})"

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

start_cmd="${name}_start"
start_precmd="${name}_prestart"

forgejo_start() {
	for d in %%DBDIR%% %%LOGDIR%%; do
		if [ ! -e "$d" ]; then
			mkdir "$d"
			chown ${forgejo_user} "$d"
		fi
	done
	/usr/sbin/daemon %%DAEMONARGS%% \
		-u ${forgejo_user} -p ${pidfile} \
		/usr/bin/env -i \
		"FORGEJO_CUSTOM=${forgejo_custom}" \
		"HOME=${githome}" \
		"PATH=%%PREFIX%%/bin:${PATH}" \
		"USER=${forgejo_user}" \
		$command
}

forgejo_prestart() {
	if checkyesno forgejo_configcheck_enable; then
		if ! errstr=$(/usr/bin/env -i \
		    "FORGEJO_CUSTOM=${forgejo_custom}" \
		    "HOME=${githome}" \
		    "PATH=%%PREFIX%%/bin:${PATH}" \
		    "USER=${forgejo_user}" \
		    /usr/bin/su -m ${forgejo_user} -c \
		    "%%PREFIX%%/sbin/${name} doctor check" 2>&1); then
			rc=$?
			echo "$errstr" >&2
			return $rc
		fi
	fi
}

run_rc_command "$1"
