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

%config
    zookeeper_enable="$openpkg_rc_def"
    zookeeper_log_prolog="true"
    zookeeper_log_epilog="true"
    zookeeper_log_numfiles="10"
    zookeeper_log_minsize="1M"
    zookeeper_log_complevel="9"

%common
    zookeeper_bindir="@l_prefix@/libexec/zookeeper/bin"
    zookeeper_pidfile="@l_prefix@/var/zookeeper/run/zookeeper.pid"
    zookeeper_logfile="@l_prefix@/var/zookeeper/log/zookeeper.log"
    zookeeper_logdir="@l_prefix@/var/zookeeper/log"
    zookeeper_signal () {
        [ -f $zookeeper_pidfile ] && kill -$1 `cat $zookeeper_pidfile`
    }
    zookeeper_start () {
        eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
        ZOOPIDFILE=$zookeeper_pidfile \
        ZOO_LOG_DIR=$zookeeper_logdir \
        $zookeeper_bindir/zkServer.sh start
    }
    zookeeper_stop () {
        eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
        ZOOPIDFILE=$zookeeper_pidfile \
        ZOO_LOG_DIR=$zookeeper_logdir \
        $zookeeper_bindir/zkServer.sh stop
        sleep 2
        zookeeper_signal TERM
        sleep 2
        rm -f $zookeeper_pidfile >/dev/null 2>&1 || true
    }

%status -u @l_nusr@ -o
    zookeeper_usable="unknown"
    zookeeper_active="no"
    rcService zookeeper enable yes && \
        zookeeper_signal 0 && zookeeper_active="yes"
    echo "zookeeper_enable=\"$zookeeper_enable\""
    echo "zookeeper_usable=\"$zookeeper_usable\""
    echo "zookeeper_active=\"$zookeeper_active\""

%start -u @l_nusr@
    rcService zookeeper enable yes || exit 0
    rcService zookeeper active yes && exit 0
    zookeeper_start

%stop -u @l_nusr@
    rcService zookeeper enable yes || exit 0
    rcService zookeeper active no  && exit 0
    zookeeper_stop

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

%daily -u @l_susr@
    rcService zookeeper enable yes || exit 0
    shtool rotate -f \
        -n $zookeeper_stats_numfiles -s $zookeeper_stats_minsize -d \
        -z $zookeeper_stats_complevel -m 644 -o @l_nusr@ -g @l_ngrp@ \
        -P "$zookeeper_stats_prolog" \
        -E "$zookeeper_stats_epilog" \
        $zookeeper_logfile

