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

%config
    quorum_enable="$openpkg_rc_def"
    quorum_flags="@flags@"
    quorum_log_prolog="true"
    quorum_log_epilog="true"
    quorum_log_numfiles="10"
    quorum_log_minsize="1M"
    quorum_log_complevel="9"

%common
    quorum_cfgfile="@l_prefix@/etc/quorum/quorum-geth.conf"
    quorum_pidfile="@l_prefix@/var/quorum/run/quorum-geth.pid"
    quorum_logfile="@l_prefix@/var/quorum/log/quorum-geth.log"
    quorum_signal () {
        [ -f $quorum_pidfile ] && kill -$1 `cat $quorum_pidfile`
    }

%status -u @l_rusr@ -o
    quorum_usable="unknown"
    quorum_active="no"
    rcService quorum enable yes && \
        quorum_signal 0 && quorum_active="yes"
    echo "quorum_enable=\"$quorum_enable\""
    echo "quorum_usable=\"$quorum_usable\""
    echo "quorum_active=\"$quorum_active\""

%start -u @l_rusr@
    rcService quorum enable yes || exit 0
    rcService quorum active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/bin/quorum-geth \
            --config "$quorum_cfgfile" \
            $quorum_flags \
        </dev/null >>$quorum_logfile 2>&1 &
        echo $! >$quorum_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService quorum enable yes || exit 0
    shtool rotate -f \
        -n ${quorum_log_numfiles} -s ${quorum_log_minsize} -d \
        -z ${quorum_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${quorum_log_prolog}" \
        -E "${quorum_log_epilog}; rc quorum restart" \
        $quorum_logfile

