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

%config
    postgresql_enable="$openpkg_rc_def"
    postgresql_flags=""
    postgresql_datadir="@l_prefix@/var/postgresql/db"
    postgresql_rundir="@l_prefix@/var/postgresql/run"
    postgresql_logdir="@l_prefix@/var/postgresql/log"
    postgresql_shut_mode="fast"
    postgresql_socket_inet_host="127.0.0.1"
    postgresql_socket_inet_port="5432"
    postgresql_socket_unix_path="@l_prefix@/var/postgresql/run"
    postgresql_log_prolog="true"
    postgresql_log_epilog="true"
    postgresql_log_numfiles="10"
    postgresql_log_minsize="1M"
    postgresql_log_complevel="9"

%common
    postgresql_opts="-h $postgresql_socket_inet_host -p $postgresql_socket_inet_port -k $postgresql_socket_unix_path"
    postgresql_opts="$postgresql_opts $postgresql_flags"
    postgresql_logfile="$postgresql_logdir/postgresql.log"
    postgresql_pidfile="$postgresql_datadir/postgresql.pid"
    postgresql_ctl () {
        if [ -s $postgresql_pidfile ]; then
            kill -0 `head -1 $postgresql_pidfile` >/dev/null 2>&1
            if [ $? -ne 0 ]; then
                rm -f $postgresql_pidfile    >/dev/null 2>&1 || true
                rm -f $postgresql_rundir/.s* >/dev/null 2>&1 || true
            fi
        fi
        cmd="$1"; shift
        @l_prefix@/bin/pg_ctl $cmd \
            -l $postgresql_logfile \
            -D $postgresql_datadir \
            ${1+"$@"}
    }

%status -u @l_rusr@ -o
    postgresql_usable="unknown"
    postgresql_active="no"
    rcService postgresql enable yes && \
        postgresql_ctl status >/dev/null && \
        postgresql_active="yes"
    echo "postgresql_enable=\"$postgresql_enable\""
    echo "postgresql_usable=\"$postgresql_usable\""
    echo "postgresql_active=\"$postgresql_active\""

%start -p 400 -u @l_rusr@
    rcService postgresql enable yes || exit 0
    rcService postgresql active yes && exit 0
    postgresql_ctl start -o "$postgresql_opts"

%stop -p 600 -u @l_rusr@
    rcService postgresql enable yes || exit 0
    rcService postgresql active no  && exit 0
    postgresql_ctl stop -m "$postgresql_shut_mode"

%restart -p 400 -u @l_rusr@
    rcService postgresql enable yes || exit 0
    rcService postgresql active no  && exit 0
    postgresql_ctl restart -o "$postgresql_opts" -m "$postgresql_shut_mode"

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

%daily -u @l_rusr@
    rcService postgresql enable yes || exit 0
    shtool rotate -f \
        -n ${postgresql_log_numfiles} -s ${postgresql_log_minsize} -d -c \
        -z ${postgresql_log_complevel} -m 600 -o @l_rusr@ -g @l_rgrp@ \
        -P "$postgresql_log_prolog" \
        -E "$postgresql_log_epilog" \
        $postgresql_logfile

