# Makefile for Sphinx documentation

# You can set these variables from the command line.
SPHINXOPTS    ?=
SPHINXBUILD   ?= sphinx-build
PAPER         ?=
BUILDDIR      ?= _build

# Internal variables.
PAPEROPT_a4     = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS   = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS)
I18NSPHINXOPTS  = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .

# the i18n builder cannot share the build directory with the others
I18NSPHINXOPTS  := $(I18NSPHINXOPTS) -d $(BUILDDIR)/doctrees

.PHONY: help Makefile

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

help:
	@echo "Please use 'make <target>' where <target> is one of:"
	@echo "  html           to make standalone HTML files"
	@echo "  dirhtml        to make HTML files named index.html in directories"
	@echo "  singlehtml     to make a single large HTML file"
	@echo "  pickle         to make pickle files"
	@echo "  json           to make JSON files"
	@echo "  htmlhelp       to make HTML files and an HTML help project"
	@echo "  qthelp         to make QT help project and HTML files"
	@echo "  devhelp        to make an Devhelp project and HTML files"
	@echo "  epub           to make an EPUB file"
	@echo "  latex          to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
	@echo "  latexpdf       to make LaTeX files and run them through pdflatex"
	@echo "  latexpdfja     to make LaTeX files and run them through platex and dvipdfmx"
	@echo "  text           to make text files"
	@echo "  man            to make manual pages"
	@echo "  texinfo        to make Texinfo files"
	@echo "  gettext        to make PO message catalogs"
	@echo "  changes        to make an overview of all changed/added/deprecated items"
	@echo "  xml            to make Docutils-compatible XML files"
	@echo "  pseudoxml      to make pseudoxml-compatible XML files"
	@echo "  linkcheck      to make link checker output"
	@echo "  doctest        to make doctest files"
	@echo "  coverage       to make coverage report"
	@echo "  dummy          to make dummy files"
	@echo "  clean          to remove build files"
	@echo "  cleanall       to remove all build files (including generated source files)"

.PHONY: clean
clean:
	rm -rf $(BUILDDIR)/*

.PHONY: cleanall
cleanall:
	rm -rf $(BUILDDIR)/*
	find . -type d -name _autosummary -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete
	find . -type d -name __pycache__ -delete

.PHONY: html
html:
	@echo "Building HTML documentation..."
	@$(SPHINXBUILD) -b html . $(BUILDDIR)/html
	@echo "Build finished. The HTML pages are in $(BUILDDIR)/html"

.PHONY: latexpdf
latexpdf:
	@echo "Building PDF documentation (via LaTeX)..."
	@$(SPHINXBUILD) -b latex . $(BUILDDIR)/latex
	@echo "Running LaTeX files through pdflatex..."
	@cd $(BUILDDIR)/latex && make all-pdf
	@echo "PDF files are in $(BUILDDIR)/latex"

.PHONY: serve
serve: html
	@echo "Serving documentation at http://localhost:8000"
	@cd $(BUILDDIR)/html && python -m http.server 8000

.PHONY: autobuild
autobuild:
	@echo "Auto-building documentation on changes..."
	sphinx-autobuild . $(BUILDDIR)/html --port 8000

.PHONY: linkcheck
linkcheck:
	@echo "Checking documentation links..."
	@$(SPHINXBUILD) -b linkcheck . $(BUILDDIR)/linkcheck
	@echo "Link check results in $(BUILDDIR)/linkcheck"

.PHONY: coverage
coverage:
	@echo "Checking documentation coverage..."
	@$(SPHINXBUILD) -b coverage . $(BUILDDIR)/coverage
	@echo "Coverage results in $(BUILDDIR)/coverage"

.PHONY: install-deps
install-deps:
	@echo "Installing documentation dependencies..."
	pip install -r requirements.txt

.PHONY: watch
watch:
	@echo "Watching for changes and rebuilding..."
	sphinx-autobuild . $(BUILDDIR)/html -B --port 8000
