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

%config
    nats_server_enable="$openpkg_rc_def"
    nats_server_flags=""
    nats_server_log_prolog="true"
    nats_server_log_epilog="true"
    nats_server_log_numfiles="10"
    nats_server_log_minsize="1M"
    nats_server_log_complevel="9"

%common
    nats_server_cfgfile="@l_prefix@/etc/nats-server/nats-server.conf"
    nats_server_pidfile="@l_prefix@/var/nats-server/run/nats-server.pid"
    nats_server_logfile="@l_prefix@/var/nats-server/log/nats-server.log"
    nats_server_signal () {
        [ -f $nats_server_pidfile ] && kill -$1 `cat $nats_server_pidfile`
    }

%status -u @l_rusr@ -o
    nats_server_usable="unknown"
    nats_server_active="no"
    rcService nats-server enable yes && \
        nats_server_signal 0 && nats_server_active="yes"
    echo "nats_server_enable=\"$nats_server_enable\""
    echo "nats_server_usable=\"$nats_server_usable\""
    echo "nats_server_active=\"$nats_server_active\""

%start -u @l_rusr@
    rcService nats-server enable yes || exit 0
    rcService nats-server active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/nats-server \
           --config "$nats_server_cfgfile" \
           $nats_server_flags \
        </dev/null >>$nats_server_logfile 2>&1 &
        echo $! >$nats_server_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService nats-server enable yes || exit 0
    rcService nats-server active no  && exit 0
    nats_server_signal TERM
    sleep 2
    rm -f $nats_server_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService nats-server enable yes || exit 0
    shtool rotate -f \
        -n ${nats_server_log_numfiles} -s ${nats_server_log_minsize} -d \
        -z ${nats_server_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${nats_server_log_prolog}" \
        -E "${nats_server_log_epilog}; rc nats-server restart" \
        $nats_server_logfile

