#**************************************************
# Master Makefile for Twitch Extensions S.M.A.R.T.
#**************************************************

# Define location of Config Env Vars
# Include vars
ENV_CONFIG = ./global.env
include $(ENV_CONFIG)

# Master CloudFormation Stack Template
MASTER_TEMPLATE := ../master.yaml

# Infrastructure CloudFormation Templates
INFRA_DIR := ../infrastructure
INFRA_TEMPLATES := $(wildcard $(INFRA_DIR)/*.yaml)

# Add git Hash to S3 Path
TIMESTAMP := $(shell /bin/date +'%s')
GIT_HASH := $$(git log -1 --pretty=%H)
ENV_BUCKET_PREFIX := "$(GIT_HASH)"
ENV_BUCKET_PATH := "$(ENV_BUCKET)/$(ENV_BUCKET_PREFIX)"

# Source Code Parent Dir for All Services
SVC_DIR := ../services

# Define for Packaging/Uploading to S3
# Required for CodeBuild
SRC_CODE := $(shell git ls-files ..)

# Local Build Scratch Dir
OUT_DIR := out
VPATH := $(OUT_DIR)

.DELETE_ON_ERROR:

all: build
.PHONY: all

upload: upload-templates upload-src-archive upload-global-env
.PHONY: upload

upload-templates: upload-master upload-infra
.PHONY: upload-templates

$(OUT_DIR):
	mkdir -p $(@)

upload-master: $(MASTER_TEMPLATE) | $(OUT_DIR)
	@echo "[INFO] Uploading Master CFN Template"
	aws s3 cp --acl public-read $(MASTER_TEMPLATE) \
		"s3://$(ENV_BUCKET_PATH)/infrastructure/master.yaml" | \
		tee -a $(OUT_DIR)/$(@)

upload-global-env: $(ENV_CONFIG) | $(OUT_DIR)
	@echo "[INFO] Uploading Global Env File"
	aws s3 cp --acl public-read $(ENV_CONFIG) \
		"s3://$(ENV_BUCKET_PATH)/build/global.env" | \
		tee -a $(OUT_DIR)/$(@)

upload-infra: $(INFRA_TEMPLATES) | $(OUT_DIR)
	@echo "[INFO] Uploading Infrastructure CFN Templates"
	aws s3 sync --acl public-read --exclude "*" --include "*.yaml" \
		$(INFRA_DIR) "s3://$(ENV_BUCKET_PATH)/infrastructure/" | \
		tee -a $(OUT_DIR)/$(@)

SRC_ZIP := $(OUT_DIR)/src.zip
$(SRC_ZIP): $(SRC_CODE) | $(OUT_DIR)
	@echo "[INFO] Archiving git repo"
	cd .. && git archive --format=zip HEAD > build/$(@)

upload-src-archive: $(SRC_ZIP) | $(OUT_DIR)
	@echo "[INFO] Uploading Git Archive"
	aws s3 cp --acl public-read \
		$(OUT_DIR)/src.zip "s3://$(ENV_BUCKET_PATH)/src.zip" | \
		tee -a $(OUT_DIR)/$(@)

# to be used after pushing images to force a new task definition revision
register-task-definition:
	[ ! -z "$$(aws ecs list-task-definitions --family-prefix $(ENV_NAME) --query taskDefinitionArns --output text)" ] && \
		aws ecs register-task-definition --family $(ENV_NAME) \
		--container-definitions \
		"$$(aws ecs describe-task-definition --task-definition \
		$$(aws ecs list-task-definitions --family-prefix $(ENV_NAME) --query taskDefinitionArns[-1] --output text) --query taskDefinition.containerDefinitions)"
.PHONY: register-task-definition

global-env.json: $(GLOBAL_ENV) | $(OUT_DIR)
	@printf '{"PKG_VERSION": "$(PKG_VERSION)"}' > $(OUT_DIR)/$(@)

build:
	@echo "[INFO] Building Services"
	@$(MAKE) -C $(SVC_DIR)/smart build
.PHONY: build

push:
	@echo "[INFO] Pushing Images"
	@$(MAKE) -C $(SVC_DIR)/smart push
.PHONY: push

push-latest:
	@echo "[INFO] Pushing docker images and tagging as latest"
	@$(MAKE) -C $(SVC_DIR)/smart push-latest
.PHONY: push-latest

clean:
	rm -rf $(OUT_DIR)/*
.PHONY: clean

docker-purge:
	docker system prune -af
.PHONY: docker-purge
