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

%config
    livekit_enable="$openpkg_rc_def"
    livekit_flags=""
    livekit_log_prolog="true"
    livekit_log_epilog="true"
    livekit_log_numfiles="10"
    livekit_log_minsize="1M"
    livekit_log_complevel="9"

%common
    livekit_cfgfile="@l_prefix@/etc/livekit/livekit.yaml"
    livekit_pidfile="@l_prefix@/var/livekit/run/livekit.pid"
    livekit_logfile="@l_prefix@/var/livekit/log/livekit.log"
    livekit_signal () {
        [ -f $livekit_pidfile ] && kill -$1 `cat $livekit_pidfile`
    }

%status -u @l_rusr@ -o
    livekit_usable="unknown"
    livekit_active="no"
    rcService livekit enable yes && \
        livekit_signal 0 && livekit_active="yes"
    echo "livekit_enable=\"$livekit_enable\""
    echo "livekit_usable=\"$livekit_usable\""
    echo "livekit_active=\"$livekit_active\""

%start -u @l_rusr@
    rcService livekit enable yes || exit 0
    rcService livekit active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/livekit \
            --config "$livekit_cfgfile" \
            $livekit_flags \
        </dev/null >>$livekit_logfile 2>&1 &
        echo $! >$livekit_pidfile
    ) >/dev/null 2>&1

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

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

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

%daily -u @l_rusr@
    rcService livekit enable yes || exit 0
    shtool rotate -f \
        -n ${livekit_log_numfiles} -s ${livekit_log_minsize} -d \
        -z ${livekit_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${livekit_log_prolog}" \
        -E "${livekit_log_epilog}; rc livekit reload" \
        $livekit_logfile

