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

%config
    beanstalkd_enable="$openpkg_rc_def"
    beanstalkd_flags="-l 127.0.0.1 -p 11300 -f 500"
    beanstalkd_log_prolog="true"
    beanstalkd_log_epilog="true"
    beanstalkd_log_numfiles="10"
    beanstalkd_log_minsize="1M"
    beanstalkd_log_complevel="9"

%common
    beanstalkd_waldir="@l_prefix@/var/beanstalkd/db"
    beanstalkd_logfile="@l_prefix@/var/beanstalkd/log/beanstalkd.log"
    beanstalkd_pidfile="@l_prefix@/var/beanstalkd/run/beanstalkd.pid"
    beanstalkd_signal () {
        [ -f $beanstalkd_pidfile ] && kill -$1 `cat $beanstalkd_pidfile`
    }

%status -u @l_rusr@ -o
    beanstalkd_usable="unknown"
    beanstalkd_active="no"
    rcService beanstalkd enable yes && \
        beanstalkd_signal 0 && beanstalkd_active="yes"
    echo "beanstalkd_enable=\"$beanstalkd_enable\""
    echo "beanstalkd_usable=\"$beanstalkd_usable\""
    echo "beanstalkd_active=\"$beanstalkd_active\""

%start -u @l_rusr@
    rcService beanstalkd enable yes || exit 0
    rcService beanstalkd active yes && exit 0
    (   nohup @l_prefix@/bin/beanstalkd \
            -V \
            -b $beanstalkd_waldir \
            $beanstalkd_flags \
            </dev/null >>$beanstalkd_logfile 2>&1 &
        echo $! >$beanstalkd_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService beanstalkd enable yes || exit 0
    rcService beanstalkd active no  && exit 0
    beanstalkd_signal TERM
    sleep 2
    rm -f $beanstalkd_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService beanstalkd enable yes || exit 0
    shtool rotate -f \
        -n ${beanstalkd_log_numfiles} -s ${beanstalkd_log_minsize} -d \
        -z ${beanstalkd_log_complevel} -m 664 -o @l_susr@ -g @l_mgrp@ \
        -P "${beanstalkd_log_prolog}" \
        -E "${beanstalkd_log_epilog}; rc beanstalkd restart" \
        $beanstalkd_logfile

