#!/bin/bash

# A script to create a Global Accelerator using AWS CLI.

# --- Configuration ---
REGION="us-east-1" # Global Accelerator is a global service, but endpoint groups are regional.
ACCELERATOR_NAME="MyCLIGlobalAccelerator"
LISTENER_PORT=80
ENDPOINT_GROUP_REGION="us-east-1" # Example region for the endpoint group

# --- 1. Create Accelerator ---
echo "--- Creating Global Accelerator: $ACCELERATOR_NAME ---"
ACCELERATOR_ARN=$(aws globalaccelerator create-accelerator \
  --name $ACCELERATOR_NAME \
  --ip-address-type IPV4 \
  --enabled \
  --region $REGION \
  --query 'Accelerator.AcceleratorArn' --output text)

echo "Global Accelerator created with ARN: $ACCELERATOR_ARN. Waiting for it to be deployed..."
aws globalaccelerator wait accelerator-deployed \
  --accelerator-arns $ACCELERATOR_ARN \
  --region $REGION

echo "Global Accelerator is deployed."

# --- 2. Create Listener ---
echo -e "\n--- Creating Listener for Accelerator ---"
LISTENER_ARN=$(aws globalaccelerator create-listener \
  --accelerator-arn $ACCELERATOR_ARN \
  --port-ranges FromPort=$LISTENER_PORT,ToPort=$LISTENER_PORT \
  --protocol TCP \
  --client-affinity NONE \
  --region $REGION \
  --query 'Listener.ListenerArn' --output text)

echo "Listener created with ARN: $LISTENER_ARN"

# --- 3. Create Endpoint Group ---
echo -e "\n--- Creating Endpoint Group in $ENDPOINT_GROUP_REGION ---"
# For this demo, we'll create an empty endpoint group.
# In a real scenario, you would add actual endpoints like ALBs, EC2 instances, etc.
ENDPOINT_GROUP_ARN=$(aws globalaccelerator create-endpoint-group \
  --listener-arn $LISTENER_ARN \
  --endpoint-group-region $ENDPOINT_GROUP_REGION \
  --traffic-dial-percentage 100 \
  --region $REGION \
  --query 'EndpointGroup.EndpointGroupArn' --output text)

echo "Endpoint Group created with ARN: $ENDPOINT_GROUP_ARN"

# --- 4. Output Accelerator DNS Name ---
ACCELERATOR_DNS_NAME=$(aws globalaccelerator describe-accelerator \
  --accelerator-arn $ACCELERATOR_ARN \
  --region $REGION \
  --query 'Accelerator.DnsName' --output text)

echo -e "\n--- Global Accelerator Setup Complete! ---"
echo "Accelerator DNS Name: $ACCELERATOR_DNS_NAME"
echo "You can now configure your application to use this DNS name."

read -p "Press Enter to delete the Global Accelerator and clean up resources..."

# --- Clean Up ---
echo -e "\n--- Cleaning up resources ---"

# Delete Endpoint Group
echo "Deleting Endpoint Group '$ENDPOINT_GROUP_ARN' ભા"
aws globalaccelerator delete-endpoint-group \
  --endpoint-group-arn $ENDPOINT_GROUP_ARN \
  --region $REGION

echo "Endpoint Group deleted."

# Delete Listener
echo "Deleting Listener '$LISTENER_ARN' ભા"
aws globalaccelerator delete-listener \
  --listener-arn $LISTENER_ARN \
  --region $REGION

echo "Listener deleted."

# Delete Accelerator
echo "Deleting Accelerator '$ACCELERATOR_ARN' ભા"
aws globalaccelerator delete-accelerator \
  --accelerator-arn $ACCELERATOR_ARN \
  --region $REGION

echo "Global Accelerator deleted."

echo -e "\n--- Global Accelerator demonstration and cleanup complete ---"
