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

%config
    mosquitto_enable="$openpkg_rc_def"
    mosquitto_flags=""
    mosquitto_log_prolog="true"
    mosquitto_log_epilog="true"
    mosquitto_log_numfiles="10"
    mosquitto_log_minsize="1M"
    mosquitto_log_complevel="9"

%common
    mosquitto_cfgfile="@l_prefix@/etc/mosquitto/mosquitto.conf"
    mosquitto_pidfile="@l_prefix@/var/mosquitto/run/mosquitto.pid"
    mosquitto_logfile="@l_prefix@/var/mosquitto/log/mosquitto.log"
    mosquitto_signal () {
        [ -f $mosquitto_pidfile ] && kill -$1 `cat $mosquitto_pidfile`
    }

%status -u @l_rusr@ -o
    mosquitto_usable="unknown"
    mosquitto_active="no"
    rcService mosquitto enable yes && \
        mosquitto_signal 0 && mosquitto_active="yes"
    echo "mosquitto_enable=\"$mosquitto_enable\""
    echo "mosquitto_usable=\"$mosquitto_usable\""
    echo "mosquitto_active=\"$mosquitto_active\""

%start -u @l_rusr@
    rcService mosquitto enable yes || exit 0
    rcService mosquitto active yes && exit 0
    @l_prefix@/sbin/mosquitto -d -c $mosquitto_cfgfile $mosquitto_flags

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

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

%daily -u @l_rusr@
    rcService mosquitto enable yes || exit 0
    shtool rotate -f \
        -n ${mosquitto_log_numfiles} -s ${mosquitto_log_minsize} -d \
        -z ${mosquitto_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${mosquitto_log_prolog}" \
        -E "${mosquitto_log_epilog}; rc mosquitto restart" \
        $mosquitto_logfile

