#!/bin/bash
# Function to trigger deployment
trigger_deploy() {
    local url=$1
    local response=$(curl -s -o /dev/null -w "%{http_code}" -X GET "$url")
    if [ "$response" = "200" ]; then
        echo "Deployment triggered successfully for $url"
    else
        echo "Failed to trigger deployment for $url (HTTP status: $response)"
        echo "The deploy URL might be expired. Please refresh your deploy URLs and update the .env file."
    fi
}
# Check if dotenv-cli is installed
if ! command -v dotenv &> /dev/null
then
    echo "Error: dotenv-cli is not installed. Run npm install -g dotenv-cli to install."
    exit 1
fi
# Check if mode parameter is provided
if [ $# -eq 0 ]; then
    echo "Please specify mode = all"
    exit 1
fi
# Parse mode parameter
mode=$1
# Export the function so it's available in the subshell
export -f trigger_deploy
# Use dotenv to run the deployment logic
dotenv -- bash -c '
# Check if required environment variables are set
if [ -z "$WEB_DEPLOY_URL" ]; then
    echo "Error: WEB_DEPLOY_URL must be set in the .env file."
    echo "Please create or update your .env file with the following content:"
    echo "(Replace xxxxx with your actual deployment identifiers)"
    exit 1
fi
case '"$mode"' in
    all)
        trigger_deploy "$WEB_DEPLOY_URL"
        ;;
esac
'

    # all)
    #     trigger_deploy "$WEB_DEPLOY_URL"
    #     trigger_deploy "$GPU_DEPLOY_URL"
    #     trigger_deploy "$QUEUE_DEPLOY_URL"
    #     ;;
    # web)
    #     trigger_deploy "$WEB_DEPLOY_URL"
    #     ;;
    # gpu)
    #     trigger_deploy "$GPU_DEPLOY_URL"
    #     ;;
    # queue)
    #     trigger_deploy "$QUEUE_DEPLOY_URL"
    #     ;;
    # webqueue)
    #     trigger_deploy "$WEB_DEPLOY_URL"
    #     trigger_deploy "$QUEUE_DEPLOY_URL"
    #     ;;
    # sandbox)
    #     trigger_deploy "$SANDBOX_DEPLOY_URL"
    #     ;;
    # *)
    #     echo "Invalid mode. Please specify mode = all, gpu, queue, or web"
    #     exit 1
    #     ;;

    # echo "WEB_DEPLOY_URL=https://envoyer.io/deploy/web-xxxxx"
    # echo "GPU_DEPLOY_URL=https://envoyer.io/deploy/gpu-xxxxx"
    # echo "QUEUE_DEPLOY_URL=https://envoyer.io/deploy/gpu-xxxxx"
    # echo "SANDBOX_DEPLOY_URL=https://envoyer.io/deploy/sandbox-xxxxx"
