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

%config
    valkey_enable="$openpkg_rc_def"
    valkey_log_prolog="true"
    valkey_log_epilog="true"
    valkey_log_numfiles="10"
    valkey_log_minsize="1M"
    valkey_log_complevel="9"

%common
    valkey_cluster="@cluster@"
    valkey_tls="@tls@"
    valkey_cfgfile="@l_prefix@/etc/valkey/valkey.conf"
    valkey_cfgfile_m="@l_prefix@/etc/valkey/valkey.master.conf"
    valkey_cfgfile_s="@l_prefix@/etc/valkey/valkey.slave.conf"
    valkey_cfgfile_t="@l_prefix@/etc/valkey/valkey-tls.conf"
    valkey_logfile="@l_prefix@/var/valkey/log/valkey.log"
    valkey_logfile_m="@l_prefix@/var/valkey/log/valkey.master.log"
    valkey_logfile_s="@l_prefix@/var/valkey/log/valkey.slave.log"
    valkey_logfile_t="@l_prefix@/var/valkey/log/valkey-tls.log"
    valkey_pidfile="@l_prefix@/var/valkey/run/valkey.pid"
    valkey_pidfile_m="@l_prefix@/var/valkey/run/valkey.master.pid"
    valkey_pidfile_s="@l_prefix@/var/valkey/run/valkey.slave.pid"
    valkey_pidfile_t="@l_prefix@/var/valkey/run/valkey-tls.pid"
    valkey_signal () {
        local a=0
        local t=0
        if [ $valkey_cluster = yes ]; then
            [ -f $valkey_pidfile_m ] && kill -$1 `cat $valkey_pidfile_m`
            local m=$?
            [ -f $valkey_pidfile_s ] && kill -$1 `cat $valkey_pidfile_s`
            local s=$?
            [ $m -eq 0 -o $s -eq 0 ]
            a=$?
        else
            [ -f $valkey_pidfile ] && kill -$1 `cat $valkey_pidfile`
            a=$?
        fi
        if [ $valkey_tls = yes ]; then
            [ -f $valkey_pidfile_t ] && kill -$1 `cat $valkey_pidfile_t`
            t=$?
        fi
        [ $a -eq 0 -a $t -eq 0 ]
    }

%status -u @l_rusr@ -o
    valkey_usable="unknown"
    valkey_active="no"
    rcService valkey enable yes && \
        valkey_signal 0 && valkey_active="yes"
    echo "valkey_enable=\"$valkey_enable\""
    echo "valkey_usable=\"$valkey_usable\""
    echo "valkey_active=\"$valkey_active\""

%start -u @l_rusr@
    rcService valkey enable yes || exit 0
    rcService valkey active yes && exit 0
    if [ $valkey_cluster = yes ]; then
        @l_prefix@/bin/valkey-server $valkey_cfgfile_m
        @l_prefix@/bin/valkey-server $valkey_cfgfile_s
    else
        @l_prefix@/bin/valkey-server $valkey_cfgfile
    fi
    if [ $valkey_tls = yes ]; then
        @l_prefix@/sbin/stunnel $valkey_cfgfile_t
    fi

%stop -u @l_rusr@
    rcService valkey enable yes || exit 0
    rcService valkey active no  && exit 0
    valkey_signal TERM
    sleep 2
    if [ $valkey_cluster = yes ]; then
        rm -f $valkey_pidfile_m >/dev/null 2>&1 || true
        rm -f $valkey_pidfile_s >/dev/null 2>&1 || true
    else
        rm -f $valkey_pidfile >/dev/null 2>&1 || true
    fi
    if [ $valkey_tls = yes ]; then
        rm -f $valkey_pidfile_t >/dev/null 2>&1 || true
    fi

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

%daily -u @l_rusr@
    rcService valkey enable yes || exit 0
    if [ $valkey_cluster = yes ]; then
        logfiles="$valkey_logfile_m $valkey_logfile_s"
    else
        logfiles="$valkey_logfile"
    fi
    if [ $valkey_tls = yes ]; then
        logfiles="$logfiles $valkey_logfile_t"
    fi
    shtool rotate -f \
        -n ${valkey_log_numfiles} -s ${valkey_log_minsize} -d \
        -z ${valkey_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${valkey_log_prolog}" \
        -E "${valkey_log_epilog}; rc valkey restart" \
        $logfiles

