# 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
#

DIST_DIR = dist
TERRAFORM_ENVIRONMENT ?= main
AWS_REGION ?= us-east-1
AWS_PROFILE ?= default


# Install dependencies required for further steps or development.
# For example: create virtual environment.
.PHONY: init
init:
	python3 scripts/create-conf-files.py --region ${AWS_REGION} --output-path "${DIST_DIR}/${TERRAFORM_ENVIRONMENT}" --terraform-environment ${TERRAFORM_ENVIRONMENT}
	scripts/generate-tfvars.sh
	python3 scripts/generate-helm-values.py --output-path "${DIST_DIR}/${TERRAFORM_ENVIRONMENT}"
	scripts/set-tfvar-defaults.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=./instance init -upgrade -backend-config=../dist/${TERRAFORM_ENVIRONMENT}/instance.conf
	terraform -chdir=./tenant init -upgrade -backend-config=../dist/${TERRAFORM_ENVIRONMENT}/tenant.conf

.PHONY: deploy-all
deploy-all: tf-init deploy-ddms-tenant deploy-ddms-instance

.PHONY: deploy-ddms-tenant
deploy-ddms-tenant:
	terraform -chdir=./tenant apply -var-file=../dist/${TERRAFORM_ENVIRONMENT}/tenant.tfvars -auto-approve

.PHONY: deploy-ddms-instance
deploy-ddms-instance:
	terraform -chdir=./instance apply -var-file=../dist/${TERRAFORM_ENVIRONMENT}/instance.tfvars -auto-approve


.PHONY: destroy-all
destroy-all: tf-init destroy-ddms-instance destroy-ddms-tenant

.PHONY: destroy-ddms-tenant
destroy-ddms-tenant:
	terraform -chdir=./tenant destroy -var-file=../dist/${TERRAFORM_ENVIRONMENT}/tenant.tfvars -auto-approve

.PHONY: destroy-ddms-instance
destroy-ddms-instance:
	terraform -chdir=./instance destroy -var-file=../dist/${TERRAFORM_ENVIRONMENT}/instance.tfvars -auto-approve


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

