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

%config
    cratedb_enable="$openpkg_rc_def"
    cratedb_flags=""
    cratedb_log_prolog="true"
    cratedb_log_epilog="true"
    cratedb_log_numfiles="10"
    cratedb_log_minsize="1M"
    cratedb_log_complevel="9"

%common
    cratedb_pidfile="@l_prefix@/var/cratedb/run/cratedb.pid"
    cratedb_logfile="@l_prefix@/var/cratedb/log/cratedb.log"
    cratedb_signal () {
        [ -f $cratedb_pidfile ] && kill -$1 `cat $cratedb_pidfile`
    }

%status -u @l_rusr@ -o
    cratedb_usable="unknown"
    cratedb_active="no"
    rcService cratedb enable yes && \
        cratedb_signal 0 && cratedb_active="yes"
    echo "cratedb_enable=\"$cratedb_enable\""
    echo "cratedb_usable=\"$cratedb_usable\""
    echo "cratedb_active=\"$cratedb_active\""

%start -u @l_rusr@
    rcService cratedb enable yes || exit 0
    rcService cratedb active yes && exit 0
    ( eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
      cmd="@l_prefix@/lib/cratedb/bin/crate"
      cmd="$cmd -d -p $cratedb_pidfile"
      cmd="$cmd $cratedb_flags"
      eval "nohup $cmd </dev/null >$cratedb_logfile 2>&1"
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService cratedb enable yes || exit 0
    shtool rotate -f \
        -n $cratedb_log_numfiles -s $cratedb_log_minsize -d \
        -z $cratedb_log_complevel -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "$cratedb_log_prolog" \
        -E "$cratedb_log_epilog; rc cratedb restart" \
        $cratedb_logfile

