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

%config
    kafka_enable="$openpkg_rc_def"
    kafka_flags="-server -Xms1G -Xmx1G"
    kafka_log_prolog="true"
    kafka_log_epilog="true"
    kafka_log_numfiles="10"
    kafka_log_minsize="1M"
    kafka_log_complevel="9"

%common
    kafka_pidfile="@l_prefix@/var/kafka/run/kafka.pid"
    kafka_logfile="@l_prefix@/var/kafka/log/kafka.log"
    kafka_signal () {
        [ -f $kafka_pidfile ] && kill -$1 `cat $kafka_pidfile`
    }

%status -u @l_rusr@ -o
    kafka_usable="unknown"
    kafka_active="no"
    rcService kafka enable yes && \
        kafka_signal 0 && kafka_active="yes"
    echo "kafka_enable=\"$kafka_enable\""
    echo "kafka_usable=\"$kafka_usable\""
    echo "kafka_active=\"$kafka_active\""

%start -u @l_rusr@
    rcService kafka enable yes || exit 0
    rcService kafka active yes && exit 0
    (   eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
        cmd="@l_prefix@/libexec/kafka/bin/kafka-server-start.sh"
        cmd="$cmd @l_prefix@/etc/kafka/server.properties"
        eval "nohup $cmd </dev/null >>$kafka_logfile 2>&1 &"
        echo $! >$kafka_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService kafka enable yes || exit 0
    shtool rotate -f \
        -n $kafka_log_numfiles -s $kafka_log_minsize -d \
        -z $kafka_log_complevel -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "$kafka_log_prolog" \
        -E "$kafka_log_epilog; rc kafka restart" \
        $kafka_logfile

