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

%config
    avis_enable="$openpkg_rc_def"
    avis_flags=""
    avis_flags_jvm="-Xms128M -Xmx256M -Xverify:none -XX:+UseParallelGC"
    avis_log_prolog="true"
    avis_log_epilog="true"
    avis_log_numfiles="10"
    avis_log_minsize="1M"
    avis_log_complevel="9"

%common
    avis_cfgfile="@l_prefix@/etc/avis/avisd.config"
    avis_pidfile="@l_prefix@/var/avis/avisd.pid"
    avis_logfile="@l_prefix@/var/avis/avisd.log"
    avis_jarfile="@l_prefix@/libexec/avis/avis-router.jar"
    avis_signal () {
        [ -f $avis_pidfile ] && kill -$1 `cat $avis_pidfile`
    }

%status -u @l_rusr@ -o
    avis_usable="unknown"
    avis_active="no"
    rcService avis enable yes && \
        avis_signal 0 && avis_active="yes"
    echo "avis_enable=\"$avis_enable\""
    echo "avis_usable=\"$avis_usable\""
    echo "avis_active=\"$avis_active\""

%start -u @l_rusr@
    rcService avis enable yes || exit 0
    rcService avis active yes && exit 0
    cmd="@l_prefix@/bin/java"
    cmd="$cmd -server"
    cmd="$cmd $avis_flags_jvm"
    cmd="$cmd -jar $avis_jarfile"
    cmd="$cmd -c $avis_cfgfile"
    cmd="$cmd $avis_flags"
    ( eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
      nohup $cmd </dev/null >$avis_logfile 2>&1 &
      echo $! >$avis_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService avis enable yes || exit 0
    shtool rotate -f \
        -n $avis_log_numfiles -s $avis_log_minsize -d \
        -z $avis_log_complevel -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "$avis_log_prolog" \
        -E "$avis_log_epilog; rc avis restart" \
        @l_prefix@/var/avis/avisd.log

