#!/bin/bash
#----------------------------------------------------------------------------------------------------------------
# BASH SHELL로 실행
if [ -z "$BASH_VERSION" ]; then exec bash "$0" "$@"; exit; fi

#----------------------------------------------------------------------------------------------------------------
# 변수 선언
echo_line="======================================================"
kops_env_file="prod_kops_env.sh"
var_file="99_variables.tf"
search_tag="masters.k8s-prod"

#----------------------------------------------------------------------------------------------------------------
# 파일에서 기존에 선언되어 있는 ID값 가져오는 함수
_get_var_id (){
    tf_variable=$1
    ret_id=`grep ${tf_variable} ${var_file_path} -A 1 | tail -1 | awk -F= '{print $2}' | tr -d ' ' | tr -d "\""`
    if [ -z ${ret_id} ]; then echo "[Error] ${tf_variable} : Not found variable default (${var_file_path})"; exit; fi
    #if [ -z ${ret_id} ]; then echo "[Error] ${tf_variable} : Not found variable default"; fi
}

#----------------------------------------------------------------------------------------------------------------
# 파일의 내용을 변경하는 함수 (before_id -> after_id)

_change_default (){
    before_id=$1
    after_id=$2
    #echo "[${before_id}]"
    #echo "[${after_id}]"
    echo ${echo_line}
    echo "[INFO] ${tf_variable}"
    if [ "${before_id}" != "${after_id}" ]
    then
        if [ "${after_id}" == "" ];
        then
            echo "[Error] # ${tf_variable} value is null"
        else
            echo "[Change] # ${before_id} --> ${after_id}"
            sed -i "s/${before_id}/${after_id}/g" ${var_file_path}
            #echo "sed -i 's/${before_id}/${after_id}/g' ${var_file_path}"
        fi
    else
        echo "[PASS] # ${ret_id}"
    fi
}

#----------------------------------------------------------------------------------------------------------------
# master 별 ID 확인 후 변수에 저장하는 함수
_get_aws_ec2_id (){
    ec2_id=$1
    tag_name=$2

    if [ `echo ${tag_name} | grep 2a | wc -l` == 1 ]
    then
        master_2a=${ec2_id}
    elif [ `echo ${tag_name} | grep 2b | wc -l` == 1 ]
    then
        master_2b=${ec2_id}
    elif [ `echo ${tag_name} | grep 2c | wc -l` == 1 ]
    then
        master_2c=${ec2_id}
    fi
}

#----------------------------------------------------------------------------------------------------------------
# path 확인
current_path=`pwd`
kops_env_path=`find ${current_path} -name ${kops_env_file}`
var_file_path=`find ${current_path} -name ${var_file}`

#----------------------------------------------------------------------------------------------------------------
# kops 환경변수 적용
source ${kops_env_path} > /dev/null 2>/dev/null
if [ -z "${VPC_ID}" ]; then
    source ../${kops_env_file}
fi

#----------------------------------------------------------------------------------------------------------------
# aws에 인스턴스 ID 조회
aws_query="Reservations[].Instances[].[ InstanceId, Tags[?Key=='Name'].Value|[0] ]"
aws_filter="Name=instance-state-name,Values=running"
aws ec2 describe-instances --query "${aws_query}" --filter ${aws_filter} --output text | grep ${search_tag} > tmp_cluster
#echo "aws ec2 describe-instances --query \"${aws_query}\" --filter \"${aws_filter}\" --output text | grep ${search_tag}"

while read line
do
    _get_aws_ec2_id ${line}
done < tmp_cluster

unlink tmp_cluster
#----------------------------------------------------------------------------------------------------------------
# 기존에 파일에 선언되어 있는 ID 값을 조회해서 변경된 ID값이 있으면 변경 진행

_get_var_id master-2a
before_master_2a=${ret_id}
_change_default ${before_master_2a} ${master_2a}

_get_var_id master-2b
before_master_2b=${ret_id}
_change_default ${before_master_2b} ${master_2b}

_get_var_id master-2c
before_master_2c=${ret_id}
_change_default ${before_master_2c} ${master_2c}

_get_var_id VPC_ID
before_VPC_ID=${ret_id}
_change_default ${before_VPC_ID} ${VPC_ID}

_get_var_id Network_CIDR
before_Network_CIDR=${ret_id}
_change_default ${before_Network_CIDR} ${Network_CIDR}

_get_var_id Private_Subnet_ID_1
before_Private_Subnet_ID_1=${ret_id}
_change_default ${before_Private_Subnet_ID_1} ${Private_Subnet_ID_1}

_get_var_id Private_Subnet_ID_2
before_Private_Subnet_ID_2=${ret_id}
_change_default ${before_Private_Subnet_ID_2} ${Private_Subnet_ID_2}

_get_var_id Private_Subnet_ID_3
before_Private_Subnet_ID_3=${ret_id}
_change_default ${before_Private_Subnet_ID_3} ${Private_Subnet_ID_3}

_get_var_id Public_Subnet_ID_1
before_Public_Subnet_ID_1=${ret_id}
_change_default ${before_Public_Subnet_ID_1} ${Public_Subnet_ID_1}

_get_var_id Public_Subnet_ID_2
before_Public_Subnet_ID_2=${ret_id}
_change_default ${before_Public_Subnet_ID_2} ${Public_Subnet_ID_2}

_get_var_id Public_Subnet_ID_3
before_Public_Subnet_ID_3=${ret_id}
_change_default ${before_Public_Subnet_ID_3} ${Public_Subnet_ID_3}

echo ${echo_line}
