MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
PROJECT_PATH := $(patsubst %/,%,$(dir $(MKFILE_PATH)))
DOCKER ?= $(shell which podman 2> /dev/null || which docker 2> /dev/null || echo "docker")

# The component name according to product/dist-git
COMPONENT ?= 3scale-istio-adapter
GO_SRC_PATH ?= github.com/3scale
# UPSTREAM: Usually the project within the GH organization
# this helps name the tarball from GH and the directory within
# the tarball for the root of the project.
UPSTREAM ?= 3scale-istio-adapter
SMOKE_BIN ?= /opt/app/3scale-istio-adapter
RELEASE ?=
RHEL_MAJOR ?= 8
TARGET_EXTRA ?= -candidate

SOURCE_REPOS = $(shell find "$(PROJECT_PATH)" -maxdepth 1 -name "source-repos*" -print -quit)

SRC_TAG ?= $(shell cut -f 2 -d ' ' $(SOURCE_REPOS))
# IMG_TAG is intended to be the container image tag, usually the same as the software version, but you can change it.
IMG_TAG ?= $(SRC_TAG)
REPO ?= $(shell cut -f 1 -d ' ' $(SOURCE_REPOS))
TARBALL ?= $(UPSTREAM)-$(SRC_TAG).tar.gz
TARBALL_URL ?= $(patsubst %.git,%,$(REPO))/archive/$(SRC_TAG)/$(TARBALL)

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

# The product this component is a part of - matches the release branch
PRODUCT ?= rhossm-1.0-rhel-$(RHEL_MAJOR)
TARGET = $(PRODUCT)$(TARGET_EXTRA)

LOCAL_IMAGE_TAG = $(COMPONENT):$(IMG_TAG)$(RELEASE)
SMOKE_CONTAINER ?= $(COMPONENT)-smoke
SMOKE_IMAGE ?= $(LOCAL_IMAGE_TAG)
VENDOR_CONTAINER ?= $(COMPONENT)-vendor
VENDOR_IMAGE ?= $(VENDOR_CONTAINER):$(IMG_TAG)$(RELEASE)

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

THREESCALE_LISTEN_ADDR := "3333"

.PHONY: default
default: info

.PHONY: build-vendor-image
build-vendor-image:
	$(DOCKER) build \
		--build-arg REPO="$(REPO)" \
		--build-arg SRC_TAG="$(SRC_TAG)" \
		--build-arg PROJECT_NAME=$(UPSTREAM) \
		-t $(VENDOR_IMAGE) -f "$(PROJECT_PATH)/Dockerfile.sources" .

$(TARBALL): build-vendor-image
	-$(DOCKER) create --name $(VENDOR_CONTAINER) $(VENDOR_IMAGE)
	$(DOCKER) cp "$(VENDOR_CONTAINER):/tmp/$(TARBALL)" "$(PROJECT_PATH)/"; \
		EXITVAL=$$?; \
		$(DOCKER) rm $(VENDOR_CONTAINER) 2> /dev/null >&2; \
		exit $${EXITVAL}

.PHONY: vendor-sources
vendor-sources: $(TARBALL) sources-dockerfile
	md5sum $(TARBALL) > "$(PROJECT_PATH)/sources"

.PHONY: sources-dockerfile
sources-dockerfile:
	# modify default ARGs in Dockerfile for OSBS to pick them up
	sed -i -E -e "s/^ARG TARBALL.*$$/ARG TARBALL=$$(echo "$(TARBALL)" | sed -E -e 's/[&/\]/\\&/g')/" \
		-e "s/^ARG SRC_TAG.*$$/ARG SRC_TAG=$$(echo "$(SRC_TAG)" | sed -E -e 's/[&/\]/\\&/g')/" \
		-e "s/^LABEL version.*$$/LABEL version=\"$$(echo "$(IMG_TAG)" | sed -E -e 's/^v(.*)/\1/' -e 's/[&/\]/\\&/g')\" \\\\/" \
		"$(PROJECT_PATH)/Dockerfile"

.PHONY: sources
sources:
	test -r "$(PROJECT_PATH)/$(TARBALL)" || $(MAKE) -f $(MKFILE_PATH) vendor-sources
	rhpkg new-sources "$(PROJECT_PATH)/$(TARBALL)"

.PHONY: fetch-sources
fetch-sources:
	cd "$(PROJECT_PATH)" && rhpkg sources

.PHONY: new-sources
new-sources:
	@if test "x$(GH_TAG)" = "x"; then \
		echo >&2 "Please set up GH_TAG variable"; \
		exit 1; \
	fi
	@GH_REPO=$(GH_REPO); \
	if test "x$(GH_REPO)" = "x"; then \
		echo >&2 "Warning: reusing repository as $(REPO) - set GH_REPO if you want a different one"; \
		GH_REPO="$(REPO)"; \
	fi; \
	echo "$${GH_REPO} $(GH_TAG)" > "$(SOURCE_REPOS)" && \
	echo "Run 'make vendor-sources' to build the vendored tarball"

.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 "$(PROJECT_PATH)/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 $(COMPONENT):$(IMG_TAG)$(RELEASE) for RHEL $(RHEL_MAJOR)
	@echo BRANCH=$(GIT_BRANCH) TARGET=$(TARGET)
	@echo IMG_TAG=$(IMG_TAG)
	@echo SRC_TAG=$(SRC_TAG)
	@echo REPO=$(REPO)
	@echo TARBALL_URL=$(TARBALL_URL)
	@echo "************************************************************"
	@echo
	@echo Please check this matches your expectations and override variables if needed.
	@echo

.PHONY: build
build: info
	test -r "$(PROJECT_PATH)/$(TARBALL)" || $(MAKE) -f $(MKFILE_PATH) "$(TARBALL)"
	@echo -e "\nBuilding local Docker image\n"
	$(DOCKER) build \
		--build-arg SRC_TAG="$(SRC_TAG)" \
		--build-arg TARBALL="$(TARBALL)" \
		-t $(LOCAL_IMAGE_TAG) -f "$(PROJECT_PATH)/Dockerfile" "$(PROJECT_PATH)"

.PHONY: smoke
smoke:
	$(DOCKER) run --rm -e "THREESCALE_LISTEN_ADDR=$(THREESCALE_LISTEN_ADDR)" -p $(THREESCALE_LISTEN_ADDR):$(THREESCALE_LISTEN_ADDR) --name $(SMOKE_CONTAINER) $(SMOKE_IMAGE) $(SMOKE_BIN) &
	@echo You can now test against the local port "$(THREESCALE_LISTEN_ADDR)" and to stop testing, run "$(DOCKER) kill $(SMOKE_CONTAINER)"

.PHONY: smoke-cli
smoke-cli:
	$(DOCKER) run --rm --entrypoint=./3scale-config-gen  $(SMOKE_IMAGE) -u https://any.com -n smoke -t test --name somename; \
	if [ $$? != 0 ]; then \
	    echo "cli smoke test failed"; \
	    exit 1; \
	fi

.PHONY: smoke-kill
smoke-kill:
	$(DOCKER) kill $(SMOKE_CONTAINER)

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

.PHONY: brew
brew: info
ifeq (private-,$(findstring private-,$(GIT_BRANCH)))
	$(error Cannot 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

clean-vendor:
	$(DOCKER) rmi $(VENDOR_IMAGE)

clean:
	rm -rf "$(TARBALL)"
