#!/usr/bin/env bash

set -x
set -euo pipefail

remote_config=${CONFIG_PATH}
local_config=/tmp/base-config.yaml
bucket_name=${remote_config#s3://}
bucket_name=${bucket_name%%/*}
output_bucket_region=$(aws s3api get-bucket-location --bucket=$bucket_name --query="LocationConstraint" \
  --region=us-west-2 --output=text --no-cli-pager)
aws s3 cp ${remote_config} ${local_config} --region=$output_bucket_region

region=$(yq '.region' "${local_config}")
cluster_name=$(yq '.k8s.name' "${local_config}")

aws cloudformation delete-stack \
  --region $region \
  --stack-name auto-deploy-$cluster_name-ingress
aws cloudformation wait stack-delete-complete \
  --region $region \
  --stack-name auto-deploy-$cluster_name-ingress
