#!/bin/bash




deploy() {
# Assign arguments to variables
  local build_no=$1
  local STUGO_TMS_SECRET_DEV=$2


# Echo deployment details
echo "Deploy to Development environment, build: $build_no" 

# Get access token
access_token=$(curl --silent --request POST 'https://kc-internal-aw-us.softwareag-stage.cloud/auth/realms/ctrlplane-dev/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data client_id=utility-jobs \
--data client_secret="$STUGO_TMS_SECRET_DEV" \
--data grant_type=client_credentials | jq -r '.access_token')

# Check if the access token was fetched successfully
if [ -z "$access_token" ]; then
    echo "Error: Failed to fetch access token."
    exit 1
fi

# Make the PATCH request and capture both response body and HTTP status code
response=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" --request PATCH \
--url https://tms.apicp-aw-us.webmethods-dev.io/tms/v3/tenants/devrealm6/chartvalues \
--header "Authorization: Bearer $access_token" \
--header 'Content-Type: application/json' \
--data '[
  {
    "name": "apiStudioStatus",
    "value": "enabled"
  },
  {
    "name": "applications.studio.imageTag",
    "value": "'"$build_no"'"
  }
]')

# Extract the HTTP status code
http_status=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
echo "Response status: $http_status"
# Extract the response body by removing the HTTP status part
response_body=$(echo "$response" | sed -e 's/HTTPSTATUS\:.*//g')

# Check for success or failure
if [ "$http_status" -eq 200 ] || [ "$http_status" -eq 204 ]; then
    echo "Success: Deployment updated successfully."
    echo "Response Body: $response_body"
else
    echo "Failure: Deployment update failed with status code $http_status."
    echo "Response Body: $response_body"
    exit 1
fi

}
# End of deploy function

# Dummy refresh function
refresh() {
  local build_no=$1
  local STUGO_TMS_SECRET_DEV=$2
  

  # Get access token
    access_token=$(curl --silent --request POST 'https://kc-internal-aw-us.softwareag-stage.cloud/auth/realms/ctrlplane-dev/protocol/openid-connect/token' \
    --header 'Content-Type: application/x-www-form-urlencoded' \
    --data client_id=utility-jobs \
    --data client_secret="$STUGO_TMS_SECRET_DEV" \
    --data grant_type=client_credentials | jq -r '.access_token')

  # Echo the build information
  echo "Deploy build: $build_no"

  # Send the PUT request to update the deployment
  response=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" --request PUT \
    --url https://tms.apicp-aw-us.webmethods-dev.io/tms/v3/tenants/devrealm6 \
    --header "Authorization: Bearer $access_token" \
    --header 'Content-Type: application/json')

    # Extract the HTTP status code
    http_status=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
    echo "Response status: $http_status"

  # Check the response status and log it
  if [ "$http_status" -eq 200 ] || [ "$http_status" -eq 204 ]; then
    echo "Refresh action completed successfully"
  else
    echo "Failed to complete refresh action"
  fi
}

# end of refresh funciton

# Ensure both build number and secret are provided as arguments
# Ensure all arguments are provided
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
  echo "Error: Missing arguments."
  echo "Usage: $0 <action> <build_no> <STUGO_TMS_SECRET_DEV>"
  exit 1
fi

# Assign input arguments to variables
action=$1
build_no=$2
STUGO_TMS_SECRET_DEV=$3

# Check the action input and call the appropriate function
if [[ "$action" == "update_Charts" ]]; then
  deploy "$build_no" "$STUGO_TMS_SECRET_DEV"
elif [[ "$action" == "refresh" ]]; then
  refresh "$build_no" "$STUGO_TMS_SECRET_DEV"
else
  echo "No action taken: Action '$action' is not recognized."
fi
