#!/usr/bin/env bash

# Exits as soon as any line fails.
set -euo pipefail

source ci/scripts/common.sh

while getopts 'p:' opt; do
    case ${opt} in
        p )
            profile=$OPTARG
            ;;
        \? )
            echo "Invalid Option: -$OPTARG" 1>&2
            exit 1
            ;;
        : )
            echo "Invalid option: $OPTARG requires an argument" 1>&2
            ;;
    esac
done
shift $((OPTIND -1))

sink_test_env_setup "$profile"
apt-get update -y && apt-get install -y redis-server

echo "--- testing sinks"
sqllogictest -p 4566 -d dev './e2e_test/sink/redis_sink.slt'
sleep 1

redis-cli -h redis-server -p 6379 get {\"v1\":1} >> ./query_result.txt
redis-cli -h redis-server -p 6379 get V1:1 >> ./query_result.txt
redis-cli -h redis-server -p 6379 GEOPOS key_is:1_1 test >> ./query_result2.txt
redis-cli -h redis-server -p 6379 xlen test_stream >> ./query_result3.txt

# check sink destination using shell
if cat ./query_result.txt | tr '\n' '\0' | xargs -0 -n1 bash -c '[[ "$0" == "{\"v1\":1,\"v2\":1,\"v3\":1,\"v4\":1.100000023841858,\"v5\":1.2,\"v6\":\"test\",\"v7\":734869,\"v8\":\"2013-01-01T01:01:01.000000Z\",\"v9\":false}" || "$0" == "V2:1,V3:1" ]]'; then
    echo "Redis sink1 check passed"
else
    cat ./query_result.txt
  echo "The output1 is not as expected."
  exit 1
fi

LINE_COUNT=$(cat ./query_result2.txt | wc -l);
if [ "$LINE_COUNT" -eq 2 ]; then
    echo "Redis sink2 check passed"
else
    cat ./query_result2.txt
    echo "The output2 is not as expected."
    exit 1
fi

if cat ./query_result3.txt | tr '\n' '\0' | xargs -0 -n1 bash -c '[[ "$0" == 1 ]]'; then
    echo "Redis sink3 check passed"
else
    cat ./query_result3.txt
  echo "The output3 is not as expected."
fi

echo "--- testing cluster sinks"
REDIS_CLUSTER_LOG_DIR=".risingwave/log"
mkdir -p "$REDIS_CLUSTER_LOG_DIR"
redis-server ./ci/redis-conf/redis-7000.conf --daemonize yes --logfile "$REDIS_CLUSTER_LOG_DIR/redis-7000.log"
redis-server ./ci/redis-conf/redis-7001.conf --daemonize yes --logfile "$REDIS_CLUSTER_LOG_DIR/redis-7001.log"
redis-server ./ci/redis-conf/redis-7002.conf --daemonize yes --logfile "$REDIS_CLUSTER_LOG_DIR/redis-7002.log"

echo "yes" | redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
# Wait a bit for cluster_state to become ok before running SLT.
sleep 2

sqllogictest -p 4566 -d dev './e2e_test/sink/redis_cluster_sink.slt'

redis-cli -c --cluster call 127.0.0.1:7000 keys \* >> ./query_result_1.txt

line_count=$(wc -l < query_result_1.txt)
if [ "$line_count" -eq 16 ]; then
    echo "Redis sink check passed"
else
    cat ./query_result_1.txt
    echo "The output is not as expected."
    exit 1
fi

echo "--- Kill cluster"
risedev ci-kill
