#!/usr/bin/make -f
# ============================================================
# Endpoint Makefile — Lint, Test, Build, Publish
# ============================================================

.DELETE_ON_ERROR:
SHELL := /bin/bash
ROOT  := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
VERSION := $(shell sed -n 's/^version = "\(.*\)"/\1/p' $(ROOT)/pyproject.toml)

.PHONY: all build install install-man lint test clean release publish help

all: lint test

# --- Build PyPI package (wheel + sdist) ---
build:
	uv build --no-sources

# --- Install locally (editable) ---
install: install-man
	uv pip install -e "$(ROOT)"

# --- Install man page to system manpath ---
MAN_DIR := $(shell manpath 2>/dev/null | tr ':' '\n' | head -1)
ifeq ($(MAN_DIR),)
MAN_DIR := $(HOME)/.local/share/man
endif
install-man:
	mkdir -p "$(MAN_DIR)/man1"
	cp "$(ROOT)/man/endpoint.1" "$(MAN_DIR)/man1/endpoint.1"
	@echo "Man page installed to $(MAN_DIR)/man1/endpoint.1"

# --- Lint ---
lint:
	uv run ruff format --check .
	uv run ruff check .
	@echo "Lint passed."

# --- Test ---
test:
	uv run pytest tests/ -v
	@echo "Tests passed."

# --- Release (build + checksum) ---
release:
	uv run scripts/release.py --dry-run
	@echo "Release built."

# --- Publish to PyPI ---
publish:
	uv build
	uv publish
	@echo "Published to PyPI."

# --- Clean ---
clean:
	rm -rf "$(ROOT)/dist/"
	rm -rf "$(ROOT)/release/"
	rm -rf "$(ROOT)/*.egg-info"
	@echo "Cleaned."

# --- Help ---
help:
	@echo "Endpoint Makefile"
	@echo "  make all        — Lint + test (default)"
	@echo "  make build      — Build wheel + sdist"
	@echo "  make install    — Editable install + man page"
	@echo "  make install-man — Install man page only"
	@echo "  make lint       — Run ruff linter"
	@echo "  make test       — Run pytest suite"
	@echo "  make release    — Build release archive"
	@echo "  make publish    — Build & publish to PyPI"
	@echo "  make clean      — Remove build artifacts"
