MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
PROJECT_PATH := $(patsubst %/,%,$(dir $(MKFILE_PATH)))

COMPONENT = backend
MAJOR ?= 2
MINOR ?= 7
PATCHLEVEL ?= 0
RELEASE ?=
RHEL_MAJOR ?= 7
TARGET_EXTRA ?= -candidate

TAG = $(shell cut -f 2 -d ' ' source-repos)
BRANCH ?= $(TAG)
SOURCE ?= $(BRANCH).tar.gz

TARGET = 3scale-amp-$(MAJOR).$(MINOR)-rhel-$(RHEL_MAJOR)$(TARGET_EXTRA)

LOCAL_IMAGE_TAG = $(COMPONENT):$(MAJOR).$(MINOR).$(PATCHLEVEL)$(RELEASE)
SMOKE_IMAGE ?= $(LOCAL_IMAGE_TAG)

GIT_BRANCH = $(shell cd $(PROJECT_PATH) && git symbolic-ref --short HEAD)

IP_TOOLING = $(PROJECT_PATH)/../integration-platform-tooling

default: info

.PHONY: update-base-image
update-base-image:
	@echo WARNING: this is going to check the STAGING repository, not PUBLIC or BREW
	$(IP_TOOLING)/scripts/update-docker-base-image.sh -f Dockerfile -s
	@echo WARNING: if a change was made *REMEMBER* to increase the release if reusing the version.

.PHONY: info
info:
	@echo "************************************************************"
	@echo This Makefile builds 3scale-$(COMPONENT) $(MAJOR).$(MINOR).$(PATCHLEVEL)$(RELEASE) for RHEL $(RHEL_MAJOR)
	@echo BRANCH=$(GIT_BRANCH) TARGET=$(TARGET)
	@echo "************************************************************"
	@echo
	@echo Please check this matches your expectations and override variables if needed.
	@echo

$(COMPONENT)-$(BRANCH)/Gemfile.lock: $(COMPONENT)-$(SOURCE)
	tar --extract --gunzip --touch --file=$(COMPONENT)-$(SOURCE)

.PHONY: replace-gemfiles
replace-gemfiles:
	mv $(CURDIR)/$(COMPONENT)-$(BRANCH)/Gemfile.on_prem $(CURDIR)/$(COMPONENT)-$(BRANCH)/Gemfile || true
	mv $(CURDIR)/$(COMPONENT)-$(BRANCH)/Gemfile.on_prem.lock $(CURDIR)/$(COMPONENT)-$(BRANCH)/Gemfile.lock || true

$(COMPONENT)-$(SOURCE):
	rhpkg sources

.PHONY: build
build: local.repo info
	@echo This is how the local Brew repo looks like, make sure versions look ok
	@cat $(PROJECT_PATH)/local.repo
	@sleep 3
	@echo -e "\nBuilding local Docker image\n"
	docker build -t $(LOCAL_IMAGE_TAG) -f $(PROJECT_PATH)/Dockerfile $(PROJECT_PATH) --build-arg BUILD_TYPE=local

.PHONY: smoke
smoke:
	docker run \
		-d \
		--name backend-smoke-test \
		-e "CONFIG_REDIS_PROXY=backend-redis=6379" \
		-e "CONFIG_QUEUES_MASTER_NAME=backend-redis:6379/1" \
		-e "RACK_ENV=production" \
		-e "CONFIG_INTERNAL_API_USER=iapiuser" \
		-e "CONFIG_INTERNAL_API_PASSWORD=iapipasswd" \
		-t \
		$(SMOKE_IMAGE) \
			/bin/bash -- /opt/app/entrypoint.sh bin/3scale_backend start -e "production" -p 3000 -x /dev/stdout
	sleep 10
	docker exec backend-smoke-test bash -c 'curl -s -o /dev/null -w "%{http_code}" localhost:3000/status' | grep 200
	docker stop backend-smoke-test
	docker rm -v backend-smoke-test

.PHONY: test
test:
	cd $(PROJECT_PATH) && rhpkg container-build --scratch --target=$(TARGET) $(EXTRA_RHPKG_ARGS)

.PHONY: brew
brew:
ifeq (private-,$(findstring private-,$(GIT_BRANCH)))
	$(error Can't build from private branch $(GIT_BRANCH))
else
	@echo Building on branch $(GIT_BRANCH) - Please interrupt if this is not OK, you have 10 secs
	@sleep 10
	cd $(PROJECT_PATH) && rhpkg container-build --target=$(TARGET) $(EXTRA_RHPKG_ARGS)
endif

test-ga-release: EXTRA_RHPKG_ARGS=--repo-url http://download-node-02.eng.bos.redhat.com/rcm-guest/puddles/3scale-amp/$(MAJOR).$(MINOR)/latest/3scale-amp-$(MAJOR).$(MINOR).repo http://download-node-02.eng.bos.redhat.com/rcm-guest/puddles/3scale-amp/$(MAJOR).$(MINOR)/latest/external-3scale-amp-$(MAJOR).$(MINOR).repo http://download-node-02.eng.bos.redhat.com/rcm-guest/puddles/3scale-amp/$(MAJOR).$(MINOR)/extras/puddle.repo
test-ga-release: test

brew-ga-release: EXTRA_RHPKG_ARGS=--repo-url http://download-node-02.eng.bos.redhat.com/rcm-guest/puddles/3scale-amp/$(MAJOR).$(MINOR)/latest/3scale-amp-$(MAJOR).$(MINOR).repo http://download-node-02.eng.bos.redhat.com/rcm-guest/puddles/3scale-amp/$(MAJOR).$(MINOR)/latest/external-3scale-amp-$(MAJOR).$(MINOR).repo http://download-node-02.eng.bos.redhat.com/rcm-guest/puddles/3scale-amp/$(MAJOR).$(MINOR)/extras/puddle.repo
brew-ga-release: brew

local.repo:
	echo LOCAL REPO
	echo -e "[brew]\nname=RHAMP Brew Tag\nbaseurl=http://download-node-02.eng.bos.redhat.com/brewroot/repos/3scale-amp-$(MAJOR).$(MINOR)-rhel-$(RHEL_MAJOR)-build/latest/x86_64/\ngpgcheck=0\nenabled=1" > $(PROJECT_PATH)/local.repo

clean:
	rm -rf cache.tar.gz $(COMPONENT)-$(SOURCE) $(COMPONENT)-$(BRANCH) $(COMPONENT)
