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

%config
    icescrum_enable="$openpkg_rc_def"
    icescrum_flags="host=127.0.0.1 port=8080 context=/"
    icescrum_log_rotsteps="10"
    icescrum_log_rotminsize="10M"
    icescrum_log_rotcomplevel="9"
    icescrum_log_rotprolog="true"
    icescrum_log_rotepilog="true"

%common
    icescrum_pidfile="@l_prefix@/var/icescrum/run/icescrum.pid"
    icescrum_logfile="@l_prefix@/var/icescrum/log/icescrum.log"
    icescrum_signal () {
        [ -f $icescrum_pidfile ] && kill -$1 `cat $icescrum_pidfile`
    }

%status -u @l_susr@ -o
    icescrum_usable="unknown"
    icescrum_active="no"
    rcService icescrum enable yes && \
        icescrum_signal 0 && icescrum_active="yes"
    echo "icescrum_enable=\"$icescrum_enable\""
    echo "icescrum_usable=\"$icescrum_usable\""
    echo "icescrum_active=\"$icescrum_active\""

%start -u @l_susr@
    rcService icescrum enable yes || exit 0
    rcService icescrum active yes && exit 0
    (   eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
        cd @l_prefix@/var/icescrum/db
        nohup @l_prefix@/bin/java \
            -Xmx1024M \
            -XX:MaxPermSize=256m \
            -jar @l_prefix@/libexec/icescrum/icescrum.jar \
            $icescrum_flags \
            </dev/null >$icescrum_logfile 2>&1 &
        echo $! >$icescrum_pidfile
    ) >/dev/null 2>&1

%stop -u @l_susr@
    rcService icescrum enable yes || exit 0
    rcService icescrum active no  && exit 0
    icescrum_signal TERM
    sleep 2
    rm -f $icescrum_pidfile 2>/dev/null || true

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

%daily -u @l_susr@
    rcService icescrum enable yes || exit 0
    shtool rotate -f \
        -n $icescrum_log_rotsteps -s $icescrum_log_rotminsize -d \
        -z $icescrum_log_rotcomplevel -o @l_susr@ -g @l_mgrp@ -m 644 \
        -P "$icescrum_log_rotprolog" \
        -E "$icescrum_log_rotepilog; rc icescrum reload" \
        $icescrum_logfile

