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

%config
    emqx_enable="$openpkg_rc_def"
    emqx_flags=""
    emqx_log_prolog="true"
    emqx_log_epilog="true"
    emqx_log_numfiles="10"
    emqx_log_minsize="1M"
    emqx_log_complevel="9"

%common
    emqx_cfgfile="@l_prefix@/etc/emqx/emqx.config"
    emqx_pidfile="@l_prefix@/var/emqx/run/emqx.pid"
    emqx_logfile="@l_prefix@/var/emqx/log/emqx.log"
    emqx_signal () {
        [ -f $emqx_pidfile ] && kill -$1 `cat $emqx_pidfile`
    }

%status -u @l_nusr@ -o
    emqx_usable="unknown"
    emqx_active="no"
    rcService emqx enable yes && \
        emqx_signal 0 && emqx_active="yes"
    echo "emqx_enable=\"$emqx_enable\""
    echo "emqx_usable=\"$emqx_usable\""
    echo "emqx_active=\"$emqx_active\""

%start -u @l_nusr@
    rcService emqx enable yes || exit 0
    rcService emqx active yes && exit 0
    @l_prefix@/bin/emqx start

%stop -u @l_nusr@
    rcService emqx enable yes || exit 0
    rcService emqx active no  && exit 0
    @l_prefix@/bin/emqx stop
    sleep 2
    rm -f $emqx_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_nusr@
    rcService emqx enable yes || exit 0
    shtool rotate -f \
        -n ${emqx_log_numfiles} -s ${emqx_log_minsize} -d \
        -z ${emqx_log_complevel} -m 664 -o @l_nusr@ -g @l_ngrp@ \
        -P "${emqx_log_prolog}" \
        -E "${emqx_log_epilog}; rc emqx restart" \
        $emqx_logfile

