# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS    ?= -W --keep-going
SPHINXBUILD   ?= sphinx-build
PYTHON        ?= python3
SOURCEDIR     = source
BUILDDIR      = build

# Put it first so that "make" without argument is like "make help".
help:
	@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile html openapi screenshot


# Render the web client and save a fresh GUI screenshot for frontend.rst.
# Keeps the layout figure in sync with index.html on every build.
screenshot:
	playwright install chromium
	python generate_screenshot.py

# Dump the backend's live OpenAPI schema so the REST API reference can be
# rendered straight from it (see source/backend.rst) instead of by hand.
openapi:
	mkdir -p build
	$(PYTHON) -c "import json; from epc_tof_studio.backend.server import app; spec=app.openapi(); json.dump(spec, open('build/openapi.json', 'w')); json.dump(spec, open('source/openapi.json', 'w'))"

# Explicitly handle 'make html' to keep generated inputs fresh before Sphinx.
html: openapi linkcheck
	@$(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" -E $(SPHINXOPTS) $(O)

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option.  $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile openapi
	@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)