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

%config
    postgresxl_enable="$openpkg_rc_def"
    postgresxl_flags=""
    postgresxl_datadir="@l_prefix@/var/postgresql/db"
    postgresxl_rundir="@l_prefix@/var/postgresql/run"
    postgresxl_shut_mode="fast"
    postgresxl_socket_inet_host="127.0.0.1"
    postgresxl_socket_inet_port="5432"
    postgresxl_socket_unix_path="@l_prefix@/var/postgresql/run"
    postgresxl_log_prolog="true"
    postgresxl_log_epilog="true"
    postgresxl_log_numfiles="10"
    postgresxl_log_minsize="1M"
    postgresxl_log_complevel="9"

%common
    postgresxl_opts="-h $postgresxl_socket_inet_host -p $postgresxl_socket_inet_port -k $postgresxl_socket_unix_path"
    postgresxl_opts="$postgresxl_opts $postgresxl_flags"
    postgresxl_logfile="$postgresxl_rundir/postgresql.log"
    postgresxl_pidfile="$postgresxl_datadir/postgresql.pid"
    postgresxl_ctl () {
        if [ -s $postgresxl_pidfile ]; then
            kill -0 `head -1 $postgresxl_pidfile` >/dev/null 2>&1
            if [ $? -ne 0 ]; then
                rm -f $postgresxl_pidfile    >/dev/null 2>&1 || true
                rm -f $postgresxl_rundir/.s* >/dev/null 2>&1 || true
            fi
        fi
        cmd="$1"; shift
        @l_prefix@/bin/pg_ctl $cmd \
            -l $postgresxl_logfile \
            -D $postgresxl_datadir \
            ${1+"$@"}
    }

%status -u @l_rusr@ -o
    postgresxl_usable="unknown"
    postgresxl_active="no"
    rcService postgresxl enable yes && \
        postgresxl_ctl status >/dev/null && \
        postgresxl_active="yes"
    echo "postgresxl_enable=\"$postgresxl_enable\""
    echo "postgresxl_usable=\"$postgresxl_usable\""
    echo "postgresxl_active=\"$postgresxl_active\""

%start -p 400 -u @l_rusr@
    rcService postgresxl enable yes || exit 0
    rcService postgresxl active yes && exit 0
    postgresxl_ctl start -o "$postgresxl_opts"

%stop -p 600 -u @l_rusr@
    rcService postgresxl enable yes || exit 0
    rcService postgresxl active no  && exit 0
    postgresxl_ctl stop -m "$postgresxl_shut_mode"

%restart -p 400 -u @l_rusr@
    rcService postgresxl enable yes || exit 0
    rcService postgresxl active no  && exit 0
    postgresxl_ctl restart -o "$postgresxl_opts" -m "$postgresxl_shut_mode"

%reload -p 400 -u @l_rusr@
    rcService postgresxl enable yes || exit 0
    rcService postgresxl active no  && exit 0
    postgresxl_ctl reload

%daily -u @l_rusr@
    rcService postgresxl enable yes || exit 0
    shtool rotate -f \
        -n ${postgresxl_log_numfiles} -s ${postgresxl_log_minsize} -d -c \
        -z ${postgresxl_log_complevel} -m 600 -o @l_rusr@ -g @l_rgrp@ \
        -P "$postgresxl_log_prolog" \
        -E "$postgresxl_log_epilog" \
        $postgresxl_logfile

