PROJECT?=ui
TAG?=latest

BASE_AWS_PROFILE    = gdb-infra-dev
BASE_AWS_ACCOUNT_ID = 483193324480
BASE_AWS_REGION     = us-east-1
BASE_ECR_REGISTRY   = $(BASE_AWS_ACCOUNT_ID).dkr.ecr.$(BASE_AWS_REGION).amazonaws.com

AWS_PROFILE    ?= gdb-mct-dev
AWS_ACCOUNT_ID ?= 925412343888
AWS_REGION     ?= us-east-1

ECR_REGISTRY        ?= $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
ECR_REPOSITORY_NAME ?= marketing/ui
ECR_REGISTRY_URI    ?= $(ECR_REGISTRY)/$(ECR_REPOSITORY_NAME)

WORKDIR?=$(shell pwd)
JEST_JUNIT_OUTPUT_DIR?=output
JEST_JUNIT_OUTPUT_NAME?=junit.xml

REVISION ?= $(shell git rev-parse --short=8 HEAD)
VERSION ?= $(shell git rev-parse --abbrev-ref HEAD)
BUILDTIME = $(shell date -u +"%Y%m%d%H%M%S")

## -
## Docker commands:

##   docker/login/base - login to AWS ECR registry using docker, aws configuration need to be set first
.PHONY:docker/login/base
docker/login/base:
	@aws ecr get-login \
		--profile ${BASE_AWS_PROFILE} \
		--no-include-email \
		--region ${BASE_AWS_REGION} | awk '{print $$6}' | \
		docker login -u AWS --password-stdin https://$(BASE_ECR_REGISTRY) || aws ecr get-login-password \
			--profile ${BASE_AWS_PROFILE} \
			--region ${BASE_AWS_REGION} | \
			docker login --username AWS --password-stdin https://$(BASE_ECR_REGISTRY)

##   docker/login - login to AWS ECR registry using docker, aws configuration need to be set first
.PHONY:docker/login
docker/login:
	@aws ecr get-login \
		--profile ${AWS_PROFILE} \
		--no-include-email \
		--region ${AWS_REGION} | awk '{print $$6}' | \
		docker login -u AWS --password-stdin https://$(ECR_REGISTRY) || aws ecr get-login-password \
			--profile ${AWS_PROFILE} \
			--region ${AWS_REGION} | \
			docker login --username AWS --password-stdin https://$(ECR_REGISTRY)

##   docker/image/build - build docker image
.PHONY: docker/test
docker/test:
	@docker build \
		--target deps \
		-t ${ECR_REGISTRY_URI}:${VERSION} \
		-f Dockerfile \
		.
	@mkdir -p ${WORKDIR}/output
	@docker run \
		--rm \
		-e JEST_JUNIT_OUTPUT_DIR=${JEST_JUNIT_OUTPUT_DIR} \
		-e JEST_JUNIT_OUTPUT_NAME=${JEST_JUNIT_OUTPUT_NAME} \
		-v ${WORKDIR}/output/:/usr/app/output/ \
		${ECR_REGISTRY_URI}:${VERSION} \
		yarn test:ci

##   docker/image/build - build docker image
.PHONY: docker/image/build
docker/image/build:
	@docker build \
		--build-arg AUTH0_DOMAIN \
		--build-arg AUTH0_CLIENT_ID \
		--build-arg AUTH0_API_AUDIENCE \
		--build-arg SENTRY_DSN \
		--build-arg AMPLITUDE_KEY \
		--build-arg STATIC_FILES_PATH \
		--build-arg LOGROCKET_KEY \
		--build-arg APPOLO_BASE_URL \
		--build-arg INTERCOM_ID \
		--build-arg ATLAS_AUTH_URL \
		--build-arg USE_OLD_AUTH \
		--build-arg SPLIT_KEY \
		--build-arg AG_GRID_KEY \
		-t ${ECR_REGISTRY_URI}:${VERSION} \
		-t ${ECR_REGISTRY_URI}:${TAG} \
		-f Dockerfile \
		.

##   docker/image/push - push an image to a registry
.PHONY: docker/image/push
docker/image/push: docker/login
	@docker push ${ECR_REGISTRY_URI}:${TAG}

## -
## help - this message
.PHONY: help
help: Makefile
	@echo "Application: ${APP}\n"
	@echo "Run command:\n  make <target>\n"
	@grep -E -h '^## .*' $(MAKEFILE_LIST) | sed -n 's/^##//p'  | column -t -s '-' |  sed -e 's/^/ /'
