SHELL := /bin/sh

PLUGIN_FILE := $(shell awk '/^[[:space:]]*\* Plugin Name:/ { print FILENAME; exit }' ./*.php)
ifeq ($(strip $(PLUGIN_FILE)),)
$(error No plugin main file found (no *.php at the repo root has a "Plugin Name:" header))
endif
VERSION := $(shell awk -F': *' '/^[[:space:]]*\* Version:/ { print $$2; exit }' $(PLUGIN_FILE))
PLUGIN_SLUG := dlit-easy-math-captcha
ZIP_NAME := $(PLUGIN_SLUG)-v$(VERSION).zip
bump_ACTION := $(filter patch minor major sync,$(MAKECMDGOALS))

.PHONY: zip bump tested clean help patch minor major sync

zip:
	@tmp=$$(mktemp -d) && \
	rsync -a \
		--exclude='.*' \
		--exclude='.git/' \
		--exclude='Makefile' \
		--exclude='*.zip' \
		--exclude='README.md' \
		--exclude='CLAUDE.md' \
		--exclude='composer.json' \
		--exclude='composer.lock' \
		--exclude='phpunit.xml' \
		--exclude='tests/' \
		--exclude='vendor/' \
		. "$$tmp/$(PLUGIN_SLUG)/" && \
	cd "$$tmp" && zip -r "$(CURDIR)/$(ZIP_NAME)" "$(PLUGIN_SLUG)" && \
	rm -rf "$$tmp" && \
	echo "Created $(ZIP_NAME)"

# Fetch the current WordPress version (major.minor) and write it into the
# readme's "Tested up to" header. wp.org delists plugins whose header lags
# behind the latest release.
tested:
	@wp_version=$$(curl -sf --max-time 10 https://api.wordpress.org/core/version-check/1.7/ \
		| perl -ne 'if (/"current":"(\d+\.\d+)/) { print $$1; exit }'); \
	if [ -z "$$wp_version" ]; then \
		echo "Error: could not fetch the current WordPress version (offline?)"; \
		exit 1; \
	fi; \
	WP_TESTED="$$wp_version" perl -0pi -e 's/^(Tested up to:\s*).*$$/$$1$$ENV{WP_TESTED}/m' readme.txt; \
	echo "Tested up to: $$wp_version"

bump:
	@action="$(bump_ACTION)"; \
	if [ -z "$$action" ]; then \
		echo "Usage: make bump [patch|minor|major|sync]"; \
		exit 1; \
	fi; \
	current_version="$$(awk -F': *' '/^[[:space:]]*\* Version:/ { print $$2; exit }' $(PLUGIN_FILE))"; \
	new_version="$$current_version"; \
	case "$$action" in \
		patch|minor|major) \
			new_version="$$(CURRENT_VERSION="$$current_version" BUMP_TYPE="$$action" awk 'BEGIN { \
				split(ENVIRON["CURRENT_VERSION"], parts, "."); \
				major = parts[1] + 0; \
				minor = parts[2] + 0; \
				patch = parts[3] + 0; \
				bump = ENVIRON["BUMP_TYPE"]; \
				if (bump == "major") { \
					major += 1; minor = 0; patch = 0; \
				} else if (bump == "minor") { \
					minor += 1; patch = 0; \
				} else { \
					patch += 1; \
				} \
				printf "%d.%d.%d", major, minor, patch; \
			}')"; \
			;; \
		sync) \
			;; \
		*) \
			echo "Usage: make bump [patch|minor|major|sync]"; \
			exit 1; \
			;; \
	esac; \
	SYNC_VERSION="$$new_version" perl -0pi -e 's/^(\s*\* Version:\s*).*$$/$$1$$ENV{SYNC_VERSION}/m; s/(define\(\s*'\''DLIT_MATH_CAPTCHA_VERSION'\'',\s*'\'').*?('\''\s*\);)/$$1$$ENV{SYNC_VERSION}$$2/m' $(PLUGIN_FILE); \
	SYNC_VERSION="$$new_version" perl -0pi -e 's/^(Stable tag:\s*).*$$/$$1$$ENV{SYNC_VERSION}/m' readme.txt; \
	if [ "$$action" = "sync" ]; then \
		echo "Synced version to $$new_version"; \
	else \
		echo "Bumped version: $$current_version -> $$new_version"; \
	fi
	-@$(MAKE) --no-print-directory tested

clean:
	@rm -f $(PLUGIN_SLUG)-v*.zip
	@echo "Removed all $(PLUGIN_SLUG)-v*.zip files"

help:
	@echo "Targets:"
	@echo "  zip              Package the plugin into $(PLUGIN_SLUG)-vX.Y.Z.zip"
	@echo "  clean            Remove all generated zip files"
	@echo "  tested           Fetch current WordPress version and update 'Tested up to' in readme.txt"
	@echo "  bump patch        Increment patch version (X.Y.Z+1), sync, refresh tested"
	@echo "  bump minor        Increment minor version (X.Y+1.0), sync, refresh tested"
	@echo "  bump major        Increment major version (X+1.0.0), sync, refresh tested"
	@echo "  bump sync         Re-sync version from plugin header to readme.txt without incrementing"

patch minor major sync:
	@:
