MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
PROJECT_PATH := $(patsubst %/,%,$(dir $(MKFILE_PATH)))
COMPONENT = apicast-operator-metadata
GIT_BRANCH = $(shell git symbolic-ref --short HEAD)
TAG = $(shell grep 'version=".*"' Dockerfile | cut -d'"' -f 2)
TARGET = 3scale-amp-2.8-rhel-7-candidate
LOCAL_IMAGE_TAG = $(COMPONENT):$(TAG)
OPERATORCOURIER := $(shell command -v operator-courier 2> /dev/null)

.DEFAULT_GOAL := help
.PHONY : help
help: Makefile
	@sed -n 's/^##//p' $<

## info: Build configuration info
.PHONY: info
info:
	@echo "************************************************************"
	@echo This Makefile builds $(COMPONENT)
	@echo BRANCH=$(GIT_BRANCH) TARGET=$(TARGET)
	@echo "************************************************************"
	@echo
	@echo Please check this matches your expectations and override variables if needed.
	@echo

## verify-manifest: Test manifests have expected format
verify-manifest:
ifndef OPERATORCOURIER
	$(error "operator-courier is not available please install pip3 install operator-courier")
endif
	rm -rf $(PROJECT_PATH)/.bundle || true
	mkdir -p $(PROJECT_PATH)/.bundle
	operator-courier --verbose flatten manifests/ $(PROJECT_PATH)/.bundle
	operator-courier --verbose verify --ui_validate_io $(PROJECT_PATH)/.bundle

## build: Run local build
.PHONY: build
build: info
	@echo -e "\nBuilding local Docker image\n"
	docker build -t $(LOCAL_IMAGE_TAG) -f "$(PROJECT_PATH)/Dockerfile" "$(PROJECT_PATH)"

## test: brew scratch-build
.PHONY: test
test:
	rhpkg container-build --scratch --target=$(TARGET)

## brew: brew build
.PHONY: brew
brew:
ifneq (,$(findstring private-,$(GIT_BRANCH)))
	$(error can't build from private branch $(GIT_BRANCH))
else
	rhpkg container-build --target=$(TARGET)
endif
