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

%config
    grafana_enable="$openpkg_rc_def"
    grafana_log_prolog="true"
    grafana_log_epilog="true"
    grafana_log_numfiles="10"
    grafana_log_minsize="1M"
    grafana_log_complevel="9"

%common
    grafana_cfgfile="@l_prefix@/etc/grafana/grafana.ini"
    grafana_pidfile="@l_prefix@/var/grafana/run/grafana.pid"
    grafana_logfile="@l_prefix@/var/grafana/log/grafana.log"
    grafana_logdir="@l_prefix@/var/grafana/log"
    grafana_signal () {
        [ -f $grafana_pidfile ] && kill -$1 `cat $grafana_pidfile`
    }

%status -u @l_rusr@ -o
    grafana_usable="unknown"
    grafana_active="no"
    rcService grafana enable yes && \
        grafana_signal 0 && grafana_active="yes"
    echo "grafana_enable=\"$grafana_enable\""
    echo "grafana_usable=\"$grafana_usable\""
    echo "grafana_active=\"$grafana_active\""

%start -u @l_rusr@
    rcService grafana enable yes || exit 0
    rcService grafana active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/grafana \
            -config="$grafana_cfgfile" \
            -homepath="$grafana_logdir" \
        </dev/null >>$grafana_logfile 2>&1 &
        echo $! >$grafana_pidfile
    ) >/dev/null 2>&1

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

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

%reload -u @l_rusr@
    rcService grafana enable yes || exit 0
    rcService grafana active no  && exit 0
    @l_prefix@/sbin/grafana reload

%daily -u @l_susr@
    rcService grafana enable yes || exit 0
    shtool rotate -f \
        -n ${grafana_log_numfiles} -s ${grafana_log_minsize} -d \
        -z ${grafana_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${grafana_log_prolog}" \
        -E "${grafana_log_epilog}; rc grafana restart" \
        $grafana_logfile

