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

%config
    influxdb_enable="$openpkg_rc_def"
    influxdb_flags=""
    influxdb_log_prolog="true"
    influxdb_log_epilog="true"
    influxdb_log_numfiles="10"
    influxdb_log_minsize="1M"
    influxdb_log_complevel="9"

%common
    influxdb_cfgdir="@l_prefix@/etc/influxdb"
    influxdb_pidfile="@l_prefix@/var/influxdb/run/influxdb.pid"
    influxdb_logfile="@l_prefix@/var/influxdb/log/influxdb.log"
    influxdb_signal () {
        [ -f $influxdb_pidfile ] && kill -$1 `cat $influxdb_pidfile`
    }

%status -u @l_rusr@ -o
    influxdb_usable="unknown"
    influxdb_active="no"
    rcService influxdb enable yes && \
        influxdb_signal 0 && influxdb_active="yes"
    echo "influxdb_enable=\"$influxdb_enable\""
    echo "influxdb_usable=\"$influxdb_usable\""
    echo "influxdb_active=\"$influxdb_active\""

%start -u @l_rusr@
    rcService influxdb enable yes || exit 0
    rcService influxdb active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        export INFLUXD_CONFIG_PATH="$influxdb_cfgdir"
        nohup @l_prefix@/sbin/influxd run \
           $influxdb_flags \
        </dev/null >>$influxdb_logfile 2>&1 &
        echo $! >$influxdb_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService influxdb enable yes || exit 0
    shtool rotate -f \
        -n ${influxdb_log_numfiles} -s ${influxdb_log_minsize} -d \
        -z ${influxdb_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${influxdb_log_prolog}" \
        -E "${influxdb_log_epilog}; rc influxdb restart" \
        $influxdb_logfile

