BUILD_NUMBER?=1
PROJECT?=user-preferences-service
REPOSITORY=rti/user-preferences-service
AWS_ACCOUNT_ID?=714609459574
AWS_REGION?=us-east-1
AWS_PROFILE?=gdb-artistapp-dev
ECR?=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
ECR_REPOSITORY?=${REPOSITORY}
REGISTRY?=${ECR}/${REPOSITORY}

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")
SEMVER ?= $(shell cat VERSION )

## -
## Local commands:

##   local/init - install bumpversion
.PHONY: local/init
local/init: ## install bumpversion via pip
	@pip install --user --upgrade bumpversion

## -
## Version management

##   version/patch - increment version patch
.PHONY: version/patch
version/patch: ## increment version
	@npm version patch

##   version/minor - increment version minor
.PHONY: version/minor
version/minor:
	@npm version minor

##   version/major - increment version major
.PHONY: version/major
version/major:
	@npm version major

## -
## Docker commands:

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

##   <docker|podman>/login - login to AWS ECR registry using docker or podman, for aws cli version 2, aws configuration need to be set first
.PHONY:%/login_v2
%/login_v2:
	@aws ecr get-login-password \
		--profile ${AWS_PROFILE} \
		--region ${AWS_REGION} | \
		$* login --username AWS --password-stdin ${ECR}

##   <docker|podman>/image/build - build docker image
.PHONY: %/image/build
%/image/build:
	@$* pull ${REGISTRY}:develop
	@$* build \
		--cache-from ${REGISTRY}:develop \
		-t ${REGISTRY}:${VERSION} \
		.

##   <docker|podman>/image/push - push an image to a registry
.PHONY: %/image/push
%/image/push:
	@$* push ${REGISTRY}:${VERSION}

## -
## 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/^/ /'
