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

%config
    rqlite_enable="$openpkg_rc_def"
    rqlite_flags="-http 127.0.0.1:4001 -raft 127.0.0.1:4002"
    rqlite_log_prolog="true"
    rqlite_log_epilog="true"
    rqlite_log_numfiles="10"
    rqlite_log_minsize="1M"
    rqlite_log_complevel="9"

%common
    rqlite_dbdir="@l_prefix@/var/rqlite/db"
    rqlite_logdir="@l_prefix@/var/rqlite/log"
    rqlite_pidfile="@l_prefix@/var/rqlite/run/rqlite.pid"
    rqlite_logfile="@l_prefix@/var/rqlite/log/rqlite.log"
    rqlite_signal () {
        [ -f $rqlite_pidfile ] && kill -$1 `cat $rqlite_pidfile`
    }

%status -u @l_rusr@ -o
    rqlite_usable="unknown"
    rqlite_active="no"
    rcService rqlite enable yes && \
        rqlite_signal 0 && rqlite_active="yes"
    echo "rqlite_enable=\"$rqlite_enable\""
    echo "rqlite_usable=\"$rqlite_usable\""
    echo "rqlite_active=\"$rqlite_active\""

%start -u @l_rusr@
    rcService rqlite enable yes || exit 0
    rcService rqlite active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/rqlited \
            $rqlite_flags \
            $rqlite_dbdir \
        </dev/null >>$rqlite_logfile 2>&1 &
        echo $! >$rqlite_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService rqlite enable yes || exit 0
    shtool rotate -f \
        -n ${rqlite_log_numfiles} -s ${rqlite_log_minsize} -d \
        -z ${rqlite_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${rqlite_log_prolog}" \
        -E "${rqlite_log_epilog}; rc rqlite restart" \
        $rqlite_logfile

