#!/bin/bash

#redis
if [ -d redis ];then
    rm -fr redis
fi

mkdir redis
ports=(6379 6479 6579 6679 7379 7479 7579 7679)
for port in ${ports[*]}
do
    #echo $port
    mkdir -p redis/$port/data
    touch redis/$port/redis.conf
    chmod 777 redis/$port/redis.conf

    tee redis/$port/redis.conf << EOF
    loglevel debug
    appendonly no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 1mb
    client-output-buffer-limit normal 0 0 0
    repl-backlog-size 100mb
    rdbcompression no
    # Generated by CONFIG REWRITE
    notify-keyspace-events "$E"
    min-slaves-to-write 0
    protected-mode no
    dir "/data"
EOF
    if [ $port == 7479 ];then
        echo "slaveof 172.19.0.14 6379" >> redis/$port/redis.conf
    fi
    if [ $port == 6479 ];then
        echo "slaveof 172.19.0.10 6379" >> redis/$port/redis.conf
    fi
done

if [ -f docker-compose.yml ];then
    rm -fr docker-compose.yml
fi

curl -sSL https://raw.githubusercontent.com/ctripcorp/x-pipe/master/redis/dockerPackage/docker-compose-from-docker-hub.yml > docker-compose.yml

if [ -n "$1" ];then
  sed -i "s#:1.0#:$1#g" docker-compose.yml
fi

docker-compose up -d --no-recreate
