AUTHOR = phsmith
PROJECT = $(notdir $(PWD))
IMAGE_NAME ?= rundeck-exporter
IMAGE_VERSION ?= latest
DOCKER_IMAGE_TAG = $(AUTHOR)/$(IMAGE_NAME)
GHCR_IMAGE_TAG = ghcr.io/$(AUTHOR)/$(PROJECT)/$(IMAGE_NAME)

RD_CLI_VERSION = 2.0.9
RD_CLI_JAR = rundeck-cli-$(RD_CLI_VERSION)-all.jar
RD_CLI_URL = https://github.com/rundeck/rundeck-cli/releases/download/v$(RD_CLI_VERSION)/$(RD_CLI_JAR)
CI_COMPOSE = examples/docker-compose/docker-compose-ci.yml

default: docker-build

docker-build:
	docker run --rm -i hadolint/hadolint:v2.14.0@sha256:27086352fd5e1907ea2b934eb1023f217c5ae087992eb59fde121dce9c9ff21e < Dockerfile

	docker pull $(GHCR_IMAGE_TAG) || true

	DOCKER_BUILDKIT=1 docker build \
		--cache-from=$(GHCR_IMAGE_TAG) \
		--cache-from=$(DOCKER_IMAGE_TAG) \
		--build-arg BUILDKIT_INLINE_CACHE=1 \
    	--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
    	--build-arg VCS_REF=`git rev-parse --short HEAD` \
    	--build-arg VERSION="$(IMAGE_VERSION)" \
		--target app \
    	--tag=$(IMAGE_NAME) $(BUILD_ARGS) .

	docker tag $(IMAGE_NAME) $(DOCKER_IMAGE_TAG):latest
	docker tag $(IMAGE_NAME) $(GHCR_IMAGE_TAG):latest
	docker tag $(IMAGE_NAME) $(DOCKER_IMAGE_TAG):$(IMAGE_VERSION)
	docker tag $(IMAGE_NAME) $(GHCR_IMAGE_TAG):$(IMAGE_VERSION)

docker-push:
	@echo "$(DOCKER_HUB_TOKEN)" | docker login -u $(AUTHOR) --password-stdin
	docker push $(DOCKER_IMAGE_TAG):$(IMAGE_VERSION)

ghcr-push:
	@echo "$(GITHUB_TOKEN)" | docker login ghcr.io -u $(AUTHOR) --password-stdin
	docker push $(GHCR_IMAGE_TAG):$(IMAGE_VERSION)

push-all:
	make -j2 docker-push ghcr-push

clean:
	@docker rmi --force `docker images | awk '/$(IMAGE_NAME)/ {print $$3}'` &> /dev/null || true
	@echo "> Project Docker images cleaned up."

git-push:
	[ -n "$$(git status --short --untracked-files=no)" ] && echo -e "\nNeed to commit changes before push.\n" && exit 1; true
	git tag -d latest
	git tag latest
	git push origin :latest
	git tag "v$(IMAGE_VERSION)"
	git push --all

debug:
	docker run --rm -it --name=$(IMAGE_NAME) --entrypoint=/bin/sh $(IMAGE_NAME)

local-env-setup:
	docker compose -f examples/docker-compose/docker-compose.yml up --build -d

local-env-teardown:
	docker compose -f examples/docker-compose/docker-compose.yml down

local-env-logs:
	docker compose -f examples/docker-compose/docker-compose.yml logs $(ARGS)

test-env-setup:
	docker compose -f $(CI_COMPOSE) up -d --wait
	curl -L -o /tmp/rd-cli.jar $(RD_CLI_URL)
	docker compose -f $(CI_COMPOSE) cp /tmp/rd-cli.jar rundeck:/tmp/
	docker compose -f $(CI_COMPOSE) cp examples/docker-compose/configs/rundeck/projects/test1.rdproject.jar rundeck:/tmp/
	docker compose -f $(CI_COMPOSE) exec \
		-e RD_URL=http://localhost:4440 \
		-e RD_TOKEN=exporter_admin_auth_token \
		rundeck /bin/bash -c "cd /tmp \
			&& java -jar rd-cli.jar projects create -p test1 || true \
			&& java -jar rd-cli.jar projects archives import -p test1 -f test1.rdproject.jar"
	@echo "> Waiting for Rundeck API to be ready..."
	@success=0; \
	for i in $$(seq 1 30); do \
		if curl -sf http://localhost:4440/api/41/system/info \
			-H "X-Rundeck-Auth-Token: exporter_admin_auth_token" > /dev/null 2>&1; then \
			echo "Rundeck API is ready."; success=1; break; \
		fi; \
		echo "Attempt $$i/30 - not ready, retrying in 5s..."; \
		sleep 5; \
	done; \
	[ $$success -eq 1 ] || { echo "Timed out waiting for Rundeck API to be ready"; exit 1; }
	@echo "> Waiting for scheduled job executions to complete..."
	@success=0; \
	for i in $$(seq 1 30); do \
		if curl -sf "http://localhost:4440/api/41/project/test1/executions?status=succeeded&max=1" \
			-H "X-Rundeck-Auth-Token: exporter_admin_auth_token" | grep -qP '"total"\s*:\s*[1-9]'; then \
			echo "Executions ready."; success=1; break; \
		fi; \
		echo "Attempt $$i/30 - waiting for executions..."; \
		sleep 5; \
	done; \
	[ $$success -eq 1 ] || { echo "Timed out waiting for scheduled executions"; exit 1; }

test-env-logs:
	docker compose -f $(CI_COMPOSE) logs $(ARGS)

test:
	uv run pytest

test-env-teardown:
	docker compose -f $(CI_COMPOSE) down --volumes
