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

%config
    tomee_enable="$openpkg_rc_def"
    #   -Xss: per-thread       stack size
    #   -Xmn: young generation heap size
    #   -Xms: start/initial    heap size
    #   -Xmx: maximum          heap size
    tomee_flags="-server -Xss2M -Xmn256M -Xms512M -Xmx1024M"
    tomee_home="@l_prefix@/libexec/tomee"
    tomee_log_prolog="true"
    tomee_log_epilog="true"
    tomee_log_numfiles="10"
    tomee_log_minsize="1M"
    tomee_log_complevel="9"

%common
    tomee_pidfile="@l_prefix@/var/tomee/run/tomee.pid"
    tomee_signal () {
        [ -f $tomee_pidfile ] && kill -$1 `cat $tomee_pidfile`
    }

%status -u @l_rusr@ -o
    tomee_usable="unknown"
    tomee_active="no"
    rcService tomee enable yes && \
        tomee_signal 0 && tomee_active="yes"
    echo "tomee_enable=\"$tomee_enable\""
    echo "tomee_usable=\"$tomee_usable\""
    echo "tomee_active=\"$tomee_active\""

%start -u @l_rusr@
    rcService tomee enable yes || exit 0
    rcService tomee active yes && exit 0
    eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
    CATALINA_HOME="$tomee_home"; export CATALINA_HOME
    CATALINA_PID="$tomee_pidfile"; export CATALINA_PID
    CATALINA_OPTS="$tomee_flags"; export CATALINA_OPTS
    $CATALINA_HOME/bin/startup.sh

%stop -u @l_rusr@
    rcService tomee enable yes || exit 0
    rcService tomee active no && exit 0
    eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
    CATALINA_HOME="$tomee_home"; export CATALINA_HOME
    CATALINA_PID="$tomee_pidfile"; export CATALINA_PID
    CATALINA_OPTS="$tomee_flags"; export CATALINA_OPTS
    $CATALINA_HOME/bin/shutdown.sh -force
    rm -f $tomee_pidfile 2>/dev/null || true

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

%daily -u @l_rusr@
    rcService tomee enable yes || exit 0
    shtool rotate -f \
        -n ${tomee_log_numfiles} -s ${tomee_log_minsize} -d \
        -z ${tomee_log_complevel} -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "${tomee_log_prolog}" \
        -E "${tomee_log_epilog}; rc tomee restart" \
        @l_prefix@/var/tomee/log/catalina.out

