# docs/book/Makefile -- the single LithosAnanke book, built from LaTeX.
#
# Run from the repo root (the root Makefile does this):
#   make docs                   -> build/docs/LithosAnanke.pdf
#   make docs TARGET=<board>    -> build/docs/LithosAnanke-<board>.pdf
#                                  (same book + that board's appendix)
#
# docs/book/main.tex is the master. Markdown sources are converted by pandoc
# into LaTeX fragments under build/docs/gen/ and \input from main.tex; the
# Markdown stays the source of truth until a chapter is rewritten in LaTeX.
# Figures and tables that present data read the CSV directly at LaTeX time
# (pgfplots / pgfplotstable) and cite it with \datasource{path}; see
# docs/book/README.md.

BOOK_DIR := docs/book
OUT      := build/docs
GEN      := $(OUT)/gen

BOARD      ?=
BOARD_NAME ?=
VARIANT    := $(if $(BOARD),$(BOARD),book)
VAR_DIR    := $(OUT)/$(VARIANT)
PDF        := $(OUT)/LithosAnanke$(if $(BOARD),-$(BOARD)).pdf

PANDOC  ?= pandoc
LATEXMK ?= latexmk
PANDOC_FILTERS := $(BOOK_DIR)/pandoc/table-widths.lua $(BOOK_DIR)/pandoc/code-breaks.lua
PANDOC_FLAGS := -f gfm -t latex --top-level-division=chapter --wrap=preserve \
    $(foreach f,$(PANDOC_FILTERS),--lua-filter=$(f))

# fragment name : Markdown source. The order of \input lines is main.tex's.
CHAPTERS := \
    ontology:docs/reference/ONTOLOGY.md \
    roadmap:docs/working/archive/superseded/2025-12-14-starforth-roadmap.md \
    boards:boards/README.md \
    justification:docs/v4.0.0/JUSTIFICATION.md \
    decomposition:docs/v4.0.0/DECOMPOSITION.md \
    fabric-0:docs/fabric/FABRIC-0.md \
    fabric-1:docs/fabric/FABRIC-1.md \
    fabric-2:docs/fabric/FABRIC-2.md \
    fabric-3:docs/fabric/FABRIC-3.md \
    fabric-3-5:docs/fabric/FABRIC-3.5.md \
    fabric-3-6:docs/fabric/FABRIC-3.6.md \
    fabric-3-7:docs/fabric/FABRIC-3.7.md \
    fabric-4:docs/fabric/FABRIC-4.md

ifneq ($(BOARD),)
ifeq ($(wildcard boards/$(BOARD)/README.md),)
    $(error boards/$(BOARD)/README.md is missing: it is the board's appendix)
endif
CHAPTERS += board-$(BOARD):boards/$(BOARD)/README.md
endif

chapter_name = $(word 1,$(subst :, ,$(1)))
chapter_src  = $(word 2,$(subst :, ,$(1)))
FRAGMENTS := $(foreach c,$(CHAPTERS),$(GEN)/$(call chapter_name,$(c)).tex)

.PHONY: all tools FORCE
all: $(PDF)

tools:
	@missing=""; \
	for t in $(PANDOC) $(LATEXMK) xelatex; do command -v $$t >/dev/null 2>&1 || missing="$$missing $$t"; done; \
	if [ -n "$$missing" ]; then \
	    echo "Error: make docs needs:$$missing"; \
	    echo "  sudo apt-get install -y pandoc latexmk texlive-xetex texlive-latex-extra texlive-pictures texlive-fonts-recommended fonts-dejavu fonts-dejavu-extra"; \
	    echo "  (or a user-local TinyTeX + pandoc in ~/.local; see docs/book/README.md)"; \
	    exit 1; \
	fi

# --id-prefix keeps heading labels unique across documents that reuse
# section names ("Summary", "Open questions", ...).
define chapter_rule
$(GEN)/$(call chapter_name,$(1)).tex: $(call chapter_src,$(1)) $(PANDOC_FILTERS) | tools
	@mkdir -p $(GEN)
	@echo "  PANDOC  $$< -> $$@"
	@$(PANDOC) $(PANDOC_FLAGS) --id-prefix=$(call chapter_name,$(1))- $$< -o $$@
endef
$(foreach c,$(CHAPTERS),$(eval $(call chapter_rule,$(c))))

# Pandoc's syntax-highlighting macros, taken from the installed pandoc so the
# fragments and their macros always come from the same version.
$(GEN)/pandoc-highlighting.tex: $(BOOK_DIR)/pandoc/highlighting.latex | tools
	@mkdir -p $(GEN)
	@printf '```c\nx\n```\n' | $(PANDOC) -f gfm -t latex -s --template=$< -o $@

# Per-build facts the book prints: the commit every \datasource refers to,
# and which board appendix (if any) is included.
$(VAR_DIR)/meta.tex: FORCE
	@mkdir -p $(VAR_DIR)
	@{ \
	    c=$$(git rev-parse --short=12 HEAD 2>/dev/null || echo unknown); \
	    git diff --quiet HEAD -- 2>/dev/null || c="$$c (+ uncommitted changes)"; \
	    printf '\\newcommand{\\bookcommit}{%s}\n' "$$c"; \
	    printf '\\newcommand{\\bookdate}{%s}\n' "$$(date -u +%Y-%m-%d)"; \
	    printf '\\def\\bookboard{%s}\n' "$(BOARD_NAME)"; \
	    $(if $(BOARD),printf '\\newcommand{\\bookboardappendix}{board-%s}\n' "$(BOARD)";) \
	} > $@.tmp
	@cmp -s $@.tmp $@ && rm -f $@.tmp || mv $@.tmp $@

FORCE:

TEXINPUTS_BOOK := $(abspath $(VAR_DIR)):$(abspath $(GEN)):$(abspath $(BOOK_DIR)):

$(PDF): $(BOOK_DIR)/main.tex $(FRAGMENTS) $(GEN)/pandoc-highlighting.tex $(VAR_DIR)/meta.tex | tools
	@echo "  LATEXMK $(BOOK_DIR)/main.tex -> $@"
	@TEXINPUTS=$(TEXINPUTS_BOOK) $(LATEXMK) -xelatex -interaction=nonstopmode -halt-on-error \
	    -file-line-error -outdir=$(VAR_DIR) $(BOOK_DIR)/main.tex > $(VAR_DIR)/latexmk.log 2>&1 || { \
	    grep -A4 -E '^(.*:[0-9]+:|!)' $(VAR_DIR)/main.log | head -40; \
	    echo "Error: LaTeX failed; full log: $(VAR_DIR)/main.log"; exit 1; }
	@cp $(VAR_DIR)/main.pdf $@
	@echo "  PDF     $@"
