# This is the main interface into the repository for building the image of the Shibboleth SP reverse proxy.
#
# All targets assume they're running on a Linux or macOS host with Bash and Docker installed.
# Any other dependencies should be satisfied by containers.
#

.DEFAULT_GOAL := help

DEBUG = [DEBUG]
INFO = [INFO]
SUCCESS = [INFO]

VERSION := $(shell cat VERSION.txt)

compose := docker compose

.PHONY: all
all: build

.PHONY: build
build: ## Build the Docker image
	$(compose) build proxy
	@echo "$(SUCCESS) Successfully built!"

.PHONY: clean
clean: ## Remove build artifacts
	$(compose) down --rmi all
	@echo "$(SUCCESS) All clean!"

.PHONY: help
help: ## Display this help (default)
	@echo "Targets:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort \
	| awk 'BEGIN {FS = ":.*?## "}; {printf "  %-17s%s\n", $$1, $$2}'

.PHONY: release
release: ## Build the Docker image with the tagged version for release
	VERSION=${VERSION} $(compose) build proxy
	@docker images --format "{{.Repository}}:{{.Tag}}" | grep '^shib-proxy:' | \
	while read -r line; do \
		tag=$$(echo $$line | awk -F: '{print $$2}'); \
		if [ "$$tag" != "$(VERSION)" ]; then \
			echo "${INFO} Removing tag - $$tag"; \
			docker rmi shib-proxy:$$tag; \
		fi \
	done
	@echo "$(SUCCESS) Successfully built!"

.PHONY: test
test: ## Run the tests on the proxy Docker image
	$(compose) up -d selenium-firefox
	@echo "${INFO} Waiting for services to come up..."
	@sleep 30
	$(compose) up test
	$(compose) down