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

%config
    postgresql_repmgr_enable="$openpkg_rc_def"
    postgresql_repmgr_flags=""
    postgresql_repmgr_log_prolog="true"
    postgresql_repmgr_log_epilog="true"
    postgresql_repmgr_log_numfiles="10"
    postgresql_repmgr_log_minsize="1M"
    postgresql_repmgr_log_complevel="9"

%common
    postgresql_repmgr_pidfile="@l_prefix@/var/postgresql-repmgr/run/postgresql-repmgr.pid"
    postgresql_repmgr_logfile="@l_prefix@/var/postgresql-repmgr/log/postgresql-repmgr.log"
    postgresql_repmgr_signal () {
        [ -f $postgresql_repmgr_pidfile ] && kill -$1 `cat $postgresql_repmgr_pidfile`
    }

%status -u @l_rusr@ -o
    postgresql_repmgr_usable="unknown"
    postgresql_repmgr_active="no"
    rcService postgresql-repmgr enable yes && \
        postgresql_repmgr_signal 0 && postgresql_repmgr_active="yes"
    echo "postgresql_repmgr_enable=\"$postgresql_repmgr_enable\""
    echo "postgresql_repmgr_usable=\"$postgresql_repmgr_usable\""
    echo "postgresql_repmgr_active=\"$postgresql_repmgr_active\""

%start -p 410 -u @l_rusr@
    rcService postgresql-repmgr enable yes || exit 0
    rcService postgresql-repmgr active yes && exit 0
    @l_prefix@/bin/repmgrd -d -p $postgresql_repmgr_pidfile $postgresql_repmgr_flags

%stop -p 590 -u @l_rusr@
    rcService postgresql-repmgr enable yes || exit 0
    rcService postgresql-repmgr active no  && exit 0
    postgresql_repmgr__signal TERM
    sleep 2
    rm -f $postgresql_repmgr_pidfile >/dev/null 2>&1 || true

%restart -p 410 -u @l_rusr@
    rcService postgresql-repmgr enable yes || exit 0
    rcService postgresql-repmgr active no  && exit 0
    rc postgresql-repmgr stop start

%daily -u @l_rusr@
    rcService postgresql-repmgr enable yes || exit 0
    shtool rotate -f \
        -n ${postgresql_repmgr_log_numfiles} -s ${postgresql_repmgr_log_minsize} -d -c \
        -z ${postgresql_repmgr_log_complevel} -m 600 -o @l_rusr@ -g @l_rgrp@ \
        -P "$postgresql_repmgr_log_prolog" \
        -E "$postgresql_repmgr_log_epilog; rc postgresql-repmgr restart" \
        $postgresql_repmgr_logfile

