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

%config
    hyperledger_burrow_enable="$openpkg_rc_def"
    hyperledger_burrow_flags="--validator-index=0"
    hyperledger_burrow_log_prolog="true"
    hyperledger_burrow_log_epilog="true"
    hyperledger_burrow_log_numfiles="10"
    hyperledger_burrow_log_minsize="1M"
    hyperledger_burrow_log_complevel="9"

%common
    hyperledger_burrow_homedir="@l_prefix@/var/hyperledger-burrow/db"
    hyperledger_burrow_pidfile="@l_prefix@/var/hyperledger-burrow/run/hyperledger-burrow.pid"
    hyperledger_burrow_logfile="@l_prefix@/var/hyperledger-burrow/log/hyperledger-burrow.log"
    hyperledger_burrow_signal () {
        [ -f $hyperledger_burrow_pidfile ] && kill -$1 `cat $hyperledger_burrow_pidfile`
    }

%status -u @l_rusr@ -o
    hyperledger_burrow_usable="unknown"
    hyperledger_burrow_active="no"
    rcService hyperledger-burrow enable yes && \
        hyperledger_burrow_signal 0 && hyperledger_burrow_active="yes"
    echo "hyperledger_burrow_enable=\"$hyperledger_burrow_enable\""
    echo "hyperledger_burrow_usable=\"$hyperledger_burrow_usable\""
    echo "hyperledger_burrow_active=\"$hyperledger_burrow_active\""

%start -u @l_rusr@
    rcService hyperledger-burrow enable yes || exit 0
    rcService hyperledger-burrow active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        cd $hyperledger_burrow_homedir || exit $?
        @l_prefix@/sbin/burrow start $hyperledger_burrow_flags \
            </dev/null >>$hyperledger_burrow_logfile 2>&1 &
        echo $! >$hyperledger_burrow_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService hyperledger-burrow enable yes || exit 0
    rcService hyperledger-burrow active no  && exit 0
    hyperledger_burrow_signal TERM
    sleep 2
    rm -f $hyperledger_burrow_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService hyperledger-burrow enable yes || exit 0
    shtool rotate -f \
        -n ${hyperledger_burrow_log_numfiles} -s ${hyperledger_burrow_log_minsize} -d \
        -z ${hyperledger_burrow_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${hyperledger_burrow_log_prolog}" \
        -E "${hyperledger_burrow_log_epilog}; rc hyperledger-burrow restart" \
        $hyperledger_burrow_logfile

