# Befund-Schani build helpers.
#
# `make msi` reproduces the CI's Windows pipeline locally, entirely via
# public Docker images (no local wine/mingw/wixl needed) — see
# docs/releasing.md. Step 1 caches its work under build/olm-win, so
# re-runs are cheap.

DOCKER = docker run --rm --network=host -v $(CURDIR):/src -w /src

# Build identity: base version gets the git commit appended (0.3.0.<commit>).
# Baked into the bundle via src/befundschani/_commit.py (read by __init__.py).
COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)

.PHONY: msi dmg deb rpm test _commit

## Bake the current git commit into the package so the app reports 0.3.0.<commit>.
_commit:
	@printf 'COMMIT = "%s"\n' '$(COMMIT)' > src/befundschani/_commit.py
	@echo ">>> baked commit '$(COMMIT)' into src/befundschani/_commit.py"

## Build the Windows installer: olm wheel -> PyInstaller bundle -> MSI.
msi: _commit
	$(DOCKER) debian:bookworm bash -c "\
	    apt-get update -qq && apt-get install -yqq --no-install-recommends \
	        gcc mingw-w64 cmake make curl ca-certificates unzip \
	        python3 python3-cffi python3-setuptools && \
	    bash scripts/build_olm_windows_wheel.sh"
	$(DOCKER) tobix/pywine:3.13 sh -c "bash scripts/build_windows_dist.sh"
	$(DOCKER) debian:bookworm bash -c "\
	    apt-get update -qq && apt-get install -yqq --no-install-recommends \
	        msitools wixl python3 && \
	    bash scripts/build_windows_msi.sh"

## Build the Linux .deb packages (Debian trixie + Ubuntu 24.04). Each runs
## in its distro image; the script installs its own build deps. Sequential
## runs overwrite dist-linux/befundschani/ — the final .debs are
## distro-suffixed, so they don't collide.
deb: _commit
	$(DOCKER) debian:trixie bash scripts/build_linux_dist.sh
	$(DOCKER) ubuntu:24.04 bash scripts/build_linux_dist.sh

## Build the Linux .rpm package (AlmaLinux 9 — covers RHEL/Rocky/Stream 9).
rpm: _commit
	$(DOCKER) almalinux:9 bash scripts/build_linux_dist.sh

## Build the macOS .app + DMG. Must run ON a Mac — PyInstaller cannot
## cross-compile, and there is no Wine equivalent for macOS.
dmg: _commit
	@[ "$$(uname -s)" = "Darwin" ] || { \
	    echo "make dmg must run on macOS (see docs/releasing.md)"; exit 1; }
	bash scripts/build_macos_dmg.sh

test:
	uv run pytest tests/
