#!/bin/bash
set -euo pipefail
# When changing this script, remember to modify the corresponding github action
vendor=${1}
env=${2}
region=${3}
logical_region=${4}
cluster=${5}
json_file_path=${6}

ROOT=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
source "${ROOT}/../../lib/lib.sh"

args=$(jq -c 'if type == "string" then fromjson else . end' "${json_file_path}")

is_byoc=$(echo "${args}" | jq -r '.byoc // false')

if [[ "$vendor" != "aws" ]]; then
  if [[ "$vendor" != "azure" || "${is_byoc}" != "true" ]]; then
    echo "Ops server region values are only configured for aws and azure byoc, skip ${vendor}"
    exit 0
  fi
fi

deploy_dir="${ROOT}/envs/${env}"
deploy_file="${deploy_dir}/regional-${vendor}-${logical_region}.yaml"
variant_dir="${ROOT}/variants"
mkdir -p ${deploy_dir}
tmpl_values_file="${variant_dir}/aws-values-tmpl.yaml"
if [[ "${is_byoc}" == "true" ]]; then
  tmpl_values_file="${variant_dir}/byoc-aws-values-tmpl.yaml"
fi
if [[ "$vendor" == "azure" && "${is_byoc}" == "true" ]]; then
  tmpl_values_file="${variant_dir}/azure-byoc-values-tmpl.yaml"
fi

image_registry=$(echo "${args}" | jq -r '.imageRegistry // ""')
ops_server_role_arn=$(echo "${args}" | jq -r '.regionalServerRoleARN // ""')
hosted_zone_name=$(echo "${args}" | jq -r '.hostedZoneName // ""')
flux_registry=$(echo "${args}" | jq -r '.fluxRegistry // .imageRegistry // ""')
certificate_arn=$(echo "${args}" | jq -r '.certificateARN // ""')
waf_acl_arn=$(echo "${args}" | jq -r '.wafACLARN // ""')
subscription_id=$(echo "${args}" | jq -r '.subscriptionID // ""')
resource_group=$(echo "${args}" | jq -r '.resourceGroup // ""')

if [[ -z "$resource_group" || "$resource_group" == "null" ]]; then
  resource_group="$cluster"
fi

if [[ -z "$flux_registry" || "$flux_registry" == "null" ]]; then
  flux_registry="$image_registry"
fi


if [[ -z "$image_registry" || "$image_registry" == "null" ]]; then
  echo "Error: imageRegistry must be provided in the JSON input." >&2
  exit 1
fi

if [[ -z "$ops_server_role_arn" || "$ops_server_role_arn" == "null" ]]; then
  echo "Error: regionalServerRoleARN must be provided in the JSON input." >&2
  exit 1
fi

if [[ "$vendor" == "azure" && "${is_byoc}" == "true" ]]; then
  if [[ -z "$subscription_id" || "$subscription_id" == "null" ]]; then
    echo "Error: subscriptionID must be provided in the JSON input for azure byoc." >&2
    exit 1
  fi
fi

if [[ -e ${deploy_file} ]]; then
  echo "New region deploy file already exist, skip it"
else
  echo "Prepare manifest files"
  cp "${tmpl_values_file}" "${deploy_file}"
  sed_inplace "TMPL_ENV" "${env}" "${deploy_file}"
  sed_inplace "TMPL_REGION" "${region}" "${deploy_file}"
  sed_inplace "TMPL_LOGICAL_REGION" "${logical_region}" "${deploy_file}"
  sed_inplace "TMPL_IMAGE_REGISTRY" "${image_registry}" "${deploy_file}"
  sed_inplace "TMPL_FLUX_REGISTRY" "${flux_registry}" "${deploy_file}"
  sed_inplace "TMPL_OPS_SERVER_ROLE_ARN" "${ops_server_role_arn}" "${deploy_file}"
  sed_inplace "TMPL_HOSTED_ZONE_NAME" "${hosted_zone_name}" "${deploy_file}"
  sed_inplace "TMPL_CERTIFICATE_ARN" "${certificate_arn}" "${deploy_file}"
  sed_inplace "TMPL_WAFV2_ACL_ARN" "${waf_acl_arn}" "${deploy_file}"
  sed_inplace "TMPL_SUBSCRIPTION_ID" "${subscription_id}" "${deploy_file}"
  sed_inplace "TMPL_RESOURCE_GROUP" "${resource_group}" "${deploy_file}"
  echo "Manifest file generated at ${deploy_file}"
fi
