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

%config
    go2rtc_enable="$openpkg_rc_def"
    go2rtc_flags=""
    go2rtc_log_prolog="true"
    go2rtc_log_epilog="true"
    go2rtc_log_numfiles="10"
    go2rtc_log_minsize="1M"
    go2rtc_log_complevel="9"

%common
    go2rtc_cfgdir="@l_prefix@/etc/go2rtc"
    go2rtc_pidfile="@l_prefix@/var/go2rtc/run/go2rtc.pid"
    go2rtc_logfile="@l_prefix@/var/go2rtc/log/go2rtc.log"
    go2rtc_signal () {
        [ -f $go2rtc_pidfile ] && kill -$1 `cat $go2rtc_pidfile`
    }

%status -u @l_rusr@ -o
    go2rtc_usable="unknown"
    go2rtc_active="no"
    rcService go2rtc enable yes && \
        go2rtc_signal 0 && go2rtc_active="yes"
    echo "go2rtc_enable=\"$go2rtc_enable\""
    echo "go2rtc_usable=\"$go2rtc_usable\""
    echo "go2rtc_active=\"$go2rtc_active\""

%start -u @l_rusr@
    rcService go2rtc enable yes || exit 0
    rcService go2rtc active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/go2rtc \
            -config "$go2rtc_cfgdir/go2rtc.yaml" \
            $go2rtc_flags \
        </dev/null >>$go2rtc_logfile 2>&1 &
        echo $! >$go2rtc_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService go2rtc enable yes || exit 0
    rcService go2rtc active no  && exit 0
    go2rtc_signal TERM
    rm -f $go2rtc_pidfile >/dev/null 2>&1 || true

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

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

%daily -u @l_rusr@
    rcService go2rtc enable yes || exit 0
    shtool rotate -f \
        -n ${go2rtc_log_numfiles} -s ${go2rtc_log_minsize} -d \
        -z ${go2rtc_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${go2rtc_log_prolog}" \
        -E "${go2rtc_log_epilog}; rc go2rtc reload" \
        $go2rtc_logfile

