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

%config
    gogs_enable="$openpkg_rc_def"
    gogs_flags=""
    gogs_log_prolog="true"
    gogs_log_epilog="true"
    gogs_log_numfiles="10"
    gogs_log_minsize="1M"
    gogs_log_complevel="9"

%common
    gogs_cfgfile="@l_prefix@/etc/gogs/app.ini"
    gogs_pidfile="@l_prefix@/var/gogs/run/gogs.pid"
    gogs_logfile="@l_prefix@/var/gogs/log/gogs.log"
    gogs_signal () {
        [ -f $gogs_pidfile ] && kill -$1 `cat $gogs_pidfile`
    }

%status -u @l_rusr@ -o
    gogs_usable="unknown"
    gogs_active="no"
    rcService gogs enable yes && \
        gogs_signal 0 && gogs_active="yes"
    echo "gogs_enable=\"$gogs_enable\""
    echo "gogs_usable=\"$gogs_usable\""
    echo "gogs_active=\"$gogs_active\""

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

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

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

%daily -u @l_rusr@
    rcService gogs enable yes || exit 0
    shtool rotate -f \
        -n ${gogs_log_numfiles} -s ${gogs_log_minsize} -d \
        -z ${gogs_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${gogs_log_prolog}" \
        -E "${gogs_log_epilog}; rc gogs restart" \
        $gogs_logfile

