SHELL=/bin/bash -o pipefail
PROHOME := $(realpath ../../..)

REGION := $(notdir $(CURDIR))
ACCOUNT := $(notdir $(patsubst %/,%,$(dir $(CURDIR))))

ifneq ($(GITHUB_ACTIONS),true)
export AWS_PROFILE := $(ACCOUNT)
endif

.PHONY: all
all:
	echo "Please specify a target"
	exit 1

.PHONY: venv
venv: $(PROHOME)/venv/bin/pip

$(PROHOME)/venv/bin/pip:
	cd $(PROHOME)/ && make venv/bin/pip

$(PROHOME)/tf-modules/VERSIONS: $(PROHOME)/venv/bin/pip $(PROHOME)/Terrafile
	cd $(PROHOME)/ && make tf-modules/VERSIONS

.PHONY: terrafile
terrafile: $(PROHOME)/tf-modules/VERSIONS

.terraform/modules/modules.json: $(PROHOME)/tf-modules/VERSIONS backend.tf
	tofu init

.PHONY: init
init: .terraform/modules/modules.json

.PHONY: clean
clean:
	$(RM) -rf .terraform
	$(RM) -rf inventory
	$(RM) .terraform.lock.hcl
	$(RM) backend-state.tf
	$(RM) backend.plan
	$(RM) backend.tf
	$(RM) dyn_locals.tf
	$(RM) external_k8s_cidr_ipv4.tf
	$(RM) terraform.tfstate
	cd $(PROHOME)/ && make clean

.PHONY: backend-state
backend-state: backend.tf

external_k8s_cidr_ipv4.tf: $(PROHOME)/venv/bin/pip $(PROHOME)/scripts/simplify_cidr_blocks.py
	$(PROHOME)/venv/bin/python $(PROHOME)/scripts/simplify_cidr_blocks.py --rules-per-sg 50 --output-file external_k8s_cidr_ipv4.tf

dyn_locals.tf:
	echo -e "locals {\n  # tflint-ignore: terraform_unused_declarations\n  aws_region = \"$(REGION)\"\n  # tflint-ignore: terraform_unused_declarations\n  aws_account_id = \"$(ACCOUNT)\"\n}\n" >dyn_locals.tf

backend.tf: backend-state.tf
	sed "s/#AWS_REGION/$(REGION)/g" <$(PROHOME)/modules/backend-file/backend.tf >backend.tf
	$(RM) terraform.tfstate

backend-state.tf: dyn_locals.tf external_k8s_cidr_ipv4.tf
	sed "s/#AWS_REGION/$(REGION)/g" <$(PROHOME)/modules/backend-file/backend-state.tf >backend-state.tf
	tofu get -update
	tofu init -backend=false
	tofu plan -input=false -out=backend.plan -detailed-exitcode -target=module.backend-state -lock-timeout=15m ; \
		ext_code=$$? ; \
		if [ $$ext_code -eq 2 ] ; then \
			if [ "$$GITHUB_ACTIONS" != "true" ] ; then \
				echo "Backend state does not exist, creating" ; \
				terraform apply ${TERRAFORM_EXTRAS} backend.plan ; \
			else \
				echo "Backend state does not exist, should not do it on a github action!" ; \
				exit 1 ; \
			fi ; \
		elif [ $$ext_code -eq 0 ] ; then \
			echo "Backend state already exists" ; \
		else \
			echo "Unexpected exit code $$ext_code" ; \
			exit 1 ; \
		fi

.PHONY: plan
plan: .terraform/modules/modules.json
	tofu plan $(TERRAFORM_EXTRAS)

.PHONY: plan-canary
plan-canary: .terraform/modules/modules.json
	tofu plan -target module.autoscaler-lambda-canary $(TERRAFORM_EXTRAS)

.PHONY: apply
apply: .terraform/modules/modules.json
	tofu apply ${TERRAFORM_EXTRAS}

.PHONY: apply-canary
apply-canary: .terraform/modules/modules.json
	tofu apply -target module.autoscaler-lambda-canary ${TERRAFORM_EXTRAS}

.PHONY: destroy
destroy: .terraform/modules/modules.json
	echo "To make sure you want to run this, go to the Makefile and comment out the destroy target" ; exit 1
	# terraform destroy ${TERRAFORM_EXTRAS}

.PHONY: tflint
tflint: .terraform/modules/modules.json
	tflint --init
	tflint --call-module-type=all --color --minimum-failure-severity=warning --recursive

.PHONY: link-test-infra-canary
link-test-infra-canary: .terraform/modules/modules.json $(ALL_ZIPS)
	@if [ -z "$${TEST_INFRA_DIR}" ]; then echo -e "\033[31mError: TEST_INFRA_DIR must be set to the path to your local pytorch/test-infra repo\033[0m" && exit 1; fi
	rm -rf ../../../tf-modules/terraform-aws-github-runner
	rm -rf ../../../lambdas-download-canary/*
	rm -rf ../../../lambdas-download/*
	cd ../../../tf-modules && ln -s $(TEST_INFRA_DIR)/terraform-aws-github-runner/
	cd ../../../tf-modules/terraform-aws-github-runner/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer && make clean && make dist
	cd ../../../tf-modules/terraform-aws-github-runner/modules/runners/lambdas/runners && make clean && make dist
	cd ../../../tf-modules/terraform-aws-github-runner/modules/webhook/lambdas/webhook && make clean && make dist
	cp ../../../tf-modules/terraform-aws-github-runner/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/runner-binaries-syncer.zip  ../../../assets/lambdas-download-canary/runner-binaries-syncer.zip
	cp ../../../tf-modules/terraform-aws-github-runner/modules/runners/lambdas/runners/runners.zip  ../../../assets/lambdas-download-canary/runners.zip
	cp ../../../tf-modules/terraform-aws-github-runner/modules/webhook/lambdas/webhook/webhook.zip  ../../../assets/lambdas-download-canary/webhook.zip
