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

%config
    tsuru_enable="$openpkg_rc_def"
    tsuru_flags=""
    tsuru_log_prolog="true"
    tsuru_log_epilog="true"
    tsuru_log_numfiles="10"
    tsuru_log_minsize="1M"
    tsuru_log_complevel="9"

%common
    tsuru_cfgfile="@l_prefix@/etc/tsuru/hooks.json"
    tsuru_pidfile="@l_prefix@/var/tsuru/run/tsuru.pid"
    tsuru_logfile="@l_prefix@/var/tsuru/log/tsuru.log"
    tsuru_signal () {
        [ -f $tsuru_pidfile ] && kill -$1 `cat $tsuru_pidfile`
    }

%status -u @l_rusr@ -o
    tsuru_usable="unknown"
    tsuru_active="no"
    rcService tsuru enable yes && \
        tsuru_signal 0 && tsuru_active="yes"
    echo "tsuru_enable=\"$tsuru_enable\""
    echo "tsuru_usable=\"$tsuru_usable\""
    echo "tsuru_active=\"$tsuru_active\""

%start -u @l_rusr@
    rcService tsuru enable yes || exit 0
    rcService tsuru active yes && exit 0
    (   nohup @l_prefix@/sbin/tsurud $tsuru_flags \
        </dev/null >>$tsuru_logfile 2>&1 &
        echo $! >$tsuru_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService tsuru enable yes || exit 0
    shtool rotate -f \
        -n ${tsuru_log_numfiles} -s ${tsuru_log_minsize} -d \
        -z ${tsuru_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${tsuru_log_prolog}" \
        -E "${tsuru_log_epilog}; rc tsuru restart" \
        $tsuru_logfile

