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

%config
    rabbitmq_enable="$openpkg_rc_def"
    rabbitmq_log_prolog="true"
    rabbitmq_log_epilog="true"
    rabbitmq_log_numfiles="10"
    rabbitmq_log_minsize="1M"
    rabbitmq_log_complevel="9"

%common
    rabbitmq_pidfile="@l_prefix@/var/rabbitmq/run/rabbitmq.pid"
    rabbitmq_logfile="@l_prefix@/var/rabbitmq/log/rabbitmq.log"
    rabbitmq_rundir="@l_prefix@/var/rabbitmq/run"
    rabbitmq_logdir="@l_prefix@/var/rabbitmq/log"
    rabbitmq_dbdir="@l_prefix@/var/rabbitmq/db"
    rabbitmq_signal () {
        [ -f $rabbitmq_pidfile ] && kill -$1 `cat $rabbitmq_pidfile`
    }

%status -u @l_rusr@ -o
    rabbitmq_usable="unknown"
    rabbitmq_active="no"
    rcService rabbitmq enable yes && \
        rabbitmq_signal 0 && rabbitmq_active="yes"
    echo "rabbitmq_enable=\"$rabbitmq_enable\""
    echo "rabbitmq_usable=\"$rabbitmq_usable\""
    echo "rabbitmq_active=\"$rabbitmq_active\""

%start -u @l_rusr@
    rcService rabbitmq enable yes || exit 0
    rcService rabbitmq active yes && exit 0
    ( export RABBITMQ_NODENAME="example"
      export RABBITMQ_NODE_IP_ADDRESS="127.0.0.1"
      export RABBITMQ_NODE_PORT="5672"
      export RABBITMQ_CTL_ERL_ARGS=""
      export RABBITMQ_SERVER_ERL_ARGS=""
      export RABBITMQ_SERVER_START_ARGS=""
      export RABBITMQ_CLUSTER_CONFIG_FILE=""
      . @l_prefix@/etc/rabbitmq/rabbitmq.conf
      export RABBITMQ_LOG_BASE="$rabbitmq_logdir"
      export RABBITMQ_MNESIA_DIR="$rabbitmq_dbdir"
      export RABBITMQ_PID_FILE="$rabbitmq_pidfile"
      export HOME="$rabbitmq_rundir"
      eval "nohup @l_prefix@/bin/rabbitmq-server </dev/null >$rabbitmq_logfile 2>&1 &"
      echo $! >$rabbitmq_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService rabbitmq enable yes || exit 0
    rcService rabbitmq active no && exit 0
    rabbitmq_signal TERM
    sleep 2
    rm -f $rabbitmq_pidfile 2>/dev/null || true

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

%daily -u @l_rusr@
    rcService rabbitmq enable yes || exit 0
    shtool rotate -f \
        -n $rabbitmq_log_numfiles -s $rabbitmq_log_minsize -d \
        -z $rabbitmq_log_complevel -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "$rabbitmq_log_prolog" \
        -E "$rabbitmq_log_epilog; rc rabbitmq restart" \
        $rabbitmq_logfile

