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

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

# Docker Naming Definitions **THIS MUST ALIGN WITH EVERYTHING ELSE**
IMAGE_NAME := $(ENV_NAME)/service
SUB_HASH := $$(git log -1 --pretty=%H)

# Define location and names of env config files
SERVICE_CONFIG_DIR := ./config
SERVICE_CONFIG_FILES := $(wildcard $(SERVICE_CONFIG_DIR)/*.env)

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

.DELETE_ON_ERROR:

all: build
.PHONY: all

# Create out dir
$(OUT_DIR):
	mkdir -p $(@)

build: Dockerfile $(ENV_CONFIG) $(SERVICE_CONFIG_FILES) | $(OUT_DIR)
	@echo "[INFO] Building Docker Image: [$(IMAGE_NAME)]"
	docker build --pull --rm -t $(IMAGE_NAME):$(SUB_HASH) . | tee $(OUT_DIR)/$(@)

tag-latest: build
	@echo "[INFO] Tagging Docker Image as latest: [$(IMAGE_NAME):$(SUB_HASH)]"
	docker tag $(IMAGE_NAME):$(SUB_HASH) $(IMAGE_NAME):latest | tee $(OUT_DIR)/$(@)

tag-repo: build ecr-repo-uri
	@echo "[INFO] Tagging Docker Images Into Registry Repo:" \
		"[$(IMAGE_NAME):$(SUB_HASH)]"
	docker tag $(IMAGE_NAME):$(SUB_HASH) $(REPOSITORY_URI):$(SUB_HASH) | \
		tee $(OUT_DIR)/$(@)

tag-repo-latest: tag-repo
	@echo "[INFO] Tagging Docker Image Into Registry Repo as latest:" \
		"[$(IMAGE_NAME):$(SUB_HASH)]"
	docker tag $(IMAGE_NAME):latest $(REPOSITORY_URI):latest | \
		tee $(OUT_DIR)/$(@)

push: tag-repo
	@echo "[INFO] Pushing Docker Image to Repo: [$(REPOSITORY_URI)]"
	eval `aws ecr get-login` && \
		docker push $(REPOSITORY_URI):$(SUB_HASH) | \
		tee $(OUT_DIR)/$(@)

push-latest: push tag-repo-latest
	@echo "[INFO] Pushing Docker Image to Repo: [$(REPOSITORY_URI)]"
	eval `aws ecr get-login` && \
		docker push $(REPOSITORY_URI):latest | \
		tee $(OUT_DIR)/$(@)

ecr-repo-uri:
ifndef ECR_REPOSITORY_URI_PREFIX
	@echo "[INFO] Obtaining ECR repository for image: [$(IMAGE_NAME)]"
	$(eval REPOSITORY_URI := $(call ecr-repo-uri))
	@if [ -z "$(REPOSITORY_URI)" ] ; then \
		echo "[ERROR] Could not find ECR repository." \
	         "Make sure to create the ECR stack." >&2 ; \
	    exit 1 ; \
	else \
		echo "[INFO] Found repository uri: [$(REPOSITORY_URI)]" ; \
	fi
else
	$(eval REPOSITORY_URI := $(addsuffix /$(IMAGE_NAME), $(ECR_REPOSITORY_URI_PREFIX)))
	echo "[INFO] Using repository uri from environmet: [$(REPOSITORY_URI)]"
endif
.PHONY: ecr-repo-uri

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

distclean: clean
	$(eval REPOSITORY_URI := $(call ecr-repo-uri))
	-@docker rmi $(IMAGE_NAME):$(SUB_HASH)
	-@docker rmi $(IMAGE_NAME)
	-@[ ! -z "$(REPOSITORY_URI)" ] && docker rmi $(REPOSITORY_URI) || :
	-@[ ! -z "$(REPOSITORY_URI)" ] && docker rmi $(REPOSITORY_URI):$(SUB_HASH) || :
.PHONY: distclean

run: build
	@echo "[INFO] Running shell in local container using image: [$(IMAGE_NAME)]"
	docker run --rm -ti $(IMAGE_NAME):$(SUB_HASH) ash
.PHONY: run

# get ECR repository URI from registry - needs aws cli and creds
# may want to set AWS_DEFAULT_PROFILE env var
define ecr-repo-uri
	$(shell aws ecr describe-repositories \
		--query 'repositories[?starts_with(repositoryName, `$(IMAGE_NAME)`)].repositoryUri' \
		--output text | head -1)
endef
