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

%config
    activemq_enable="$openpkg_rc_def"
    activemq_flags="-server -Xms1G -Xmx1G -Dorg.apache.activemq.UseDedicatedTaskRunner=true -Dcom.sun.management.jmxremote"
    activemq_log_prolog="true"
    activemq_log_epilog="true"
    activemq_log_numfiles="10"
    activemq_log_minsize="1M"
    activemq_log_complevel="9"

%common
    activemq_pidfile="@l_prefix@/var/activemq/run/activemq.pid"
    activemq_logfile="@l_prefix@/var/activemq/log/activemq.log"
    activemq_signal () {
        [ -f $activemq_pidfile ] && kill -$1 `cat $activemq_pidfile`
    }

%status -u @l_rusr@ -o
    activemq_usable="unknown"
    activemq_active="no"
    rcService activemq enable yes && \
        activemq_signal 0 && activemq_active="yes"
    echo "activemq_enable=\"$activemq_enable\""
    echo "activemq_usable=\"$activemq_usable\""
    echo "activemq_active=\"$activemq_active\""

%start -u @l_rusr@
    rcService activemq enable yes || exit 0
    rcService activemq active yes && exit 0
    activemq_home="@l_prefix@/libexec/activemq"
    activemq_base="@l_prefix@/libexec/activemq"
    activemq_conf="@l_prefix@/etc/activemq"
    activemq_data="@l_prefix@/var/activemq/data"
    activemq_classpath="@l_prefix@/etc/activemq"
    cmd="@l_prefix@/bin/java"
    cmd="$cmd $activemq_flags"
    cmd="$cmd -Dactivemq.home=\"${activemq_home}\""
    cmd="$cmd -Dactivemq.base=\"${activemq_base}\""
    cmd="$cmd -Dactivemq.conf=\"${activemq_conf}\""
    cmd="$cmd -Dactivemq.data=\"${activemq_data}\""
    cmd="$cmd -Dactivemq.classpath=\"${activemq_classpath}\""
    cmd="$cmd -Djava.endorsed.dirs=\"@l_prefix@/libexec/activemq/endorsed\""
    cmd="$cmd -Djava.io.tmpdir=@l_prefix@/var/activemq/run"
    cmd="$cmd -Djava.util.logging.config.file=@l_prefix@/etc/activemq/logging.properties"
    cmd="$cmd -Djava.security.auth.login.config=@l_prefix@/etc/activemq/login.config"
    cmd="$cmd -Dhawtio.realm=activemq"
    cmd="$cmd -Dhawtio.role=admins"
    cmd="$cmd -Dhawtio.rolePrincipalClasses=org.apache.activemq.jaas.GroupPrincipal"
    cmd="$cmd -jar @l_prefix@/libexec/activemq/activemq.jar"
    cmd="$cmd start"
    ( eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
      JETTY_RUN=@l_prefix@/var/activemq/run
      export JETTY_RUN
      eval "nohup $cmd </dev/null >$activemq_logfile 2>&1 &"
      echo $! >$activemq_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService activemq enable yes || exit 0
    shtool rotate -f \
        -n $activemq_log_numfiles -s $activemq_log_minsize -d \
        -z $activemq_log_complevel -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "$activemq_log_prolog" \
        -E "$activemq_log_epilog; rc activemq restart" \
        $activemq_logfile

