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

OPERATOR_SDK ?= $(shell command -v operator-sdk 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 OPERATOR_SDK
	$(error "operator-sdk is not available please install operator-sdk v0.18.2 https://github.com/operator-framework/operator-sdk/releases/tag/v0.18.2")
endif
	$(OPERATOR_SDK) bundle validate $(PROJECT_PATH)

## 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
