#!/usr/bin/env bash

set -x
set -euo pipefail

ROOT=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)

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

account_id=$(yq -r -ojson '.aws.dataplaneAccounts.0' "${local_config}")

aws --profile $account_id cloudformation validate-template --template-body file://$ROOT/aws/cloudformation/iam.yaml
aws --profile $account_id cloudformation validate-template --template-body file://$ROOT/aws/cloudformation/iam-dataplane.yaml
aws --profile $account_id cloudformation validate-template --template-body file://$ROOT/aws/cloudformation/nodegroup-control.yaml
aws --profile $account_id cloudformation validate-template --template-body file://$ROOT/aws/cloudformation/nodegroup-data.yaml
aws --profile $account_id cloudformation validate-template --template-body file://$ROOT/aws/cloudformation/ingress.yaml
aws --profile $account_id cloudformation validate-template --template-body file://$ROOT/aws/cloudformation/vpc-endpoint-service.yaml
