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

%config
    redis_enable="$openpkg_rc_def"
    redis_log_prolog="true"
    redis_log_epilog="true"
    redis_log_numfiles="10"
    redis_log_minsize="1M"
    redis_log_complevel="9"

%common
    redis_cluster="@cluster@"
    redis_tls="@tls@"
    redis_cfgfile="@l_prefix@/etc/redis/redis.conf"
    redis_cfgfile_m="@l_prefix@/etc/redis/redis.master.conf"
    redis_cfgfile_s="@l_prefix@/etc/redis/redis.slave.conf"
    redis_cfgfile_t="@l_prefix@/etc/redis/redis-tls.conf"
    redis_logfile="@l_prefix@/var/redis/log/redis.log"
    redis_logfile_m="@l_prefix@/var/redis/log/redis.master.log"
    redis_logfile_s="@l_prefix@/var/redis/log/redis.slave.log"
    redis_logfile_t="@l_prefix@/var/redis/log/redis-tls.log"
    redis_pidfile="@l_prefix@/var/redis/run/redis.pid"
    redis_pidfile_m="@l_prefix@/var/redis/run/redis.master.pid"
    redis_pidfile_s="@l_prefix@/var/redis/run/redis.slave.pid"
    redis_pidfile_t="@l_prefix@/var/redis/run/redis-tls.pid"
    redis_signal () {
        local a=0
        local t=0
        if [ $redis_cluster = yes ]; then
            [ -f $redis_pidfile_m ] && kill -$1 `cat $redis_pidfile_m`
            local m=$?
            [ -f $redis_pidfile_s ] && kill -$1 `cat $redis_pidfile_s`
            local s=$?
            [ $m -eq 0 -o $s -eq 0 ]
            a=$?
        else
            [ -f $redis_pidfile ] && kill -$1 `cat $redis_pidfile`
            a=$?
        fi
        if [ $redis_tls = yes ]; then
            [ -f $redis_pidfile_t ] && kill -$1 `cat $redis_pidfile_t`
            t=$?
        fi
        [ $a -eq 0 -a $t -eq 0 ]
    }

%status -u @l_rusr@ -o
    redis_usable="unknown"
    redis_active="no"
    rcService redis enable yes && \
        redis_signal 0 && redis_active="yes"
    echo "redis_enable=\"$redis_enable\""
    echo "redis_usable=\"$redis_usable\""
    echo "redis_active=\"$redis_active\""

%start -u @l_rusr@
    rcService redis enable yes || exit 0
    rcService redis active yes && exit 0
    if [ $redis_cluster = yes ]; then
        @l_prefix@/bin/redis-server $redis_cfgfile_m
        @l_prefix@/bin/redis-server $redis_cfgfile_s
    else
        @l_prefix@/bin/redis-server $redis_cfgfile
    fi
    if [ $redis_tls = yes ]; then
        @l_prefix@/sbin/stunnel $redis_cfgfile_t
    fi

%stop -u @l_rusr@
    rcService redis enable yes || exit 0
    rcService redis active no  && exit 0
    redis_signal TERM
    sleep 2
    if [ $redis_cluster = yes ]; then
        rm -f $redis_pidfile_m >/dev/null 2>&1 || true
        rm -f $redis_pidfile_s >/dev/null 2>&1 || true
    else
        rm -f $redis_pidfile >/dev/null 2>&1 || true
    fi
    if [ $redis_tls = yes ]; then
        rm -f $redis_pidfile_t >/dev/null 2>&1 || true
    fi

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

%daily -u @l_rusr@
    rcService redis enable yes || exit 0
    if [ $redis_cluster = yes ]; then
        logfiles="$redis_logfile_m $redis_logfile_s"
    else
        logfiles="$redis_logfile"
    fi
    if [ $redis_tls = yes ]; then
        logfiles="$logfiles $redis_logfile_t"
    fi
    shtool rotate -f \
        -n ${redis_log_numfiles} -s ${redis_log_minsize} -d \
        -z ${redis_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${redis_log_prolog}" \
        -E "${redis_log_epilog}; rc redis restart" \
        $logfiles

