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

%config
    gobetween_enable="$openpkg_rc_def"
    gobetween_flags=""
    gobetween_log_prolog="true"
    gobetween_log_epilog="true"
    gobetween_log_numfiles="10"
    gobetween_log_minsize="1M"
    gobetween_log_complevel="9"

%common
    gobetween_cfgfile="@l_prefix@/etc/gobetween/gobetween.toml"
    gobetween_pidfile="@l_prefix@/var/gobetween/run/gobetween.pid"
    gobetween_logfile="@l_prefix@/var/gobetween/log/gobetween.log"
    gobetween_signal () {
        [ -f $gobetween_pidfile ] && kill -$1 `cat $gobetween_pidfile`
    }

%status -u @l_susr@ -o
    gobetween_usable="unknown"
    gobetween_active="no"
    rcService gobetween enable yes && \
        gobetween_signal 0 && gobetween_active="yes"
    echo "gobetween_enable=\"$gobetween_enable\""
    echo "gobetween_usable=\"$gobetween_usable\""
    echo "gobetween_active=\"$gobetween_active\""

%start -u @l_susr@
    rcService gobetween enable yes || exit 0
    rcService gobetween active yes && exit 0
    (   nohup @l_prefix@/sbin/gobetween \
           -c "$gobetween_cfgfile" \
           $gobetween_flags \
        </dev/null >>$gobetween_logfile 2>&1 &
        echo $! >$gobetween_pidfile
    ) >/dev/null 2>&1

%stop -u @l_susr@
    rcService gobetween enable yes || exit 0
    rcService gobetween active no  && exit 0
    gobetween_signal TERM
    sleep 2
    rm -f $gobetween_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService gobetween enable yes || exit 0
    shtool rotate -f \
        -n ${gobetween_log_numfiles} -s ${gobetween_log_minsize} -d \
        -z ${gobetween_log_complevel} -m 664 -o @l_susr@ -g @l_sgrp@ \
        -P "${gobetween_log_prolog}" \
        -E "${gobetween_log_epilog}; rc gobetween restart" \
        $gobetween_logfile

