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

%config
    opa_enable="$openpkg_rc_def"
    opa_flags="-a 127.0.0.1:8181 -l info"
    opa_log_prolog="true"
    opa_log_epilog="true"
    opa_log_numfiles="10"
    opa_log_minsize="1M"
    opa_log_complevel="9"

%common
    opa_cfgdir="@l_prefix@/etc/opa"
    opa_pidfile="@l_prefix@/var/opa/run/opa.pid"
    opa_logfile="@l_prefix@/var/opa/log/opa.log"
    opa_signal () {
        [ -f $opa_pidfile ] && kill -$1 `cat $opa_pidfile`
    }

%status -u @l_rusr@ -o
    opa_usable="unknown"
    opa_active="no"
    rcService opa enable yes && \
        opa_signal 0 && opa_active="yes"
    echo "opa_enable=\"$opa_enable\""
    echo "opa_usable=\"$opa_usable\""
    echo "opa_active=\"$opa_active\""

%start -u @l_rusr@
    rcService opa enable yes || exit 0
    rcService opa active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/opa run \
            --server \
            --tls-cert-file $opa_cfgdir/server.crt \
            --tls-private-key-file $opa_cfgdir/server.key \
            $opa_flags \
            $opa_cfgdir/conf.d/* \
        </dev/null >>$opa_logfile 2>&1 &
        echo $! >$opa_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService opa enable yes || exit 0
    shtool rotate -f \
        -n ${opa_log_numfiles} -s ${opa_log_minsize} -d \
        -z ${opa_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${opa_log_prolog}" \
        -E "${opa_log_epilog}; rc opa restart" \
        $opa_logfile

