#!/bin/bash
SESSION=etcd-cluster
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

ulimit -n unlimited

tmux new-session -d -s $SESSION

peer_args=
if [ -n "${DISCOVERY_URL}" ]; then
	peer_args="-discovery ${DISCOVERY_URL}"
fi

# Setup a window for tailing log files
tmux new-window -t $SESSION:1 -n 'peers'
tmux split-window -h
tmux select-pane -t 0
tmux send-keys "${DIR}/../bin/etcd -peer-addr 127.0.0.1:7001 -addr 127.0.0.1:4001 -data-dir peer1 -name peer1 ${peer_args}" C-m

if [ -z "${peer_args}" ]; then
	peer_args="-peers 127.0.0.1:7001"
fi

for i in 2 3; do
	tmux select-pane -t 0
	tmux split-window -v
	tmux send-keys "${DIR}/../bin/etcd -cors='*' -peer-addr 127.0.0.1:700${i} -addr 127.0.0.1:400${i} -data-dir peer${i} -name peer${i} ${peer_args}" C-m
done

# Attach to session
tmux attach-session -t $SESSION
