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.12-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)

PACKAGE_NAME := apicast-operator
DISPLAY_NAME := Red Hat Integration - 3scale APIcast gateway
STREAM ?= alpha

PRODUCT_PROPERTIES_FILE := tools/downstream-conversion/apicast.properties

# Different suffixes depending on Release Stream
BUNDLE_SUFFIX.alpha := alpha
BUNDLE_SUFFIX.dev := dev
BUNDLE_SUFFIX.stable :=
BUNDLE_SUFFIX := $(or $(BUNDLE_SUFFIX.$(STREAM)),)

CHANNEL := threescale-$(shell echo $(TAG) |  awk -F'.' ' { print $$1"."$$2 } ')
CHANNEL_NAME.alpha := alpha
CHANNEL_NAME.dev := $(CHANNEL)-dev
CHANNEL_NAME.stable := $(CHANNEL)
CHANNEL_NAME := $(or $(CHANNEL_NAME.$(STREAM)),)

ifeq ($(STREAM),stable)
BUNDLE_SUFFIX_FULL :=
BUNDLE_SUFFIX_WITH_SPACE :=
STREAM_REGISTRY_FROM ?= RHCC
STREAM_REGISTRY_TO ?= PROXY

else
BUNDLE_SUFFIX_FULL := -$(BUNDLE_SUFFIX)
nullstring :=
space := $(nullstring) # end of the line
BUNDLE_SUFFIX_WITH_SPACE := $(space)$(BUNDLE_SUFFIX)
STREAM_REGISTRY_FROM ?= RHCC
STREAM_REGISTRY_TO ?= PROXY
endif

REGISTRY.RHCC := registry\.redhat\.io\/
REGISTRY.QUAY := quay\.io\/
REGISTRY.PROXY := registry-proxy\.engineering\.redhat\.com\/

REGISTRY_NAMESPACE.RHCC := 3scale-amp2\/
REGISTRY_NAMESPACE.QUAY := 3scale\/
REGISTRY_NAMESPACE.PROXY := rh-osbs\/

APICAST_REPO.RHCC := apicast-gateway-rhel8
APICAST_REPO.QUAY := rh-apicast
APICAST_REPO.PROXY := 3scale-amp2-apicast-gateway-rhel8

OPERATOR_REPO.RHCC := apicast-rhel7-operator
OPERATOR_REPO.QUAY := rh-apicast-operator
OPERATOR_REPO.PROXY := 3scale-amp2-apicast-rhel7-operator


.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

.PHONY: update-bundle-suffix
update-bundle-suffix:
	@echo "Update metadata to avoid collisions and enforce per-release-channel policies"
	@echo "STREAM: $(STREAM)."
	make update-property PROPERTY_NAME=DISPLAY_NAME PROPERTY_VALUE="$(DISPLAY_NAME)$(BUNDLE_SUFFIX_WITH_SPACE)"
	make update-property PROPERTY_NAME=PROVIDER_NAME PROPERTY_VALUE="Red Hat$(BUNDLE_SUFFIX_WITH_SPACE)"
	make update-property PROPERTY_NAME=MATURITY PROPERTY_VALUE="$(STREAM)"
	make update-property PROPERTY_NAME=CHANNEL PROPERTY_VALUE="$(CHANNEL_NAME)"
	sed -E --in-place 's/(operators\.operatorframework\.io\.bundle\.channels\.v1=).+/\1$(CHANNEL_NAME)/' Dockerfile
	sed -E --in-place 's/(operators\.operatorframework\.io\.bundle\.channel\.default\.v1=).+/\1$(CHANNEL_NAME)/' Dockerfile


.PHONY: update-repos
update-repos:
	@echo "Update registry URLs to enforce per-release-channel policies"
	@echo "STREAM: $(STREAM). registry replacing from: $(STREAM_REGISTRY_FROM), registry replacing to: $(STREAM_REGISTRY_TO)"
	sed -E --in-place 's/$(REGISTRY.$(STREAM_REGISTRY_FROM))$(REGISTRY_NAMESPACE.$(STREAM_REGISTRY_FROM))$(APICAST_REPO.$(STREAM_REGISTRY_FROM))/$(REGISTRY.$(STREAM_REGISTRY_TO))$(REGISTRY_NAMESPACE.$(STREAM_REGISTRY_TO))$(APICAST_REPO.$(STREAM_REGISTRY_TO))/' $(PRODUCT_PROPERTIES_FILE)
	sed -E --in-place 's/$(REGISTRY.$(STREAM_REGISTRY_FROM))$(REGISTRY_NAMESPACE.$(STREAM_REGISTRY_FROM))$(OPERATOR_REPO.$(STREAM_REGISTRY_FROM))/$(REGISTRY.$(STREAM_REGISTRY_TO))$(REGISTRY_NAMESPACE.$(STREAM_REGISTRY_TO))$(OPERATOR_REPO.$(STREAM_REGISTRY_TO))/' $(PRODUCT_PROPERTIES_FILE)

.PHONY: update-image-property
update-image-property:
#if stable don't update
ifeq ($(STREAM),stable)
	@echo "Politely refusing to update image digest in image pullspec, because we want to use floating tags in stable release stream."
else
	sed -E --in-place 's/$(PROPERTY_NAME).*/$(PROPERTY_NAME)="$(PROPERTY_VALUE)"/' $(PRODUCT_PROPERTIES_FILE)
endif


.PHONY: update-property
update-property:
# sed man entries
# 	  -i      --in-place  Edit files in-place, saving backups with the specified extension.
#     -E      --regexp-extended Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's).  The re_format(7) manual page fully describes both formats.
#     Because -i takes an optional argument, it should not be followed by other short options:
	sed -E --in-place 's/$(PROPERTY_NAME).*/$(PROPERTY_NAME)="$(PROPERTY_VALUE)"/' $(PRODUCT_PROPERTIES_FILE)

.PHONY: generate-bundles
generate-bundles: update-bundle-suffix update-repos
	$(PROJECT_PATH)/tools/downstream-conversion/convert.sh

# find the last existing version

bump:
	@echo $(LAST_VERSION) | awk -F. -v a="$$1" -v b="$$2" -v c="$$3" '{printf("%d.%d.%d", $$1+a, $$2+b , $$3+c)}'

