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

%config
    hazelcast_enable="$openpkg_rc_def"
    hazelcast_flags="-server"
    hazelcast_minmem="512m"
    hazelcast_maxmem="512m"
    hazelcast_log_prolog="true"
    hazelcast_log_epilog="true"
    hazelcast_log_numfiles="10"
    hazelcast_log_minsize="1M"
    hazelcast_log_complevel="9"

%common
    hazelcast_pidfile="@l_prefix@/var/hazelcast/run/hazelcast.pid"
    hazelcast_logfile="@l_prefix@/var/hazelcast/log/hazelcast.log"
    hazelcast_signal () {
        [ -f $hazelcast_pidfile ] && kill -$1 `cat $hazelcast_pidfile`
    }

%status -u @l_rusr@ -o
    hazelcast_usable="unknown"
    hazelcast_active="no"
    rcService hazelcast enable yes && \
        hazelcast_signal 0 && hazelcast_active="yes"
    echo "hazelcast_enable=\"$hazelcast_enable\""
    echo "hazelcast_usable=\"$hazelcast_usable\""
    echo "hazelcast_active=\"$hazelcast_active\""

%start -u @l_rusr@
    rcService hazelcast enable yes || exit 0
    rcService hazelcast active yes && exit 0
    (    eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
         nohup @l_prefix@/bin/java $hazelcast_flags \
             -cp "@l_prefix@/lib/hazelcast/hazelcast.jar:@l_prefix@/etc/hazelcast" \
             com.hazelcast.core.server.StartServer \
             </dev/null >$hazelcast_logfile 2>&1 &
         echo $! >$hazelcast_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService hazelcast enable yes || exit 0
    rcService hazelcast active no  && exit 0
    hazelcast_signal TERM
    sleep 2
    rm -f $hazelcast_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService hazelcast enable yes || exit 0
    shtool rotate -f \
        -n $hazelcast_log_numfiles -s $hazelcast_log_minsize -d \
        -z $hazelcast_log_complevel -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "$hazelcast_log_prolog" \
        -E "$hazelcast_log_epilog; rc hazelcast restart" \
        $hazelcast_logfile

