#!/usr/bin/env bash


num_of_nodes=3
docker_image_version=latest

### first we check if docker and docker-compose are installed

has_docker=$(docker version | echo "$?")

if [ ! has_docker ]; then
  echo [-] You must have docker installed to run the network
  exit 1
fi

has_docker_compose=$(docker compose --version | echo "$?")

if [ ! has_docker_compose ]; then
  echo [-] You must have docker compose installed to run the network
  exit 1
fi

echo [+] Cleaning up old run
docker-compose -f docker-compose-network.yml down -v

echo [+] Pulling the official drand docker image $docker_image_version
docker pull ghcr.io/drand/go-drand:$docker_image_version 1>/dev/null


### then we create a volume for each of those nodes
echo [+] Creating docker volumes for $num_of_nodes nodes

for i in $(seq 1 $num_of_nodes);
do
  docker volume create drand_docker_demo$i
done

### next we're going to generate a keypair on each of those volumes
echo [+] Generating a default network keypair for each node

for i in $(seq 1 $num_of_nodes);
do
  # these will end up on drand1:8010, drand2:8020, drand3:8030, etc
  # note they map to the container's mapped ports, but the internal ports; internally the services still listen on 8080
  path=drand_docker_demo$i:80${i}0
  docker run --rm --volume drand_docker_demo$i:/data/drand ghcr.io/drand/go-drand:$docker_image_version generate-keypair --folder /data/drand/.drand --id default $path 1>/dev/null
done

### now we start them all using docker-compose as it'll be easy to spin up and down
echo [+] Starting all the nodes using docker-compose

docker compose -f docker-compose-network.yml up -d


### sleep to let the nodes start up
sleep 5

### now we run the initial distributed key generation
### we're going to use the first node as the leader node

echo [+] Initialising distributed key generation for the leader

docker exec drand_docker_demo1 drand dkg generate-proposal  --joiner drand_docker_demo1:8010 --joiner drand_docker_demo2:8020 --joiner drand_docker_demo3:8030 --out proposal.toml
# we start the DKG and send it to the background;
docker exec drand_docker_demo1 drand dkg init --proposal ./proposal.toml --threshold 2 --period 3s --control 8818


echo [+] Joining distributed key generation for the followers
for i in $(seq 2 $num_of_nodes);
do
  # we start the DKG and send it to the background
  docker exec --detach drand_docker_demo$i drand dkg join --control 88$i8
done


echo [+] Started distributed key generation execution on the leader
docker exec --detach drand_docker_demo1 drand dkg execute --control 8818
### now we wait for the distributed key generation to be completed and the first round to be created

echo [+] Waiting for the DKG to finish - could take up to a minute!
attempts=60

while :
do
  ### if it isn't working after a bunch of attempts, it probably failed
  if [ "$attempts" -eq 0 ]; then
    echo [-] the DKG didn\'t finish successfully - check the container logs with '`docker logs -f drand_docker_demo1`'
    exit 1
  fi

  ### once the first round has been created, we know that the DKG happened successfully
  response=$(curl --silent 127.0.0.1:9010/public/1)
  code=$?
  if [ $code -eq 0 ]; then
    if [[ $response =~ "round" ]]; then
      break
    fi
  fi

  attempts=$(($attempts - 1))
  sleep 1
done

echo [+] Network running successfully!
