# Makefile for Sphinx documentation
#
# Use "make docs" to automatically regenerate API docs and build HTML output.

SPHINXOPTS    ?=
SPHINXBUILD   ?= sphinx-build
SPHINXAPIDOC  ?= sphinx-apidoc
SOURCEDIR     = source
BUILDDIR      = build
CODEDIR       = ../src/pyftle

# Default target: show help
help:
	@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile clean api html docs

# Remove generated documentation
clean:
	@echo "Cleaning build and autogenerated files..."
	rm -rf "$(BUILDDIR)" "$(SOURCEDIR)"/pyftle*.rst "$(SOURCEDIR)"/modules.rst
	@echo "Done."

# Automatically (re)generate API .rst files from Python sources
api:
	@echo "Generating API documentation from source..."
	@$(SPHINXAPIDOC) -o "$(SOURCEDIR)" "$(CODEDIR)" \
		"$(CODEDIR)/grid_interp/*" \
		--force --no-toc
	@echo "API docs updated."

# Build HTML output
html: api
	@echo "Building HTML documentation..."
	@$(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
	@echo "Documentation available at: $(BUILDDIR)/html/index.html"

# High-level target for everything
docs: clean html
	@echo "✅ Documentation successfully rebuilt."
