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

%config
    cassandra_enable="$openpkg_rc_def"
    cassandra_flags=""
    cassandra_log_prolog="true"
    cassandra_log_epilog="true"
    cassandra_log_numfiles="10"
    cassandra_log_minsize="1M"
    cassandra_log_complevel="9"

%common
    cassandra_pidfile="@l_prefix@/var/cassandra/run/cassandra.pid"
    cassandra_dmpfile="@l_prefix@/var/cassandra/run/cassandra.dump"
    cassandra_logfile="@l_prefix@/var/cassandra/log/cassandra.error.log"
    cassandra_signal () {
        [ -f $cassandra_pidfile ] && kill -$1 `cat $cassandra_pidfile`
    }

%status -u @l_rusr@ -o
    cassandra_usable="unknown"
    cassandra_active="no"
    rcService cassandra enable yes && \
        cassandra_signal 0 && cassandra_active="yes"
    echo "cassandra_enable=\"$cassandra_enable\""
    echo "cassandra_usable=\"$cassandra_usable\""
    echo "cassandra_active=\"$cassandra_active\""

%start -u @l_rusr@
    rcService cassandra enable yes || exit 0
    rcService cassandra active yes && exit 0
    cmd="CASSANDRA_CONF=@l_prefix@/etc/cassandra"
    cmd="$cmd nohup @l_prefix@/libexec/cassandra/cassandra"
    cmd="$cmd -p $cassandra_pidfile"
    cmd="$cmd -H $cassandra_dmpfile"
    cmd="$cmd -E $cassandra_logfile"
    cmd="$cmd $cassandra_flags"
    ( eval "$cmd </dev/null >$cassandra_logfile 2>&1 &"
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService cassandra enable yes || exit 0
    shtool rotate -f \
        -n $cassandra_log_numfiles -s $cassandra_log_minsize -d \
        -z $cassandra_log_complevel -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "$cassandra_log_prolog" \
        -E "$cassandra_log_epilog; rc cassandra restart" \
        $cassandra_logfile

