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

%config
    galera_enable="$openpkg_rc_def"
    galera_flags=""
    galera_log_prolog="true"
    galera_log_epilog="true"
    galera_log_numfiles="10"
    galera_log_minsize="1M"
    galera_log_complevel="9"

%common
    galera_cfgfile="@l_prefix@/etc/galera/galera.conf"
    galera_pidfile="@l_prefix@/var/galera/run/galera.pid"
    galera_logfile="@l_prefix@/var/galera/log/galera.log"
    galera_signal () {
        [ -f $galera_pidfile ] && kill -$1 `cat $galera_pidfile`
    }

%status -u @l_rusr@ -o
    galera_usable="unknown"
    galera_active="no"
    rcService galera enable yes && \
        galera_signal 0 && galera_active="yes"
    echo "galera_enable=\"$galera_enable\""
    echo "galera_usable=\"$galera_usable\""
    echo "galera_active=\"$galera_active\""

%start -u @l_rusr@
    rcService galera enable yes || exit 0
    rcService galera active yes && exit 0
    (   nohup @l_prefix@/sbin/garbd \
            --cfg "$galera_cfgfile" \
            --log "$galera_logfile" \
            $galera_flags \
        </dev/null >>$galera_logfile 2>&1 &
        echo $! >$galera_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService galera enable yes || exit 0
    shtool rotate -f \
        -n ${galera_log_numfiles} -s ${galera_log_minsize} -d \
        -z ${galera_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${galera_log_prolog}" \
        -E "${galera_log_epilog}; rc galera restart" \
        $galera_logfile

