# All heavy tools run inside the container; the host drives via make.
# Set NODOCKER=1 to run directly if xelatex/poppler/tesseract are installed locally.
# zsh does not export UID/GID; compose.yaml needs them to run as the tree's owner.
export UID := $(shell id -u)
export GID := $(shell id -g)

RUN := $(if $(NODOCKER),,docker compose run --rm -T tex)

OUT     := ross-1988-retypeset.pdf
SOURCES := build.sh tools/polish.py tools/gen_seal.py tools/gen_cover_mosaic.py tools/optimize_plates.py src/preamble.tex src/cover.tex src/colophon.tex \
           src/errata.tex $(wildcard src/pages/*.tex)

.DEFAULT_GOAL := pdf
.PHONY: pdf build image prep plate cover check reprocheck verify clean distclean shell help

# The PDF is a build product and is not in git. This remakes it from src/ and
# plates/, neither of which needs the ProQuest scan — only prep and plate do.
pdf: $(OUT)     ## typeset the edition (only if a source changed)

$(OUT): $(SOURCES)
	$(RUN) ./build.sh

build:            ## typeset unconditionally, even if nothing changed
	$(RUN) ./build.sh

image:            ## build the toolchain image (once)
	docker compose build

prep:             ## rasterize + OCR-scaffold pages A..B:  make prep A=19 B=32   [needs source/]
	$(RUN) tools/prep.sh $(A) $(B)

plate:            ## crop plate image for PDF page N:      make plate N=40 [ARGS="--top 0.06"]   [needs source/]
	$(RUN) python3 tools/crop_plate.py $(N) $(ARGS)

check:            ## consistency checks (manifest <-> files <-> macros), no build
	$(RUN) python3 tools/check.py

reprocheck:       ## every token of src/pages must reach the built PDF
	$(RUN) python3 tools/reprocheck.py $(OUT)

# Depends on `build`, not `pdf`: `pdf` is timestamp-conditional, and a source edited
# while a build is running leaves a PDF NEWER than the source it does not contain,
# so make would skip the rebuild and verify would pass against stale output.
verify: check build reprocheck   ## the full gate: check, typeset unconditionally, then prove nothing was dropped

cover:            ## rebuild the cover mosaic from plates/ and fonts/
	$(RUN) python3 tools/gen_cover_mosaic.py --force

shell:            ## interactive shell in the container
	docker compose run --rm tex bash

clean:            ## remove LaTeX aux and scratch rasters (keeps plates and the PDF)
	rm -rf work/*

distclean: clean  ## also drop the page rasters and the built PDF
	rm -rf scans/* $(OUT)

help:
	@grep -E '^[a-z-]+:.*##' Makefile | sed 's/:.*##/ —/'
