#!@l_prefix@/bin/openpkg rc
##
##  rc.gitea -- Run-Commands
##

%config
    gitea_enable="$openpkg_rc_def"
    gitea_flags=""
    gitea_log_prolog="true"
    gitea_log_epilog="true"
    gitea_log_numfiles="10"
    gitea_log_minsize="1M"
    gitea_log_complevel="9"

%common
    gitea_cfgfile="@l_prefix@/etc/gitea/app.ini"
    gitea_pidfile="@l_prefix@/var/gitea/run/gitea.pid"
    gitea_logfile="@l_prefix@/var/gitea/log/gitea.log"
    gitea_signal () {
        [ -f $gitea_pidfile ] && kill -$1 `cat $gitea_pidfile`
    }

%status -u @l_rusr@ -o
    gitea_usable="unknown"
    gitea_active="no"
    rcService gitea enable yes && \
        gitea_signal 0 && gitea_active="yes"
    echo "gitea_enable=\"$gitea_enable\""
    echo "gitea_usable=\"$gitea_usable\""
    echo "gitea_active=\"$gitea_active\""

%start -u @l_rusr@
    rcService gitea enable yes || exit 0
    rcService gitea active yes && exit 0
    (   cd @l_prefix@/share/gitea
        PATH=@l_prefix@/bin:$PATH; export PATH
        HOME=@l_prefix@/var/gitea/home; export HOME
        nohup @l_prefix@/share/gitea/gitea web --config "$gitea_cfgfile" $gitea_flags \
        </dev/null >>$gitea_logfile 2>&1 &
        echo $! >$gitea_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService gitea enable yes || exit 0
    rcService gitea active no  && exit 0
    gitea_signal TERM
    sleep 2
    rm -f $gitea_pidfile >/dev/null 2>&1 || true

%restart -u @l_rusr@
    rcService gitea enable yes || exit 0
    rcService gitea active no  && exit 0
    rc gitea stop start

%daily -u @l_rusr@
    rcService gitea enable yes || exit 0
    shtool rotate -f \
        -n ${gitea_log_numfiles} -s ${gitea_log_minsize} -d \
        -z ${gitea_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${gitea_log_prolog}" \
        -E "${gitea_log_epilog}; rc gitea restart" \
        $gitea_logfile

