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

%config
    influxdb_relay_enable="$openpkg_rc_def"
    influxdb_relay_flags=""
    influxdb_relay_log_prolog="true"
    influxdb_relay_log_epilog="true"
    influxdb_relay_log_numfiles="10"
    influxdb_relay_log_minsize="1M"
    influxdb_relay_log_complevel="9"

%common
    influxdb_relay_cfgfile="@l_prefix@/etc/influxdb-relay/influxdb-relay.conf"
    influxdb_relay_pidfile="@l_prefix@/var/influxdb-relay/run/influxdb-relay.pid"
    influxdb_relay_logfile="@l_prefix@/var/influxdb-relay/log/influxdb-relay.log"
    influxdb_relay_signal () {
        [ -f $influxdb_relay_pidfile ] && kill -$1 `cat $influxdb_relay_pidfile`
    }

%status -u @l_rusr@ -o
    influxdb_relay_usable="unknown"
    influxdb_relay_active="no"
    rcService influxdb-relay enable yes && \
        influxdb_relay_signal 0 && influxdb_relay_active="yes"
    echo "influxdb_relay_enable=\"$influxdb_relay_enable\""
    echo "influxdb_relay_usable=\"$influxdb_relay_usable\""
    echo "influxdb_relay_active=\"$influxdb_relay_active\""

%start -u @l_rusr@
    rcService influxdb-relay enable yes || exit 0
    rcService influxdb-relay active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/influxdb-relay \
           -config "$influxdb_relay_cfgfile" \
           $influxdb_relay_flags \
        </dev/null >>$influxdb_relay_logfile 2>&1 &
        echo $! >$influxdb_relay_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService influxdb-relay enable yes || exit 0
    rcService influxdb-relay active no  && exit 0
    influxdb_relay_signal TERM
    sleep 2
    rm -f $influxdb_relay_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService influxdb-relay enable yes || exit 0
    shtool rotate -f \
        -n ${influxdb_relay_log_numfiles} -s ${influxdb_relay_log_minsize} -d \
        -z ${influxdb_relay_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${influxdb_relay_log_prolog}" \
        -E "${influxdb_relay_log_epilog}; rc influxdb-relay restart" \
        $influxdb_relay_logfile

