#!/bin/bash

# Check if all arguments are provided
if [ "$#" -lt 4 ]; then
    echo "Usage: $0 <number_of_iterations> <project_name_prefix> <cluster_name_prefix> <initial_password> [start_index]"
    exit 1
fi

# Assign arguments to variables
iterations=$1
project_name_prefix=$2
cluster_name_prefix=$3
initial_password=$4

if [ -n "$5" ]; then
    start_index=$5
else
    start_index=1
fi
start_index=$5

# Loop and call the python script
for ((i=start_index; i<=iterations; i++))
do
    group_no=$((i / 5 + 1))
    project_name=$(printf "%s%d" "$project_name_prefix" "$group_no")
    cluster_name=$(printf "%s%02d" "$cluster_name_prefix" "$i")
    echo "creating ${project_name}/${cluster_name} cluster ..."
    python src/create_cluster.py $cluster_name $project_name $TIDB_DEFAULT_REGION $initial_password

    # loop to wait for the cluster to be ready
    while true
    do
        python src/get_cluster_info.py $project_name | grep 'AVAILABLE'
        if [ $? -eq 0 ]; then
            echo "cluster ${cluster_name} is ready"
            break
        fi
        echo "waiting for cluster ${cluster_name} to be ready ..."
        sleep 300
    done

    # suspend the cluster
    echo "suspending cluster ${cluster_name} ..."
    python src/suspend_cluster.py $project_name 'suspend'
    sleep 120
done