#!/bin/bash

## EXPERIMENT RUNNER
# ex:  sudo ./scripts/run-3 2 . orig-client 100 100 100, orig-client 100 100 100 . orig-server, orig-server

die() { echo "$*" 1>&2 ; exit 1; }

WAITFOR=2280 # 38min

# SPLIT CLIENT AND SERVER TARGETS
OLD_IFS=$IFS; IFS="."
read -a PARTS <<< "$@"
IFS=$OLD_IFS
[[ ${#PARTS[@]} == 3 ]] || die "Separate count, clients and servers experiments by starts. I expect 3 parts but got ${#PARTS[@]}"

# EXTRACT DATA
OLD_IFS=$IFS; IFS=","
read -a CONFIG <<< "${PARTS[0]}"
read -a CLIENT_TARGETS <<< "${PARTS[1]}"
read -a SERVER_TARGETS <<< "${PARTS[2]}"
IFS=$OLD_IFS

[[ ${#CLIENT_TARGETS[@]} == ${#SERVER_TARGETS[@]} ]] \
  || die "Same number of experiments is required. Got ${#CLIENT_TARGETS[@]} clients and ${#SERVER_TARGETS[@]} servers"
instances=${#CLIENT_TARGETS[@]}
REPEAT=${CONFIG[0]}

echo ":: Configuration Summary"
echo "repeat=$REPEAT"
for i in $(seq 0 $(expr $instances - 1)); do
  echo "  instance=$i - server=${SERVER_TARGETS[$i]} - client=${CLIENT_TARGETS[$i]}"
done

echo ":: Clear Resources"
for i in $(seq 0 $(expr $instances - 1)); do
  echo "clear $i"
  docker container inspect donarxp_server_${i} > /dev/null 2>&1 && docker kill donarxp_server_${i}
  docker container inspect donarxp_client_${i} > /dev/null 2>&1 && docker kill donarxp_client_${i}
done

echo ":: Bootstrap"
echo "Create output folder..."
docker run \
  --rm \
  --user root \
  -v `pwd`/out:/home/donar \
  registry.gitlab.inria.fr/qdufour/donar \
  chown -R 1000:1000 /home/donar

for i in $(seq 0 $(expr $instances - 1)); do
echo "Spawning container for instance=$i..."
docker run \
  --rm \
  -d \
  --name "donarxp_server_${i}" \
  -e HOME='/tmp' \
  -v `pwd`/out:/home/donar \
  registry.gitlab.inria.fr/qdufour/donar \
  tor -f /etc/torrc

docker run \
  --rm \
  -d \
  --name "donarxp_client_${i}" \
  -e HOME='/tmp' \
  -v `pwd`/out:/home/donar \
  registry.gitlab.inria.fr/qdufour/donar \
  tor -f /etc/torrc

done

sleep 10

echo ":: Start experiment..."
for j in $(seq 1 $REPEAT); do
run_fold=`mktemp -up . XXXXXXXXXXXXXXXX`
echo "start - batch_count=$j - id=$run_fold"

echo "  reset containers"
for i in $(seq 0 $(expr $instances - 1)); do
  docker exec donarxp_client_${i} sh -c 'killall --quiet -9 bash; killall --quiet -9 donar; killall --quiet -9 measlat; killall --quiet -9 udpecho; killall --quiet -9 torecho'
  docker exec donarxp_server_${i} sh -c 'killall --quiet -9 bash; killall --quiet -9 donar; killall --quiet -9 measlat; killall --quiet -9 udpecho; killall --quiet -9 torecho'
done

echo "  launch servers"
for i in $(seq 0 $(expr $instances - 1)); do
  echo "    instance=$i - exec=${SERVER_TARGETS[$i]}"
  docker exec donarxp_server_$i ${SERVER_TARGETS[$i]} ${run_fold}-$i &
done

sleep 10
echo "  launch clients"

(subshellpid=$BASHPID 
  (sleep $WAITFOR; echo "  timeout"; kill -9 $subshellpid) & 
  (for i in $(seq 0 $(expr $instances - 1)); do
    echo "    instance=$i - exec=${CLIENT_TARGETS[$i]}"
    docker exec donarxp_client_$i ${CLIENT_TARGETS[$i]} ${run_fold}-$i &
    sleep 10
  done; wait; echo "  ok"; kill -9 $subshellpid)) 2>/dev/null 

done

echo ":: Clear Resources"
for i in $(seq 0 $(expr $instances - 1)); do
  echo "clear $i"
  docker kill "donarxp_server_${i}"
  docker kill "donarxp_client_${i}"
done