# Documentation build rules.
#
# Two pieces live here:
#   1. The man page (kat5200.1) -- gzipped and installed; part of `all`.
#   2. The user guide (doc/guide) -- an MkDocs + Material site. Building it
#      needs Python tooling (mkdocs, mkdocs-material, pymdown-extensions) that
#      is not a hard build dependency of the emulator, so the guide targets are
#      developer-convenience targets and are NOT part of `all`. They mirror the
#      flatpak-* targets in the top-level CMakeLists.txt.
#
# (The old texinfo manual -- kat5200.texi / makeinfo -- has been retired in
# favor of the MkDocs guide.)

# --- Man page --------------------------------------------------------------

find_program(GZIP_EXECUTABLE gzip)

if(GZIP_EXECUTABLE)
    add_custom_command(
        OUTPUT kat5200.1.gz
        COMMAND ${GZIP_EXECUTABLE} -c9 ${CMAKE_CURRENT_SOURCE_DIR}/kat5200.1 > kat5200.1.gz
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/kat5200.1
        COMMENT "Compressing man page kat5200.1"
        VERBATIM)
    add_custom_target(man ALL DEPENDS kat5200.1.gz)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kat5200.1.gz
            DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
else()
    message(STATUS "gzip not found; skipping compressed man page target")
endif()

# --- User guide (MkDocs + Material) ----------------------------------------
#
#   make guide        Build the HTML site (doc/guide/site/)
#   make guide-serve  Live-preview server on http://127.0.0.1:8000
#   make guide-pdf    Single-file PDF (doc/guide/site/pdf/kat5200-user-guide.pdf)
#
# These shell out to the tooling in doc/guide/ and run with that directory as
# the working dir so mkdocs picks up mkdocs.yml and writes site/ in-tree, as
# documented in doc/guide/README.md.

set(_guide_dir ${CMAKE_CURRENT_SOURCE_DIR}/guide)
find_program(MKDOCS_EXECUTABLE mkdocs)

if(MKDOCS_EXECUTABLE)
    add_custom_target(guide
        COMMAND ${MKDOCS_EXECUTABLE} build --strict
        WORKING_DIRECTORY ${_guide_dir} VERBATIM
        COMMENT "Building the kat5200 user guide (doc/guide/site)")

    add_custom_target(guide-serve
        COMMAND ${MKDOCS_EXECUTABLE} serve
        WORKING_DIRECTORY ${_guide_dir} VERBATIM USES_TERMINAL
        COMMENT "Serving the kat5200 user guide on http://127.0.0.1:8000")
else()
    set(_guide_msg [=[mkdocs not found on PATH. Install it (e.g. on Arch: pacman -S mkdocs mkdocs-material python-pymdown-extensions), then re-run.]=])
    add_custom_target(guide
        COMMAND ${CMAKE_COMMAND} -E echo ${_guide_msg}
        COMMAND ${CMAKE_COMMAND} -E false VERBATIM)
    add_custom_target(guide-serve
        COMMAND ${CMAKE_COMMAND} -E echo ${_guide_msg}
        COMMAND ${CMAKE_COMMAND} -E false VERBATIM)
endif()

# PDF export uses the mkdocs-with-pdf plugin, which lives in a local venv
# (see doc/guide/README.md). build-pdf.sh wraps the venv invocation.
add_custom_target(guide-pdf
    COMMAND ${_guide_dir}/build-pdf.sh
    WORKING_DIRECTORY ${_guide_dir} VERBATIM
    COMMENT "Building the kat5200 user guide PDF")
