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

%config
    tendermint_enable="$openpkg_rc_def"
    tendermint_flags="@flags@"
    tendermint_log_prolog="true"
    tendermint_log_epilog="true"
    tendermint_log_numfiles="10"
    tendermint_log_minsize="1M"
    tendermint_log_complevel="9"

%common
    tendermint_homedir="@l_prefix@/var/tendermint/db"
    tendermint_pidfile="@l_prefix@/var/tendermint/run/tendermint.pid"
    tendermint_logfile="@l_prefix@/var/tendermint/log/tendermint.log"
    tendermint_signal () {
        [ -f $tendermint_pidfile ] && kill -$1 `cat $tendermint_pidfile`
    }

%status -u @l_rusr@ -o
    tendermint_usable="unknown"
    tendermint_active="no"
    rcService tendermint enable yes && \
        tendermint_signal 0 && tendermint_active="yes"
    echo "tendermint_enable=\"$tendermint_enable\""
    echo "tendermint_usable=\"$tendermint_usable\""
    echo "tendermint_active=\"$tendermint_active\""

%start -u @l_rusr@
    rcService tendermint enable yes || exit 0
    rcService tendermint active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        cmd="nohup @l_prefix@/bin/tendermint node"
        cmd="$cmd --home \"$tendermint_homedir\""
        cmd="$cmd $tendermint_flags"
        cmd="$cmd </dev/null >>$tendermint_logfile 2>&1 &"
        eval "$cmd"
        echo $! >$tendermint_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService tendermint enable yes || exit 0
    shtool rotate -f \
        -n ${tendermint_log_numfiles} -s ${tendermint_log_minsize} -d \
        -z ${tendermint_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${tendermint_log_prolog}" \
        -E "${tendermint_log_epilog}; rc tendermint restart" \
        $tendermint_logfile

