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

%config
    pgbouncer_enable="$openpkg_rc_def"
    pgbouncer_flags=""
    pgbouncer_log_prolog="true"
    pgbouncer_log_epilog="true"
    pgbouncer_log_numfiles="10"
    pgbouncer_log_minsize="1M"
    pgbouncer_log_complevel="9"

%common
    pgbouncer_cfgfile="@l_prefix@/etc/pgbouncer/pgbouncer.ini"
    pgbouncer_pidfile="@l_prefix@/var/pgbouncer/run/pgbouncer.pid"
    pgbouncer_logfile="@l_prefix@/var/pgbouncer/log/pgbouncer.log"
    pgbouncer_signal () {
        [ -f $pgbouncer_pidfile ] && kill -$1 `cat $pgbouncer_pidfile`
    }

%status -u @l_rusr@ -o
    pgbouncer_usable="unknown"
    pgbouncer_active="no"
    rcService pgbouncer enable yes && \
        pgbouncer_signal 0 && pgbouncer_active="yes"
    echo "pgbouncer_enable=\"$pgbouncer_enable\""
    echo "pgbouncer_usable=\"$pgbouncer_usable\""
    echo "pgbouncer_active=\"$pgbouncer_active\""

%start -u @l_rusr@
    rcService pgbouncer enable yes || exit 0
    rcService pgbouncer active yes && exit 0
    @l_prefix@/bin/pgbouncer \
        --daemon \
        $pgbouncer_flags \
        $pgbouncer_cfgfile \
        </dev/null >>$pgbouncer_logfile 2>&1

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

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

%daily -u @l_susr@
    rcService pgbouncer enable yes || exit 0
    shtool rotate -f \
        -n ${pgbouncer_log_numfiles} -s ${pgbouncer_log_minsize} -d \
        -z ${pgbouncer_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${pgbouncer_log_prolog}" \
        -E "${pgbouncer_log_epilog}; rc pgbouncer restart" \
        $pgbouncer_logfile

