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

%config
    crossbar_enable="$openpkg_rc_def"
    crossbar_loglevel="info"
    crossbar_log_prolog="true"
    crossbar_log_epilog="true"
    crossbar_log_numfiles="10"
    crossbar_log_minsize="1M"
    crossbar_log_complevel="9"

%common
    crossbar_rundir="@l_prefix@/var/crossbar/run"
    crossbar_cfgfile="@l_prefix@/etc/crossbar/crossbar.json"
    crossbar_pidfile="@l_prefix@/var/crossbar/run/crossbar.pid"
    crossbar_logdir="@l_prefix@/var/crossbar/log"
    crossbar_logfile="@l_prefix@/var/crossbar/log/crossbar.log"
    crossbar_signal () {
        [ -f $crossbar_pidfile ] && kill -$1 `cat $crossbar_pidfile`
    }

%status -u @l_rusr@ -o
    crossbar_usable="unknown"
    crossbar_active="no"
    rcService crossbar enable yes && \
        crossbar_signal 0 && crossbar_active="yes"
    echo "crossbar_enable=\"$crossbar_enable\""
    echo "crossbar_usable=\"$crossbar_usable\""
    echo "crossbar_active=\"$crossbar_active\""

%start -u @l_rusr@
    rcService crossbar enable yes || exit 0
    rcService crossbar active yes && exit 0
    (   nohup @l_prefix@/bin/crossbar start \
            --cbdir "$crossbar_rundir" \
            --config "$crossbar_cfgfile" \
            --logdir "$crossbar_logdir" \
            --loglevel "$crossbar_loglevel" \
            --logtofile \
        </dev/null >>$crossbar_logfile 2>&1 &
        echo $! >$crossbar_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService crossbar enable yes || exit 0
    shtool rotate -f \
        -n ${crossbar_log_numfiles} -s ${crossbar_log_minsize} -d \
        -z ${crossbar_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${crossbar_log_prolog}" \
        -E "${crossbar_log_epilog}; rc crossbar restart" \
        $crossbar_logfile

