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

%config
    consul_replicate_enable="$openpkg_rc_def"
    consul_replicate_flags=""
    consul_replicate_log_prolog="true"
    consul_replicate_log_epilog="true"
    consul_replicate_log_numfiles="10"
    consul_replicate_log_minsize="1M"
    consul_replicate_log_complevel="9"

%common
    consul_replicate_cfgfile="@l_prefix@/etc/consul-replicate/consul-replicate.conf"
    consul_replicate_pidfile="@l_prefix@/var/consul-replicate/run/consul-replicate.pid"
    consul_replicate_logfile="@l_prefix@/var/consul-replicate/log/consul-replicate.log"
    consul_replicate_signal () {
        [ -f $consul_replicate_pidfile ] && kill -$1 `cat $consul_replicate_pidfile`
    }

%status -u @l_rusr@ -o
    consul_replicate_usable="unknown"
    consul_replicate_active="no"
    rcService consul-replicate enable yes && \
        consul_replicate_signal 0 && consul_replicate_active="yes"
    echo "consul_replicate_enable=\"$consul_replicate_enable\""
    echo "consul_replicate_usable=\"$consul_replicate_usable\""
    echo "consul_replicate_active=\"$consul_replicate_active\""

%start -u @l_rusr@
    rcService consul-replicate enable yes || exit 0
    rcService consul-replicate active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/consul-replicate \
            -config="$consul_replicate_cfgfile" \
            $consul_replicate_flags \
        </dev/null >>$consul_replicate_logfile 2>&1 &
        echo $! >$consul_replicate_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService consul-replicate enable yes || exit 0
    rcService consul-replicate active no  && exit 0
    consul_replicate_signal TERM
    sleep 2
    rm -f $consul_replicate_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService consul-replicate enable yes || exit 0
    shtool rotate -f \
        -n ${consul_replicate_log_numfiles} -s ${consul_replicate_log_minsize} -d \
        -z ${consul_replicate_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${consul_replicate_log_prolog}" \
        -E "${consul_replicate_log_epilog}; rc consul-replicate restart" \
        $consul_replicate_logfile

