#!/bin/bash

# A script to deploy a simple Python web application to AWS Elastic Beanstalk using AWS CLI.

# --- Configuration ---
REGION="us-east-1"
APPLICATION_NAME="MyCLIWebApp"
ENVIRONMENT_NAME="MyCLIWebApp-env"
VERSION_LABEL="v1"
PLATFORM_SOLUTION_STACK="64bit Amazon Linux 2 v3.4.1 running Python 3.9" # Check AWS docs for latest
SOURCE_BUNDLE_ZIP="webapp.zip"
APP_DIR="webapp"

# --- 1. Create Elastic Beanstalk Application ---
echo "--- Creating Elastic Beanstalk Application: $APPLICATION_NAME ---"
aws elasticbeanstalk create-application \
  --application-name $APPLICATION_NAME \
  --description "My CLI Python Web App" \
  --region $REGION

echo "Application created."

# --- 2. Create Source Bundle ---
echo -e "\n--- Creating source bundle for the web application ---"
mkdir $APP_DIR
cat > $APP_DIR/application.py <<EOF
from flask import Flask
application = Flask(__name__)

@application.route('/')
def hello_world():
    return 'Hello, World from Elastic Beanstalk CLI!'

if __name__ == '__main__':
    application.run(debug=True)
EOF

cat > $APP_DIR/requirements.txt <<EOF
Flask==2.0.2
EOF

cat > $APP_DIR/.ebextensions/python.config <<EOF
option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: application.py
EOF

zip -r $SOURCE_BUNDLE_ZIP $APP_DIR

echo "Source bundle '$SOURCE_BUNDLE_ZIP' created."

# --- 3. Create Application Version ---
echo -e "\n--- Creating Application Version: $VERSION_LABEL ---"
aws elasticbeanstalk create-application-version \
  --application-name $APPLICATION_NAME \
  --version-label $VERSION_LABEL \
  --source-bundle S3Bucket="elasticbeanstalk-$REGION-$(aws sts get-caller-identity --query Account --output text)",S3Key=$SOURCE_BUNDLE_ZIP \
  --region $REGION

echo "Application version created."

# --- 4. Create Environment ---
echo -e "\n--- Creating Elastic Beanstalk Environment: $ENVIRONMENT_NAME ---"
aws elasticbeanstalk create-environment \
  --application-name $APPLICATION_NAME \
  --environment-name $ENVIRONMENT_NAME \
  --version-label $VERSION_LABEL \
  --solution-stack-name "$PLATFORM_SOLUTION_STACK" \
  --option-settings \
    Namespace=aws:autoscaling:launchconfiguration,OptionName=InstanceType,Value=t2.micro \
    Namespace=aws:autoscaling:asg,OptionName=MinSize,Value=1 \
    Namespace=aws:autoscaling:asg,OptionName=MaxSize,Value=1 \
  --region $REGION

echo "Environment creation initiated. Waiting for environment to be ready (this can take several minutes)..."
aws elasticbeanstalk wait environment-ready \
  --environment-name $ENVIRONMENT_NAME \
  --region $REGION

echo "Environment is ready."

# --- 5. Output Environment URL ---
ENV_URL=$(aws elasticbeanstalk describe-environments \
  --environment-names $ENVIRONMENT_NAME \
  --query 'Environments[0].CNAME' \
  --region $REGION \
  --output text)

echo -e "\n--- Web Application Deployed Successfully! ---"
echo "Environment URL: http://$ENV_URL"

read -p "Press Enter to terminate the environment and clean up resources..."

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

# Terminate Environment
echo "Terminating environment '$ENVIRONMENT_NAME'வுகளை..."
aws elasticbeanstalk terminate-environment \
  --environment-name $ENVIRONMENT_NAME \
  --region $REGION

echo "Waiting for environment to be terminated..."
aws elasticbeanstalk wait environment-terminated \
  --environment-name $ENVIRONMENT_NAME \
  --region $REGION

echo "Environment terminated."

# Delete Application Version
echo "Deleting application version '$VERSION_LABEL'வுகளை..."
aws elasticbeanstalk delete-application-version \
  --application-name $APPLICATION_NAME \
  --version-label $VERSION_LABEL \
  --region $REGION

echo "Application version deleted."

# Delete Application
echo "Deleting application '$APPLICATION_NAME'வுகளை..."
aws elasticbeanstalk delete-application \
  --application-name $APPLICATION_NAME \
  --region $REGION

echo "Application deleted."

# Clean up local files
rm $SOURCE_BUNDLE_ZIP
rm -rf $APP_DIR

echo -e "\n--- Elastic Beanstalk demonstration and cleanup complete ---"
