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

%config
    apollo_enable="$openpkg_rc_def"
    apollo_flags="-server -server -Xmx1G -XX:+HeapDumpOnOutOfMemoryError -XX:-UseBiasedLocking"
    apollo_log_prolog="true"
    apollo_log_epilog="true"
    apollo_log_numfiles="10"
    apollo_log_minsize="1M"
    apollo_log_complevel="9"

%common
    apollo_pidfile="@l_prefix@/var/apollo/run/apollo.pid"
    apollo_logfile="@l_prefix@/var/apollo/log/apollo.log"
    apollo_signal () {
        [ -f $apollo_pidfile ] && kill -$1 `cat $apollo_pidfile`
    }

%status -u @l_rusr@ -o
    apollo_usable="unknown"
    apollo_active="no"
    rcService apollo enable yes && \
        apollo_signal 0 && apollo_active="yes"
    echo "apollo_enable=\"$apollo_enable\""
    echo "apollo_usable=\"$apollo_usable\""
    echo "apollo_active=\"$apollo_active\""

%start -u @l_rusr@
    rcService apollo enable yes || exit 0
    rcService apollo active yes && exit 0
    (   APOLLO_HOME="@l_prefix@/libexec/apollo"
        export APOLLO_HOME
        APOLLO_BASE="@l_prefix@/var/apollo"
        export APOLLO_BASE
        JVM_FLAGS="$apollo_flags"
        export JVM_FLAGS
        eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
        eval "nohup $APOLLO_HOME/bin/apollo run </dev/null >$apollo_logfile 2>&1 &"
        echo $! >$apollo_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService apollo enable yes || exit 0
    shtool rotate -f \
        -n $apollo_log_numfiles -s $apollo_log_minsize -d \
        -z $apollo_log_complevel -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "$apollo_log_prolog" \
        -E "$apollo_log_epilog; rc apollo restart" \
        $apollo_logfile

