#!/bin/sh

# PROVIDE: gitlab_pages
# REQUIRE: DAEMON NETWORKING
# KEYWORD: shutdown

#
# Add the following line to /etc/rc.conf to enable gitlab_pages:
#
# gitlab_pages_enable (bool):  Set to "NO" by default.
#                              Set it to "YES" to enable gitlab-pages
# gitlab_pages_dir (str):      Set to "/var/tmp/gitlab_pages" by default.
#                              Set it to directory to run gitlab-pages in
# gitlab_pages_user (str):     Set to "gitlab-pages" by default.
#                              Set it to user to run gitlab_pages under
# gitlab_pages_group (str):    Set to "gitlab-pages" by default.
#                              Set it to group to run gitlab-pages under
# gitlab_pages_logfile (str):  Set to "/var/log/gitlab_pages.log" by default.
#                              Set it to file to send gitlab-pages logs to
#

. /etc/rc.subr

name="gitlab_pages"
rcvar="gitlab_pages_enable"

load_rc_config $name

: ${gitlab_pages_enable:="NO"}
: ${gitlab_pages_dir:="/var/tmp/gitlab_pages"}
: ${gitlab_pages_user:="gitlab-pages"}
: ${gitlab_pages_group:="gitlab-pages"}
: ${gitlab_pages_logfile:="/var/log/gitlab_pages.log"}
: ${gitlab_pages_args:="-config=%%PREFIX%%/share/gitlab-pages/gitlab-pages.conf"}
: ${gitlab_pages_svcj_options:="net_basic"}

export HOME=${gitlab_pages_dir}
export PATH=${PATH}:%%PREFIX%%/bin

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

procname=%%PREFIX%%/share/gitlab-pages/bin/gitlab-pages

start_precmd="gitlab_pages_startprecmd"
start_cmd="gitlab_pages_startcmd"

gitlab_pages_startprecmd()
{
        if [ ! -e "${pidfile}" ]; then
                install -g ${gitlab_pages_group} -o ${gitlab_pages_user} -- /dev/null "${pidfile}";
        fi
        if [ ! -d "${gitlab_pages_dir}" ]; then
                install -d -o "${gitlab_pages_user}" -g "${gitlab_pages_group}" "${gitlab_pages_dir}"
        fi
}

gitlab_pages_startcmd()
{
        cd ${gitlab_pages_chdir} ; \
        daemon -u ${gitlab_pages_user} -p ${pidfile} ${procname} ${gitlab_pages_args} run < /dev/null >> ${gitlab_pages_logfile} 2>> ${gitlab_pages_logfile}
}

run_rc_command $1
