@echo off
rem ---------------------------------------------------------------------------
rem build_docs.bat — Local documentation build helper for ut-arr (Windows)
rem
rem Usage:
rem   build_docs.bat              Build HTML (default)
rem   build_docs.bat html         Build HTML
rem   build_docs.bat pdf          Build PDF via rst2pdf
rem   build_docs.bat latexpdf     Build PDF via LaTeX
rem   build_docs.bat epub         Build EPUB
rem   build_docs.bat docx         Build DOCX via pandoc
rem   build_docs.bat text         Build plain text
rem   build_docs.bat man          Build man pages
rem   build_docs.bat all          Build all formats
rem   build_docs.bat clean        Remove build artefacts
rem   build_docs.bat install-deps Install build dependencies
rem   build_docs.bat formats      List valid build formats
rem ---------------------------------------------------------------------------

setlocal enabledelayedexpansion

set "SCRIPT_DIR=%~dp0"
set "DOCS_DIR=%SCRIPT_DIR%..\docs"
set "BUILD_DIR=%DOCS_DIR%\_build"

set "TARGET=%~1"
if "%TARGET%"=="" set "TARGET=html"

goto :%TARGET% 2>nul || goto :unknown

rem ---- helpers --------------------------------------------------------------

:ensure_deps
    python -c "import sphinx" >nul 2>&1
    if errorlevel 1 (
        echo [INFO]  Installing build dependencies ...
        python -m pip install --user --quiet --upgrade pip
        python -m pip install --user --quiet -r "%DOCS_DIR%\requirements.txt"
    )
    exit /b 0

:check_tool
    where %1 >nul 2>&1
    if errorlevel 1 (
        echo [ERROR] '%1' is not installed.  %2
        exit /b 1
    )
    exit /b 0

rem ---- build targets --------------------------------------------------------

:html
    call :ensure_deps
    echo [INFO]  Building HTML ...
    sphinx-build -M html "%DOCS_DIR%" "%BUILD_DIR%" -W --keep-going
    echo [INFO]  HTML ready: %BUILD_DIR%\html\index.html
    goto :eof

:pdf
    call :ensure_deps
    call :check_tool rst2pdf "Install rst2pdf: pip install rst2pdf"
    if errorlevel 1 goto :eof
    echo [INFO]  Building PDF (rst2pdf) ...
    sphinx-build -M pdf "%DOCS_DIR%" "%BUILD_DIR%"
    echo [INFO]  PDF ready: %BUILD_DIR%\pdf\
    goto :eof

:latexpdf
    call :ensure_deps
    call :check_tool pdflatex "Install a TeX distribution (texlive or miktex)."
    if errorlevel 1 goto :eof
    echo [INFO]  Building PDF (LaTeX) ...
    sphinx-build -M latexpdf "%DOCS_DIR%" "%BUILD_DIR%"
    echo [INFO]  PDF ready: %BUILD_DIR%\latex\ut-arr.pdf
    goto :eof

:epub
    call :ensure_deps
    echo [INFO]  Building EPUB ...
    sphinx-build -M epub "%DOCS_DIR%" "%BUILD_DIR%"
    echo [INFO]  EPUB ready: %BUILD_DIR%\epub\ut-arr.epub
    goto :eof

:docx
    call :ensure_deps
    call :check_tool pandoc "Install pandoc: https://pandoc.org/installing.html"
    if errorlevel 1 goto :eof
    echo [INFO]  Building single-page HTML for DOCX conversion ...
    sphinx-build -M singlehtml "%DOCS_DIR%" "%BUILD_DIR%"
    echo [INFO]  Converting to DOCX via pandoc ...
    pandoc "%BUILD_DIR%\singlehtml\index.html" --from html --to docx -o "%BUILD_DIR%\ut-arr.docx" --metadata title="ut-arr Documentation"
    echo [INFO]  DOCX ready: %BUILD_DIR%\ut-arr.docx
    goto :eof

:text
    call :ensure_deps
    echo [INFO]  Building plain text ...
    sphinx-build -M text "%DOCS_DIR%" "%BUILD_DIR%"
    echo [INFO]  Text ready: %BUILD_DIR%\text\
    goto :eof

:man
    call :ensure_deps
    echo [INFO]  Building man pages ...
    sphinx-build -M man "%DOCS_DIR%" "%BUILD_DIR%"
    echo [INFO]  Man pages ready: %BUILD_DIR%\man\
    goto :eof

:all
    call :html
    call :pdf
    call :latexpdf
    call :epub
    call :docx
    call :man
    echo [INFO]  All formats built in %BUILD_DIR%\
    goto :eof

:clean
    echo [INFO]  Cleaning %BUILD_DIR% ...
    if exist "%BUILD_DIR%" rmdir /s /q "%BUILD_DIR%"
    echo [INFO]  Clean complete.
    goto :eof

:install-deps
    echo [INFO]  Installing / upgrading build dependencies ...
    python -m pip install --user --upgrade pip
    python -m pip install --user --upgrade -r "%DOCS_DIR%\requirements.txt"
    echo [INFO]  Dependencies installed.
    goto :eof

:formats
    echo Valid build formats:
    echo.
    echo   html          HTML documentation
    echo   pdf           PDF via rst2pdf
    echo   latexpdf      PDF via LaTeX
    echo   epub          EPUB e-book
    echo   docx          Word document (requires pandoc)
    echo   text          Plain text
    echo   man           Man pages
    echo.
    echo Multiple formats can be built by running the script once per format,
    echo or use 'all' to build all formats at once.
    goto :eof

:-h
:--help
:help
    echo Usage: %~nx0 {html^|pdf^|latexpdf^|epub^|docx^|text^|man^|all^|clean^|install-deps^|formats}
    echo.
    echo Format targets:
    echo   html          Build HTML documentation (default)
    echo   pdf           Build PDF via rst2pdf
    echo   latexpdf      Build PDF via LaTeX
    echo   epub          Build EPUB e-book
    echo   docx          Build DOCX via pandoc
    echo   text          Build plain text
    echo   man           Build man pages
    echo.
    echo Combo targets:
    echo   all           Build all formats (html, pdf, latexpdf, epub, docx, man)
    echo.
    echo Utility targets:
    echo   clean         Remove all build artefacts
    echo   install-deps  Install Python dependencies (pip --user)
    echo   formats       List valid build formats
    goto :eof

:unknown
    echo [ERROR] Unknown target: %TARGET%
    echo Run '%~nx0 --help' for usage.
    exit /b 1
