# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the “License”).
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Configuration
#

DEPLOYMENT_BASE_DIR = deployment
DIST_DIR = dist
TERRAFORM_ENVIRONMENT ?= migration
AWS_REGION ?= us-east-1
AWS_PROFILE ?= default

.PHONY: init
init:
	python3 terraform-scripts/create-conf-files.py --region ${AWS_REGION} --output-path "${DIST_DIR}/${TERRAFORM_ENVIRONMENT}" --terraform-environment ${TERRAFORM_ENVIRONMENT}
	terraform-scripts/generate-tfvars.sh
	
	@make OUTPUT="Generated files to the dist folder \($(shell pwd)/${DIST_DIR}\), validate files before continuing" output;

.PHONY: tf-init
tf-init:
	terraform -chdir=${DEPLOYMENT_BASE_DIR}/ init -upgrade -backend-config=../${DIST_DIR}/${TERRAFORM_ENVIRONMENT}/migration.conf

.PHONY: deploy-all
deploy-all: tf-init deploy-migration

.PHONY: deploy-migration
deploy-migration:
	terraform -chdir=${DEPLOYMENT_BASE_DIR}/ apply -var-file=../${DIST_DIR}/${TERRAFORM_ENVIRONMENT}/migration.tfvars -auto-approve

.PHONY: destroy-all
destroy-all: tf-init destroy-migration 

.PHONY: destroy-migration
destroy-migration:
	terraform -chdir=${DEPLOYMENT_BASE_DIR}/ destroy -var-file=../${DIST_DIR}/${TERRAFORM_ENVIRONMENT}/migration.tfvars -auto-approve

.PHONY: output
output: 
	@echo ""
	@printf -- '=%.0s' {1..$(shell tput cols)}; echo ""
	@echo ${OUTPUT}
	@printf -- '=%.0s' {1..$(shell tput cols)}; echo ""


