# justfile for django-docutils documentation
# https://just.systems/

set shell := ["bash", "-uc"]

# Configuration
http_port := "8028"
builddir := "_build"
sphinxopts := ""
sphinxbuild := "uv run sphinx-build"
sourcedir := "."

# File patterns for watching
watch_files := "find .. -type f -not -path '*/\\.*' | grep -i '.*[.]\\(rst\\|md\\)$\\|.*[.]py$\\|CHANGES\\|TODO\\|.*conf\\.py' 2> /dev/null"

# Sphinx options
allsphinxopts := "-d " + builddir + "/doctrees " + sphinxopts + " ."

# List all available commands
default:
    @just --list

# Build HTML documentation
[group: 'build']
html:
    {{ sphinxbuild }} -b dirhtml {{ allsphinxopts }} {{ builddir }}/html
    @echo ""
    @echo "Build finished. The HTML pages are in {{ builddir }}/html."

# Build directory HTML files
[group: 'build']
dirhtml:
    {{ sphinxbuild }} -b dirhtml {{ allsphinxopts }} {{ builddir }}/dirhtml
    @echo ""
    @echo "Build finished. The HTML pages are in {{ builddir }}/dirhtml."

# Build single HTML file
[group: 'build']
singlehtml:
    {{ sphinxbuild }} -b singlehtml {{ allsphinxopts }} {{ builddir }}/singlehtml
    @echo ""
    @echo "Build finished. The HTML page is in {{ builddir }}/singlehtml."

# Build EPUB
[group: 'build']
epub:
    {{ sphinxbuild }} -b epub {{ allsphinxopts }} {{ builddir }}/epub
    @echo ""
    @echo "Build finished. The epub file is in {{ builddir }}/epub."

# Build LaTeX files
[group: 'build']
latex:
    {{ sphinxbuild }} -b latex {{ allsphinxopts }} {{ builddir }}/latex
    @echo ""
    @echo "Build finished; the LaTeX files are in {{ builddir }}/latex."

# Build PDF via LaTeX
[group: 'build']
latexpdf:
    {{ sphinxbuild }} -b latex {{ allsphinxopts }} {{ builddir }}/latex
    @echo "Running LaTeX files through pdflatex..."
    make -C {{ builddir }}/latex all-pdf
    @echo "pdflatex finished; the PDF files are in {{ builddir }}/latex."

# Build plain text files
[group: 'build']
text:
    {{ sphinxbuild }} -b text {{ allsphinxopts }} {{ builddir }}/text
    @echo ""
    @echo "Build finished. The text files are in {{ builddir }}/text."

# Build man pages
[group: 'build']
man:
    {{ sphinxbuild }} -b man {{ allsphinxopts }} {{ builddir }}/man
    @echo ""
    @echo "Build finished. The manual pages are in {{ builddir }}/man."

# Build JSON output
[group: 'build']
json:
    {{ sphinxbuild }} -b json {{ allsphinxopts }} {{ builddir }}/json
    @echo ""
    @echo "Build finished; now you can process the JSON files."

# Clean build directory
[group: 'misc']
[confirm]
clean:
    rm -rf {{ builddir }}/*

# Check all external links
[group: 'validate']
linkcheck:
    {{ sphinxbuild }} -b linkcheck {{ allsphinxopts }} {{ builddir }}/linkcheck
    @echo ""
    @echo "Link check complete; look for any errors in the above output or in {{ builddir }}/linkcheck/output.txt."

# Run doctests embedded in documentation
[group: 'validate']
doctest:
    {{ sphinxbuild }} -b doctest {{ allsphinxopts }} {{ builddir }}/doctest
    @echo "Testing of doctests in the sources finished, look at the results in {{ builddir }}/doctest/output.txt."

# Check build from scratch
[group: 'validate']
checkbuild:
    rm -rf {{ builddir }}
    {{ sphinxbuild }} -n -q ./ {{ builddir }}

# Watch files and rebuild on change
[group: 'dev']
watch:
    #!/usr/bin/env bash
    set -euo pipefail
    if command -v entr > /dev/null; then
        ${{ watch_files }} | entr -c just html
    else
        just html
    fi

# Serve documentation via Python http.server
[group: 'dev']
serve:
    @echo '=============================================================='
    @echo ''
    @echo 'docs server running at http://localhost:{{ http_port }}/'
    @echo ''
    @echo '=============================================================='
    python -m http.server {{ http_port }} --directory {{ builddir }}/html

# Watch and serve simultaneously
[group: 'dev']
dev:
    #!/usr/bin/env bash
    set -euo pipefail
    just watch &
    just serve

# Start sphinx-autobuild server
[group: 'dev']
start:
    uv run sphinx-autobuild -b dirhtml "{{ sourcedir }}" "{{ builddir }}" {{ sphinxopts }} --port {{ http_port }}

# Design mode: watch static files and disable incremental builds
[group: 'dev']
design:
    uv run sphinx-autobuild -b dirhtml "{{ sourcedir }}" "{{ builddir }}" {{ sphinxopts }} --port {{ http_port }} --watch "." -a
