HUGO     := hugo
PAGEFIND := npx -y pagefind
LYCHEE   := lychee
OUT      := public

.PHONY: all build index serve run link-check link-fix external-check serve-lan clean

all: index

# Build Hugo content -> public/
build:
	$(HUGO) --gc

# (Re)build the Pagefind search index from public/ -> public/pagefind/
index: build
	$(PAGEFIND) --site $(OUT)

# Build content + index, then serve the built site WITH search on :1414 (dev preview).
# Uses pagefind's own server. Local only (binds 127.0.0.1) -- for LAN use `make serve-lan`.
serve: index
	$(PAGEFIND) --site $(OUT) --serve

run: serve

# DEAD LINKS -- internal (offline, fast): scan the SOURCE markdown; internal links
# resolve to file:// under public/. External links are skipped by --scheme file.
# Output: linkcheck/internal.json (exit != 0 when dead links exist; that is fine).
link-check: build
	mkdir -p linkcheck
	$(LYCHEE) --root-dir $(OUT) --default-extension html --scheme file \
	  --format json --timeout 10 \
	  $(wildcard content/*.md) $(wildcard content/*/*.md) > linkcheck/internal.json

# Comment out confirmed dead INTERNAL page links, then rebuild content + index so they
# leave the live page AND the search index. (External failures -> manual_review.txt.)
link-fix:
	python3 scripts/fix_dead_links.py --report linkcheck/internal.json --apply
	$(HUGO) --gc
	$(PAGEFIND) --site $(OUT)

# DEAD LINKS -- external (network): scan all http/https links in content. Report-only.
# Always exits 0; the report is the point.
external-check:
	python3 scripts/check_external_links.py

# Serve the BUILT site on all interfaces so another LAN machine can open it:
#   http://<this-machine-LAN-IP>:8090/    (IP printed at startup; also `hostname -I | head -1`)
serve-lan:
	python3 scripts/serve_lan.py

clean:
	rm -rf $(OUT) linkcheck
