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

%config
    gnats_enable="$openpkg_rc_def"
    gnats_bind="127.0.0.1"
    gnats_port="1529"
    gnats_flags=""
    gnats_log_prolog="true"
    gnats_log_epilog="true"
    gnats_log_numfiles="10"
    gnats_log_minsize="1M"
    gnats_log_complevel="9"

%common
    gnats_pidfile="@l_prefix@/var/gnats/gnats.pid"
    gnats_logfile="@l_prefix@/var/gnats/gnats.log"
    gnats_signal () {
        [ -f $gnats_pidfile ] && kill -$1 `cat $gnats_pidfile`
    }
    gnats_start () {
        ( @l_prefix@/libexec/gnats/gnatsdd ${gnats_bind}${gnats_bind:+:}${gnats_port} \
          @l_prefix@/libexec/gnats/gnatsd ${gnats_flags} \
          </dev/null >/dev/null 2>&1 &
          echo $! >$gnats_pidfile
        ) </dev/null >/dev/null 2>&1
    }

%status -u @l_susr@ -o
    gnats_usable="unknown"
    gnats_active="no"
    rcService gnats enable yes && \
        gnats_signal 0 && gnats_active="yes"
    echo "gnats_enable=\"$gnats_enable\""
    echo "gnats_usable=\"$gnats_usable\""
    echo "gnats_active=\"$gnats_active\""

%start -u @l_rusr@
    rcService gnats enable yes || exit 0
    rcService gnats active yes && exit 0
    gnats_start

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

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

%quarterly -u @l_rusr@
    rcService gnats enable yes || exit 0
    @l_prefix@/libexec/gnats/queue-pr --run

%daily -u @l_rusr@
    rcService gnats enable yes || exit 0
    shtool rotate -f \
        -n ${gnats_log_numfiles} -s ${gnats_log_minsize} -d \
        -z ${gnats_log_complevel} -m 664 -o @l_susr@ -g @l_mgrp@ \
        -P "${gnats_log_prolog}" \
        -E "${gnats_log_epilog}; rc gnats restart" \
        $gnats_logfile

