#!/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}
args=$(jq -r '.' "${json_file_path}")

cluster_autoscaler_role_arn=$(echo ${args} | jq -r '.clusterAutoscalerRoleArn')
image_registry=$(echo ${args} | jq -r '.imageRegistry')

ROOT=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
deploy_dir="${ROOT}/envs/${env}-${vendor}-${logical_region}"
deploy_file="${deploy_dir}/values.yaml"
variant_dir="${ROOT}/variants"

# only deploy for aws
if [ "${vendor}" != "aws" ]; then
  exit 0
fi

if [ -e ${deploy_dir} ]; then
  echo "New region deploy dir already exist, skip it"
else
  mkdir -p ${deploy_dir}
  echo "Prepare manifest files"
  cp "${variant_dir}/${env}/values-${vendor}-tmpl.yaml" "${deploy_file}"
  sed -i "s|TMPL_CLUSTER_NAME|${cluster}|g" ${deploy_file}
  sed -i "s|TMPL_CLUSTER_AUTOSCALER_ROLE_ARN|${cluster_autoscaler_role_arn}|g" ${deploy_file}
  sed -i "s|TMPL_IMAGE_REGISTRY|${image_registry}|g" ${deploy_file}
fi

