#!/bin/sh
#
# Redis init script for Debian-based distros
#
#
### BEGIN INIT INFO
# Provides:          sentinel_{{ redis_sentinel_port }}
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      $syslog $named
# Should-Stop:       $syslog $named
# Short-Description: Start and stop sentinel_{{ redis_sentinel_port }}
# Description:       Redis Sentinel monitor
### END INIT INFO

# Source the Linux Standard Base functions
. /lib/lsb/init-functions

SENTINEL_PORT={{ redis_sentinel_port }}
NAME="sentinel_${SENTINEL_PORT}"
DAEMON={{ redis_install_dir }}/bin/redis-server
PIDFILE={{ redis_sentinel_pidfile }}
PIDFILE_DIR=$(dirname "${PIDFILE}")

REDIS_USER={{ redis_user }}
CONF="/etc/redis/sentinel_${SENTINEL_PORT}.conf"
CLIEXEC="{{ redis_install_dir }}/bin/redis-cli -p ${SENTINEL_PORT}"

if [ -r /etc/default/sentinel_${SENTINEL_PORT} ]; then
    . /etc/default/sentinel_${SENTINEL_PORT}
fi

if [ -n "$REDIS_PASSWORD" ]; then
    CLIEXEC="${CLIEXEC} -a ${REDIS_PASSWORD}"
fi

if [ -n "$BIND_ADDRESS" ]; then
    CLIEXEC="${CLIEXEC} -h ${BIND_ADDRESS}"
fi

case "$1" in
    start)
        if [ -f "$PIDFILE" ]; then
            status_of_proc -p "$PIDFILE" $DAEMON "$NAME process" && return 0
        fi

        if [ -n "$NOFILE_LIMIT" ]; then
            ulimit -n $NOFILE_LIMIT
        fi

        if [ ! -d "$PIDFILE_DIR" ]; then
            mkdir "$PIDFILE_DIR"
            chown ${REDIS_USER}:${REDIS_USER} "$PIDFILE_DIR"
            chmod 0755 "$PIDFILE_DIR"
        fi

        log_daemon_msg "Starting $NAME..."
        if start-stop-daemon --start -q --oknodo -p "$PIDFILE" -c $REDIS_USER --exec $DAEMON -- $CONF --sentinel; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
    stop)
        if [ -f "$PIDFILE" ]; then
            PID=$(cat "$PIDFILE")
            log_daemon_msg "Stopping $NAME..."
            $CLIEXEC shutdown
            while [ -x /proc/${PID} ]; do
                log_daemon_msg "Waiting for Redis Sentinel to shutdown ..."
                sleep 1
            done
            log_end_msg 0
        else
            log_daemon_msg "$NAME is not running"
            log_end_msg 0
        fi
        ;;
    status)
        status_of_proc -p "$PIDFILE" $DAEMON "$NAME" && exit 0 || exit $?
        ;;
    restart|force-reload)
        ${0} stop
        ${0} start
        ;;
    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|status|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
