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

%config
    pgpool_enable="$openpkg_rc_def"
    pgpool_instances="default"
    pgpool_log_prolog="true"
    pgpool_log_epilog="true"
    pgpool_log_numfiles="10"
    pgpool_log_minsize="1M"
    pgpool_log_complevel="9"

%common
    pgpool_etcdir="@l_prefix@/etc/pgpool"
    pgpool_vardir="@l_prefix@/var/pgpool"
    pgpool_signal () {
        [ -f $pgpool_vardir/$1.pid ] && kill -$2 `cat $pgpool_vardir/$1.pid`
    }
    pgpool_cfgfiles () {
        for instance in $pgpool_instances; do
            cfgfile=`echo "$instance" |\
                sed -e 's;^;X;' \
                    -e 's;Xdefault$;pgpool;' \
                    -e 's;X\(.*\)$;pgpool-\1;' \
                    -e 's;$;.conf;'`
            if [ -f "$pgpool_etcdir/$cfgfile" ]; then
                echo "$pgpool_etcdir/$cfgfile"
            fi
        done
    }

%status -u @l_nusr@ -o
    pgpool_usable="unknown"
    pgpool_active="yes"
    if rcService pgpool enable yes; then
        for cfgfile in `pgpool_cfgfiles`; do
            name=`echo "$cfgfile" | sed -e 's;^.*/\([^/]*\)$;\1;' -e 's;\.conf$;;'`
            pgpool_signal $name 0
            if [ $? -ne 0 ]; then
                pgpool_active="no"
                break
            fi
        done
    fi
    echo "pgpool_enable=\"$pgpool_enable\""
    echo "pgpool_usable=\"$pgpool_usable\""
    echo "pgpool_active=\"$pgpool_active\""

%start -p 200 -u @l_nusr@
    rcService pgpool enable yes || exit 0
    rcService pgpool active yes && exit 0
    for cfgfile in `pgpool_cfgfiles`; do
        @l_prefix@/sbin/pgpool -f $cfgfile
    done

%stop -p 800 -u @l_nusr@
    rcService pgpool enable yes || exit 0
    rcService pgpool active no && exit 0
    for cfgfile in `pgpool_cfgfiles`; do
        @l_prefix@/sbin/pgpool -f $cfgfile stop
    done

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

%daily -u @l_nusr@
    rcService pgpool enable yes || exit 0
    for cfgfile in `pgpool_cfgfiles`; do
        name=`echo "$cfgfile" | sed -e 's;^.*/\([^/]*\)$;\1;' -e 's;\.conf$;;'`
        shtool rotate -f \
            -n ${pgpool_log_numfiles} -s ${pgpool_log_minsize} -d \
            -z ${pgpool_log_complevel} -o @l_nusr@ -g @l_ngrp@ -m 644 \
            -P "${pgpool_log_prolog}" \
            -E "${pgpool_log_epilog}" \
            $pgpool_vardir/$name.log
    done

