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

%config
    pgcat_enable="$openpkg_rc_def"
    pgcat_flags=""
    pgcat_log_prolog="true"
    pgcat_log_epilog="true"
    pgcat_log_numfiles="10"
    pgcat_log_minsize="1M"
    pgcat_log_complevel="9"

%common
    pgcat_cfgfile="@l_prefix@/etc/pgcat/pgcat.conf"
    pgcat_pidfile="@l_prefix@/var/pgcat/run/pgcat.pid"
    pgcat_logfile="@l_prefix@/var/pgcat/log/pgcat.log"
    pgcat_signal () {
        [ -f $pgcat_pidfile ] && kill -$1 `cat $pgcat_pidfile`
    }

%status -u @l_rusr@ -o
    pgcat_usable="unknown"
    pgcat_active="no"
    rcService pgcat enable yes && \
        pgcat_signal 0 && pgcat_active="yes"
    echo "pgcat_enable=\"$pgcat_enable\""
    echo "pgcat_usable=\"$pgcat_usable\""
    echo "pgcat_active=\"$pgcat_active\""

%start -u @l_rusr@
    rcService pgcat enable yes || exit 0
    rcService pgcat active yes && exit 0
    (   nohup @l_prefix@/bin/pgcat \
            `sed -e "/^ *#/d" -e '/^ *$/d' -e "s;^;--;" <$pgcat_cfgfile | \
            sed -e ":a" -e "N" -e '$!ba' -e 's/\n/ /g'` \
            $pgcat_flags \
        </dev/null >>$pgcat_logfile 2>&1 &
        echo $! >$pgcat_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService pgcat enable yes || exit 0
    shtool rotate -f \
        -n ${pgcat_log_numfiles} -s ${pgcat_log_minsize} -d \
        -z ${pgcat_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${pgcat_log_prolog}" \
        -E "${pgcat_log_epilog}; rc pgcat restart" \
        $pgcat_logfile

