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

%config
    m3_enable="$openpkg_rc_def"
    m3_flags=""
    m3_log_prolog="true"
    m3_log_epilog="true"
    m3_log_numfiles="10"
    m3_log_minsize="1M"
    m3_log_complevel="9"

%common
    m3_cfgfile="@l_prefix@/etc/m3/m3.yaml"
    m3_pidfile="@l_prefix@/var/m3/run/m3.pid"
    m3_logfile="@l_prefix@/var/m3/log/m3.log"
    m3_signal () {
        [ -f $m3_pidfile ] && kill -$1 `cat $m3_pidfile`
    }

%status -u @l_susr@ -o
    m3_usable="unknown"
    m3_active="no"
    rcService m3 enable yes && \
        m3_signal 0 && m3_active="yes"
    echo "m3_enable=\"$m3_enable\""
    echo "m3_usable=\"$m3_usable\""
    echo "m3_active=\"$m3_active\""

%start -u @l_susr@
    rcService m3 enable yes || exit 0
    rcService m3 active yes && exit 0
    (   nohup @l_prefix@/sbin/m3 \
           -f "$m3_cfgfile" \
           $m3_flags \
        </dev/null >>$m3_logfile 2>&1 &
        echo $! >$m3_pidfile
    ) >/dev/null 2>&1

%stop -u @l_susr@
    rcService m3 enable yes || exit 0
    rcService m3 active no  && exit 0
    m3_signal TERM
    sleep 2
    m3_signal KILL
    sleep 2
    rm -f $m3_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService m3 enable yes || exit 0
    shtool rotate -f \
        -n ${m3_log_numfiles} -s ${m3_log_minsize} -d \
        -z ${m3_log_complevel} -m 664 -o @l_susr@ -g @l_sgrp@ \
        -P "${m3_log_prolog}" \
        -E "${m3_log_epilog}; rc m3 restart" \
        $m3_logfile

