#!/bin/bash

if parse_out=$(cat "/etc/redis.conf" | egrep "^bind +.*$"); then
  redis_host=$(echo -n $parse_out | awk '{print $2}')
else
  redis_host=127.0.0.1
fi

if parse_out=$(cat "/etc/redis.conf" | egrep "^port +.*$"); then
  redis_port=$(echo -n $parse_out | awk '{print $2}')
else
  redis_port=6379
fi

if parse_out=$(cat "/etc/redis.conf" | egrep "^requirepass +.*$"); then
  redis_pass=$(echo -n $parse_out | awk '{print $2}')
  result=$(printf "*2\r\n\$4\r\nAUTH\r\n\$${#redis_pass}\r\n${redis_pass}\r\n*1\r\n\$4\r\nPING\r\n" | socat - TCP:$redis_host:$redis_port)
else
  result=$(printf "*1\r\n\$4\r\nPING\r\n" | socat - TCP:$redis_host:$redis_port)
fi

if echo $result | grep -q '+PONG'; then
  echo "Redis server responded correctly on ${redis_host}:${redis_port}."
else
  echo "Redis server does not respond correctly: ${result}"
  exit 1
fi
