.DEFAULT_GOAL := help

UV ?= uv
ENV_FILE ?= .env

.PHONY: help build publish release

help:
	@printf '%s\n' 'make build    Build wheel and sdist, clearing old dist/ artifacts' 'make publish  Publish the existing dist/ artifacts using .env' 'make release  Build, then publish' 'Override credentials file with ENV_FILE=path/to/.env'

build:
	$(UV) build --clear

publish:
	@test -f "$(ENV_FILE)" || { echo 'Missing credentials file: create .env from .env.example or set ENV_FILE.' >&2; exit 1; }
	@test -n "$$(find dist -maxdepth 1 -name '*.whl' -print 2>/dev/null)" && test -n "$$(find dist -maxdepth 1 -name '*.tar.gz' -print 2>/dev/null)" || { echo 'Missing release artifacts. Run make build first.' >&2; exit 1; }
	@$(UV) run --no-project --no-sync --env-file "$(ENV_FILE)" -- sh -c 'test -n "$$UV_PUBLISH_TOKEN" || { echo "Set UV_PUBLISH_TOKEN in the credentials file." >&2; exit 1; }; exec "$$1" publish --trusted-publishing never dist/*.whl dist/*.tar.gz' sh "$(UV)"

release: build
	@$(MAKE) publish
