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

%config
    liftbridge_enable="$openpkg_rc_def"
    liftbridge_flags=""
    liftbridge_log_prolog="true"
    liftbridge_log_epilog="true"
    liftbridge_log_numfiles="10"
    liftbridge_log_minsize="1M"
    liftbridge_log_complevel="9"

%common
    liftbridge_cfgfile="@l_prefix@/etc/liftbridge/liftbridge.conf"
    liftbridge_pidfile="@l_prefix@/var/liftbridge/run/liftbridge.pid"
    liftbridge_logfile="@l_prefix@/var/liftbridge/log/liftbridge.log"
    liftbridge_signal () {
        [ -f $liftbridge_pidfile ] && kill -$1 `cat $liftbridge_pidfile`
    }

%status -u @l_rusr@ -o
    liftbridge_usable="unknown"
    liftbridge_active="no"
    rcService liftbridge enable yes && \
        liftbridge_signal 0 && liftbridge_active="yes"
    echo "liftbridge_enable=\"$liftbridge_enable\""
    echo "liftbridge_usable=\"$liftbridge_usable\""
    echo "liftbridge_active=\"$liftbridge_active\""

%start -u @l_rusr@
    rcService liftbridge enable yes || exit 0
    rcService liftbridge active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/liftbridge \
           --config "$liftbridge_cfgfile" \
           $liftbridge_flags \
        </dev/null >>$liftbridge_logfile 2>&1 &
        echo $! >$liftbridge_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService liftbridge enable yes || exit 0
    shtool rotate -f \
        -n ${liftbridge_log_numfiles} -s ${liftbridge_log_minsize} -d \
        -z ${liftbridge_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${liftbridge_log_prolog}" \
        -E "${liftbridge_log_epilog}; rc liftbridge restart" \
        $liftbridge_logfile

