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

%config
    ethereum_enable="$openpkg_rc_def"
    ethereum_flags="@flags@"
    ethereum_log_prolog="true"
    ethereum_log_epilog="true"
    ethereum_log_numfiles="10"
    ethereum_log_minsize="1M"
    ethereum_log_complevel="9"

%common
    ethereum_cfgfile="@l_prefix@/etc/ethereum/ethereum-geth.conf"
    ethereum_pidfile="@l_prefix@/var/ethereum/run/ethereum-geth.pid"
    ethereum_logfile="@l_prefix@/var/ethereum/log/ethereum-geth.log"
    ethereum_signal () {
        [ -f $ethereum_pidfile ] && kill -$1 `cat $ethereum_pidfile`
    }

%status -u @l_rusr@ -o
    ethereum_usable="unknown"
    ethereum_active="no"
    rcService ethereum enable yes && \
        ethereum_signal 0 && ethereum_active="yes"
    echo "ethereum_enable=\"$ethereum_enable\""
    echo "ethereum_usable=\"$ethereum_usable\""
    echo "ethereum_active=\"$ethereum_active\""

%start -u @l_rusr@
    rcService ethereum enable yes || exit 0
    rcService ethereum active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/bin/ethereum-geth \
            --config "$ethereum_cfgfile" \
            $ethereum_flags \
        </dev/null >>$ethereum_logfile 2>&1 &
        echo $! >$ethereum_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService ethereum enable yes || exit 0
    shtool rotate -f \
        -n ${ethereum_log_numfiles} -s ${ethereum_log_minsize} -d \
        -z ${ethereum_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${ethereum_log_prolog}" \
        -E "${ethereum_log_epilog}; rc ethereum restart" \
        $ethereum_logfile

