export PYTHONDONTWRITEBYTECODE := 1
PY := $(shell [ -x .venv/bin/python ] && echo .venv/bin/python || echo python3)
PACK ?= variance-bridge
FILE ?=
PURPOSE ?= cover
PROMPT ?=
# The 20-file count is one host's documented limit: a warning by default.
# BUDGET=strict makes it an error, for a pack bound for that host.
BUDGET ?= warn
# Reader checks: the pack tests, the shared checks core, the scripts' input errors.
READER_TESTS = $(sort $(wildcard tests/test_pack_*.py)) tests/test_checks_core.py tests/test_script_errors.py

.PHONY: reader-setup reader-check companion setup check validate sync sync-check house-release install-check lint style test book review portability portability-dry split portability-split portability-split-dry image models

# Reader setup: Python 3.11+ and pip only; no git, no TeX, no book build.
reader-setup:
	@bash setup.sh --reader

reader-check:
	@$(PY) -c "import yaml, pytest, pptx" 2>/dev/null || { echo "The reader packages are missing: run make reader-setup first."; exit 2; }
	@find packs -name __pycache__ -prune -exec rm -rf {} +
	@echo "Validate the packs"
	@PATH="$(CURDIR)/.venv/bin:$$PATH" $(PY) tools/validate_pack.py --budget $(BUDGET) packs/*/
	@echo "Run the pack tests"
	@$(PY) -m pytest -q $(READER_TESTS)
	@echo "reader-check: the packs validate and their tests pass"

# The companion files: check that the archive holds everything the book needs, then build
# dist/skills-pack-companion-<version>.zip and dist/SHA256SUMS (version and rule: tools/companion_files.py).
companion:
	@$(PY) tools/companion_check.py -q
	@$(PY) tools/companion_build.py

# Manuscript setup: also git and a TeX engine; builds the book.
setup:
	@bash setup.sh

check: validate lint style test book

validate: sync-check
	@find packs examples house answers -name __pycache__ -prune -exec rm -rf {} +
	PATH="$(CURDIR)/.venv/bin:$$PATH" $(PY) tools/validate_pack.py --budget $(BUDGET) packs/*/
	PATH="$(CURDIR)/.venv/bin:$$PATH" $(PY) tools/validate_pack.py --budget $(BUDGET) answers/capstone/vendor-rebates/
	PATH="$(CURDIR)/.venv/bin:$$PATH" $(PY) tools/validate_pack.py --budget $(BUDGET) --auto $$(dirname $$(ls house/*/SKILL.md))
	@if ls -d examples/*/*/SKILL.md >/dev/null 2>&1; then PATH="$(CURDIR)/.venv/bin:$$PATH" $(PY) tools/validate_pack.py --budget $(BUDGET) --auto $$(dirname $$(ls examples/*/*/SKILL.md)); fi

# House skills: house/ upstream, each pack's _shared/ copies and house.lock downstream.
sync:
	$(PY) tools/sync_house.py

sync-check:
	$(PY) tools/sync_house.py --check

# After a change to house/: bump house/VERSION, then write its release manifest.
house-release:
	$(PY) tools/sync_house.py --release

# Compare an installed pack with its library manifest: make install-check INSTALLED=<folder>
INSTALLED ?=
install-check:
	@test -n "$(INSTALLED)" || (echo "usage: make install-check INSTALLED=<installed pack folder>" && exit 2)
	$(PY) tools/library_manifest.py check $(INSTALLED)

lint:
	$(PY) tools/lint_manuscript.py

style:
	$(PY) tools/style_check.py -q

test:
	$(PY) -m pytest -q

book:
	bash tools/build_book.sh

review:
	@test -n "$(FILE)" || (echo "usage: make review FILE=book/tex/ch04.tex" && exit 2)
	$(PY) tools/second_opinion.py $(FILE)

portability:
	$(PY) tools/portability.py packs/$(PACK) --sentinels

portability-dry:
	$(PY) tools/portability.py packs/$(PACK) --sentinels --dry-run

split:
	$(PY) tools/split_pack.py packs/$(PACK) build/split/$(PACK) --force

portability-split:
	$(PY) tools/portability.py packs/$(PACK) --sentinels --shape split

portability-split-dry:
	$(PY) tools/portability.py packs/$(PACK) --sentinels --shape split --dry-run

image:
	@test -n "$(PROMPT)" || (echo 'usage: make image PURPOSE=cover PROMPT="..."' && exit 2)
	$(PY) tools/make_image.py --purpose $(PURPOSE) --prompt "$(PROMPT)"

models:
	$(PY) tools/second_opinion.py --list-models
