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

%config
    mosca_enable="$openpkg_rc_def"
    mosca_log_prolog="true"
    mosca_log_epilog="true"
    mosca_log_numfiles="10"
    mosca_log_minsize="1M"
    mosca_log_complevel="9"

%common
    mosca_logfile="@l_prefix@/var/mosca/log/mosca.log"
    mosca_pidfile="@l_prefix@/var/mosca/run/mosca.pid"
    mosca_signal () {
        [ -f $mosca_pidfile ] && kill -$1 `cat $mosca_pidfile`
    }

%status -u @l_nusr@ -o
    mosca_usable="unknown"
    mosca_active="no"
    rcService mosca enable yes && \
        mosca_signal 0 && mosca_active="yes"
    echo "mosca_enable=\"$mosca_enable\""
    echo "mosca_usable=\"$mosca_usable\""
    echo "mosca_active=\"$mosca_active\""

%start -u @l_nusr@
    rcService mosca enable yes || exit 0
    rcService mosca active yes && exit 0
    (   nohup @l_prefix@/sbin/mosca \
        --config @l_prefix@/etc/mosca/mosca.conf.js \
        --credentials @l_prefix@/etc/mosca/mosca.cred.json \
        </dev/null >>$mosca_logfile 2>&1 &
        echo $! >$mosca_pidfile
    ) >/dev/null 2>&1

%stop -u @l_nusr@
    rcService mosca enable yes || exit 0
    rcService mosca active no  && exit 0
    mosca_signal TERM
    sleep 2
    rm -f $mosca_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_nusr@
    rcService mosca enable yes || exit 0
    shtool rotate -f \
        -n ${mosca_log_numfiles} -s ${mosca_log_minsize} -d \
        -z ${mosca_log_complevel} -m 664 -o @l_nusr@ -g @l_ngrp@ \
        -P "${mosca_log_prolog}" \
        -E "${mosca_log_epilog}; rc mosca restart" \
        $mosca_logfile

