VENV := .venv
PY   := $(VENV)/bin/python
PIP  := $(VENV)/bin/pip

# setuptools_scm version, resolved on the host. Passed in through
# SETUPTOOLS_SCM_PRETEND_VERSION_FOR_* so the sdist build skips the VCS file
# finder (root=".." would pull in the whole repository) and builds without
# git (the containers) get it too.
DIST_ENV := SETUPTOOLS_SCM_PRETEND_VERSION_FOR_LR_LUMENLABEL_CLI
SCM_VERSION ?= $(shell $(PY) -m setuptools_scm 2>/dev/null)

# dist/ also holds the PyInstaller binary; only these are PyPI distributions.
DIST_FILES := dist/*.whl dist/*.tar.gz

PYPI_REPOSITORY ?= https://upload.pypi.org/legacy/
TEST_PYPI_REPOSITORY ?= https://test.pypi.org/legacy/

# PyPI keeps every version number forever and rejects a local (+...) segment,
# so an upload is refused unless the version is a clean lumenlabel-X.Y.Z tag.
CHECK_RELEASE_VERSION = case "$(SCM_VERSION)" in *dev*|*+*|"") echo "error: refusing to upload '$(SCM_VERSION)': build from a clean 'lumenlabel-X.Y.Z' tag"; exit 2 ;; esac

.DEFAULT_GOAL := install

.PHONY: install dev test test-only dist check publish publish-test clean package build-linux-docker build-windows-docker

$(VENV)/bin/activate:
	python3.12 -m venv $(VENV)
	$(PIP) install --upgrade pip

install: $(VENV)/bin/activate
	$(PIP) install .

dev: $(VENV)/bin/activate
	$(PIP) install -e ".[dev,package]"

test: dev test-only

test-only:
	$(VENV)/bin/pytest \
		--cov=lumenlabel \
		--cov-report=term-missing \
		--junit-xml=build/reports/junit.xml \
		tests/

# Clearing the previous sdist and wheel keeps $(DIST_FILES) to this build.
dist: dev
	rm -f dist/*.whl dist/*.tar.gz
	$(DIST_ENV)="$(SCM_VERSION)" $(PY) -m build

check: dist
	$(VENV)/bin/twine check $(DIST_FILES)

# twine reads TWINE_USERNAME/TWINE_PASSWORD itself, so the token never shows
# in the echoed recipe or in the upload's argv.
publish-test: check
	@$(CHECK_RELEASE_VERSION)
	@if [ -z "$$TEST_PYPI_TOKEN" ]; then echo "error: TEST_PYPI_TOKEN is required"; exit 2; fi
	@TWINE_USERNAME=__token__ TWINE_PASSWORD="$$TEST_PYPI_TOKEN" $(VENV)/bin/twine upload --repository-url $(TEST_PYPI_REPOSITORY) $(DIST_FILES)

publish: check
	@$(CHECK_RELEASE_VERSION)
	@if [ -z "$$PYPI_TOKEN" ]; then echo "error: PYPI_TOKEN is required"; exit 2; fi
	@TWINE_USERNAME=__token__ TWINE_PASSWORD="$$PYPI_TOKEN" $(VENV)/bin/twine upload --repository-url $(PYPI_REPOSITORY) $(DIST_FILES)

clean:
	rm -rf $(VENV) dist/ build/ .coverage
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null; true
	find . -name "*.egg-info" -type d -exec rm -rf {} + 2>/dev/null; true

# dist/lumenlabel for this host.
package: dev
	$(VENV)/bin/pyinstaller -y lumenlabel.spec

# dist/lumenlabel in python:3.12-slim-bookworm, the image of the app's build-linux
# job. Debian 12 sets the glibc floor of the .deb (packaging/deb/README.md).
build-linux-docker: dev
	docker run --rm \
		-e $(DIST_ENV)="$(SCM_VERSION)" \
		-v "$(CURDIR):/src" \
		-w /src \
		python:3.12-slim-bookworm \
		bash -c "apt-get update -qq && apt-get install -y -qq --no-install-recommends binutils >/dev/null && \
		python3 -m venv /build-venv && \
		/build-venv/bin/pip install --no-cache-dir --upgrade pip setuptools wheel && \
		/build-venv/bin/pip install --no-cache-dir -e '.[package]' && \
		/build-venv/bin/pyinstaller -y lumenlabel.spec; \
		rc=\$$?; chown -R $(shell id -u):$(shell id -g) build dist src 2>/dev/null; exit \$$rc"

# dist/lumenlabel.exe in the Wine image the app's Windows build uses.
build-windows-docker: dev
	docker run --rm \
		--entrypoint bash \
		-e $(DIST_ENV)="$(SCM_VERSION)" \
		-v "$(CURDIR):/src" \
		-w /src \
		batonogov/pyinstaller-windows:v5.0.0 \
		-c "pip install --no-cache-dir --upgrade -e '.[package]' && pyinstaller -y lumenlabel.spec; \
		rc=\$$?; chown -R $(shell id -u):$(shell id -g) build dist src 2>/dev/null; exit \$$rc"
