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

%config
    sftpgo_enable="$openpkg_rc_def"
    sftpgo_flags=""
    sftpgo_log_prolog="true"
    sftpgo_log_epilog="true"
    sftpgo_log_numfiles="10"
    sftpgo_log_minsize="1M"
    sftpgo_log_complevel="9"

%common
    sftpgo_etcdir="@l_prefix@/etc/sftpgo/"
    sftpgo_pidfile="@l_prefix@/var/sftpgo/run/sftpgo.pid"
    sftpgo_logfile="@l_prefix@/var/sftpgo/log/sftpgo.log"
    sftpgo_signal () {
        [ -f $sftpgo_pidfile ] && kill -$1 `cat $sftpgo_pidfile`
    }

%status -u @l_rusr@ -o
    sftpgo_usable="unknown"
    sftpgo_active="no"
    rcService sftpgo enable yes && \
        sftpgo_signal 0 && sftpgo_active="yes"
    echo "sftpgo_enable=\"$sftpgo_enable\""
    echo "sftpgo_usable=\"$sftpgo_usable\""
    echo "sftpgo_active=\"$sftpgo_active\""

%start -u @l_rusr@
    rcService sftpgo enable yes || exit 0
    rcService sftpgo active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/sftpgo serve \
            --config-dir "$sftpgo_etcdir" \
            --config-file "sftpgo.yaml" \
            --log-file-path "" \
            $sftpgo_flags \
            </dev/null >>$sftpgo_logfile 2>&1 &
        echo $! >$sftpgo_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService sftpgo enable yes || exit 0
    shtool rotate -f \
        -n ${sftpgo_log_numfiles} -s ${sftpgo_log_minsize} -d \
        -z ${sftpgo_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${sftpgo_log_prolog}" \
        -E "${sftpgo_log_epilog}; rc sftpgo restart" \
        $sftpgo_logfile

