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

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

cmake_minimum_required(VERSION 3.12) # Required for CONFIGURE_DEPENDS

# 1. Find TYPST
find_program(TYPST_EXECUTABLE typst)
if(NOT TYPST_EXECUTABLE)

    message(FATAL_ERROR "TYPST executable not found. Please install TYPST and ensure it is in your PATH.")

else()
    message("Found typst executable at:" ${TYPST_EXECUTABLE})

endif()

# Define paths
set(USER_MANUAL_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(USER_MANUAL_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(DOC_STORE_PATH "${HOME_DEVEL_DIR}/msxpertsuite-store/docs/${LOWCASE_PROJECT_NAME_NO_VER}")
set(MAIN_TYP "${CMAKE_CURRENT_SOURCE_DIR}/main.typ")
set(OUTPUT_PDF "${CMAKE_CURRENT_SOURCE_DIR}/${CMAKE_PROJECT_NAME}-doc.pdf")
set(DEPSFILE "${CMAKE_CURRENT_BINARY_DIR}/user-manual-deps")

file(
    GLOB_RECURSE
    TYP_DEPS
    CONFIGURE_DEPENDS
    "${USER_MANUAL_SRC_DIR}/*.typ"
    "${USER_MANUAL_SRC_DIR}/contents/*.typ"
)
file(GLOB_RECURSE JSON_DEPS CONFIGURE_DEPENDS "${USER_MANUAL_SRC_DIR}/*.json")
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 ${TYP_DEPS} ${JSON_DEPS} ${PNG_DEPS} ${SVG_DEPS})

# message("ALL_DOC_DEPS:" ${ALL_DOC_DEPS})

# Original makefile commands:
# For HTML:
# /all-inst-packages/typst/typst-x86_64-unknown-linux-musl/typst compile main.typ --features html --format html
# For PDF:
# /all-inst-packages/typst/typst-x86_64-unknown-linux-musl/typst compile main.typ --format pdf

add_custom_command(
    OUTPUT "${OUTPUT_PDF}"
    COMMAND "${TYPST_EXECUTABLE}" compile --root "${CMAKE_CURRENT_SOURCE_DIR}" --deps "${DEPSFILE}" --deps-format "make" "${MAIN_TYP}"
            "${OUTPUT_PDF}"
    DEPFILE "${DEPSFILE}"
    WORKING_DIRECTORY "${USER_MANUAL_SRC_DIR}"
    MAIN_DEPENDENCY "${MAIN_TYP}"
    # Rebuild the user manual whenever something changes that triggers
    # rebuild of the program itself.
    DEPENDS MassXpert3
    VERBATIM
)

add_custom_target(user_manual ALL DEPENDS "${OUTPUT_PDF}")

# This has become superfluous right now because we do no more build the
# documentation when building the Debian package. But we keep this
# for the time to come when Typst will be available in Debian.
if(DEFINED ENV{DEB_BUILD_OPTIONS})

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

else()

    message("Not building debian package: synchronizing documentation to doc store.")
    # 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
        POST_BUILD
        COMMAND rsync -avpP --delete ${USER_MANUAL_SRC_DIR}/${CMAKE_PROJECT_NAME}-doc.pdf ${DOC_STORE_PATH}
        WORKING_DIRECTORY ${USER_MANUAL_SRC_DIR}
        COMMENT "Copy the documentation to ${DOC_STORE_PATH}"
    )

endif()

install(
    FILES ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}-doc.pdf
    DESTINATION ${MASSXPERT_INSTALL_DOC_DIR}
)

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