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

%config
    frp_enable="$openpkg_rc_def"
    frp_daemons="@daemons@"
    frp_log_prolog="true"
    frp_log_epilog="true"
    frp_log_numfiles="10"
    frp_log_minsize="1M"
    frp_log_complevel="9"

%common
    frp_cfgdir="@l_prefix@/etc/frp"
    frp_piddir="@l_prefix@/var/frp/run"
    frp_logdir="@l_prefix@/var/frp/log"
    frp_signal () {
        [ -f "$frp_piddir/frpc.pid" ] && \
            kill -$1 `cat $frp_piddir/frpc.pid`
        local frp_rc_frpc=$?
        [ -f "$frp_piddir/frps.pid" ] && \
            kill -$1 `cat $frp_piddir/frps.pid`
        local frp_rc_frps=$?
        [    $frp_rc_frps -eq 0 \
          -o $frp_rc_frpc -eq 0 ]
    }

%status -u @l_rusr@ -o
    frp_usable="unknown"
    frp_active="no"
    rcService frp enable yes && \
        frp_signal 0 && frp_active="yes"
    echo "frp_enable=\"$frp_enable\""
    echo "frp_usable=\"$frp_usable\""
    echo "frp_active=\"$frp_active\""

%start -u @l_rusr@
    rcService frp enable yes || exit 0
    rcService frp active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        for daemon in $frp_daemons; do
            nohup @l_prefix@/sbin/$daemon -c "$frp_cfgdir/$daemon.ini" \
                </dev/null >>$frp_logdir/$daemon.log 2>&1 &
            echo $! >$frp_piddir/$daemon.pid
            sleep 1
        done
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService frp enable yes || exit 0
    shtool rotate -f \
        -n ${frp_log_numfiles} -s ${frp_log_minsize} -d \
        -z ${frp_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${frp_log_prolog}" \
        -E "${frp_log_epilog}; rc frp restart" \
        $frp_logfile

