message("\n${BoldGreen}Configuring subdirectory doc/user-manual${ColourReset}\n")

message("")
message("\n${BoldYellow}Configuring the user manual compilation${ColourReset}\n")
message("")

# The file driving the user manual build is DC-user-manual.
# This file contains a config bit that points to the directory
# that contains de fop.xconf configuration file needed by
# the FOP utility that crafts the user manual pdf file.

cmake_minimum_required(VERSION 3.12) # Required for CONFIGURE_DEPENDS

# 1. Find DAPS
find_program(DAPS_EXECUTABLE daps)
if(NOT DAPS_EXECUTABLE)
    message(FATAL_ERROR "DAPS executable not found. Please install DAPS and ensure it is in your PATH.")
endif()

set(FOP_XCONF_DIR ${CMAKE_SOURCE_DIR}/doc/user-manual)
configure_file(${CMAKE_SOURCE_DIR}/CMakeStuff/DC-user-manual.cmake.in ${CMAKE_CURRENT_SOURCE_DIR}/DC-user-manual @ONLY)

# Define paths
set(USER_MANUAL_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(USER_MANUAL_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(DAPS_CONFIG ${USER_MANUAL_SRC_DIR}/DC-user-manual)
set(DOC_STORE_PATH "${HOME_DEVEL_DIR}/msxpertsuite-store/docs/${LOWCASE_PROJECT_NAME_NO_VER}")

# 2. Granular File-Level Dependency Tracking
# CONFIGURE_DEPENDS ensures CMake re-evaluates the glob if files are added/removed
file(GLOB_RECURSE XML_DEPS CONFIGURE_DEPENDS "${USER_MANUAL_SRC_DIR}/xml/*.xml")
file(GLOB_RECURSE XML_ENTITIES_DEPS CONFIGURE_DEPENDS "${USER_MANUAL_SRC_DIR}/xml/*.ent")
file(GLOB_RECURSE PNG_DEPS CONFIGURE_DEPENDS "${USER_MANUAL_SRC_DIR}/images/src/png/*.png")
file(GLOB_RECURSE SVG_DEPS CONFIGURE_DEPENDS "${USER_MANUAL_SRC_DIR}/images/src/svg/*.svg")

# Combine all source dependencies
set(ALL_DOC_DEPS
    ${XML_DEPS}
    ${XML_ENTITIES_DEPS}
    ${PNG_DEPS}
    ${SVG_DEPS}
    ${DAPS_CONFIG}
)

# Original makefile commands:
# For HTML:
# daps --force --builddir=${buildDir} -d ${topSrcDir}/DC-user-manual --verbosity=3 html --static
# For PDF:
# daps --force --builddir=${buildDir} -d ${topSrcDir}/DC-user-manual --verbosity=3 pdf
# mv -v $(pdfFilePathName) ${pdfFilePath}/${project}-doc.pdf

# 3. Out-of-Source Directories for DAPS
# Keeping cache and results in the build tree preserves DAPS's internal caches
set(DAPS_CACHE_DIR ${USER_MANUAL_BIN_DIR}/.daps_cache)

# Common DAPS arguments
set(DAPS_COMMON_ARGS -d ${DAPS_CONFIG} --builddir=${USER_MANUAL_BIN_DIR} --verbosity=3)

# ---------------------------------------------------------
# HTML TARGET
# ---------------------------------------------------------
set(HTML_STAMP ${USER_MANUAL_BIN_DIR}/html.stamp)

add_custom_command(
    OUTPUT ${HTML_STAMP}
    COMMAND ${CMAKE_COMMAND} -E env DAPS_CACHE=${DAPS_CACHE_DIR} ${DAPS_EXECUTABLE} ${DAPS_COMMON_ARGS} html
    COMMAND ${CMAKE_COMMAND} -E touch ${HTML_STAMP}
    DEPENDS ${ALL_DOC_DEPS}
    WORKING_DIRECTORY ${USER_MANUAL_SRC_DIR}
    COMMENT "Rendering HTML manual with DAPS..."
    VERBATIM
)
add_custom_target(user_manual_html DEPENDS ${HTML_STAMP})

# ---------------------------------------------------------
# PDF TARGET
# ---------------------------------------------------------
# We use a "stamp" file because DAPS generates multiple files/directories.
# CMake's add_custom_command works best with a single output file.
set(PDF_STAMP ${USER_MANUAL_BIN_DIR}/pdf.stamp)

add_custom_command(
    OUTPUT ${PDF_STAMP}
    # Use 'cmake -E env' to inject the DAPS_CACHE environment variable
    COMMAND ${CMAKE_COMMAND} -E env DAPS_CACHE=${DAPS_CACHE_DIR} ${DAPS_EXECUTABLE} ${DAPS_COMMON_ARGS} pdf
    COMMAND ${CMAKE_COMMAND} -E copy ${USER_MANUAL_BIN_DIR}/user-manual/user-manual_color_en.pdf
            ${USER_MANUAL_BIN_DIR}/user-manual/${TARGET}-doc.pdf
    COMMAND ${CMAKE_COMMAND} -E touch ${PDF_STAMP}
    DEPENDS ${ALL_DOC_DEPS}
    WORKING_DIRECTORY ${USER_MANUAL_SRC_DIR}
    COMMENT "Rendering PDF manual with DAPS..."
    VERBATIM
)
add_custom_target(user_manual_pdf DEPENDS ${PDF_STAMP})

if(DEFINED ENV{DEB_BUILD_OPTIONS})

    message("Building debian package: not synchronizing documentation to doc store.")

else()

    # We want to store a local copy of the documentation so that we do not mess around
    # with build in unix that destroy the documentation if the build user manual switch
    # is not set and then MXE fails to find the doc pdf file.
    add_custom_command(
        TARGET user_manual_html
        user_manual_pdf POST_BUILD
        COMMAND rsync -avpP --delete ${USER_MANUAL_BIN_DIR}/user-manual/${TARGET}-doc.pdf
                ${USER_MANUAL_BIN_DIR}/user-manual/html ${DOC_STORE_PATH}
        WORKING_DIRECTORY ${USER_MANUAL_SRC_DIR}
        COMMENT "Copy the documentation to ${DOC_STORE_PATH}"
    )

endif()

# HTML can be considered the default.
add_custom_target(user_manual ALL DEPENDS ${HTML_STAMP})

# Build both the HTML and PDF format artifacts.
add_custom_target(
    user_manual_all
    DEPENDS ${HTML_STAMP} ${PDF_STAMP}
    COMMENT "Building both HTML and PDF user manuals..."
)

install(
    FILES ${CMAKE_CURRENT_BINARY_DIR}/user-manual/${TARGET}-doc.pdf
    DESTINATION ${PROJECT_INSTALL_DOC_DIR}
)

install(
    DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/user-manual/html/user-manual/
    DESTINATION ${PROJECT_INSTALL_DOC_DIR}/html
)

message("")
message(STATUS "${BoldGreen}Finished configuring the ${CMAKE_PROJECT_NAME} user manual.${ColourReset}")
message("")

message("\n${BoldGreen}Done configuring subdirectory doc/user-manual${ColourReset}\n")
