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 "${USER_MANUAL_SRC_DIR}/${CMAKE_PROJECT_NAME}-doc.pdf")
set(OUTPUT_HTML "${USER_MANUAL_SRC_DIR}/${CMAKE_PROJECT_NAME}-doc.html")
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 ${CMAKE_PROJECT_NAME}
    VERBATIM
)

add_custom_command(
    OUTPUT "${OUTPUT_HTML}"
    COMMAND "${TYPST_EXECUTABLE}" compile --root "${CMAKE_CURRENT_SOURCE_DIR}" --deps "${DEPSFILE}" --deps-format "make" --features html --format html
            "${MAIN_TYP}" ${CMAKE_PROJECT_NAME}-doc.html
    #COMMAND "mv" "${USER_MANUAL_SRC_DIR}/main.html" "${OUTPUT_HTML}"
    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 ${CMAKE_PROJECT_NAME}
    VERBATIM
)

add_custom_target(user_manual_pdf ALL DEPENDS "${OUTPUT_PDF}" ${CMAKE_PROJECT_NAME})
add_custom_target(user_manual_html ALL DEPENDS "${OUTPUT_HTML}" ${CMAKE_PROJECT_NAME})
add_custom_target(user_manual ALL DEPENDS user_manual_pdf user_manual_html)

# 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_pdf
        POST_BUILD
        COMMAND rsync -avpP --delete ${OUTPUT_PDF} ${DOC_STORE_PATH}
        WORKING_DIRECTORY ${USER_MANUAL_SRC_DIR}
        COMMENT "Copy the documentation to ${DOC_STORE_PATH}"
    )

    add_custom_command(
        TARGET user_manual_html
        POST_BUILD
        COMMAND rsync -avpP --delete ${OUTPUT_HTML} html-output.css ${DOC_STORE_PATH}/html/user-manual
        WORKING_DIRECTORY ${USER_MANUAL_SRC_DIR}
        COMMENT "Copy the documentation to ${DOC_STORE_PATH}"
    )

endif()

install(
    FILES ${OUTPUT_PDF}
    DESTINATION ${MINEXPERT_INSTALL_DOC_DIR}
)

install(
    FILES ${OUTPUT_HTML} html-output.css
    DESTINATION ${MINEXPERT_INSTALL_DOC_DIR}/html
)

# Create a symbolic link MassXpert3-doc.html -> index.html in the install directory.
install(CODE "
  message(STATUS \"Creating symlink:
    \$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/share/doc/massxpert3/html/${CMAKE_PROJECT_NAME}-doc.html
      -> \$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/share/doc/massxpert3/html/index.html\")
  execute_process(COMMAND \${CMAKE_COMMAND} -E create_symlink
    \"\$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/share/doc/massxpert3/html/${CMAKE_PROJECT_NAME}-doc.html\"
    \"\$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/share/doc/massxpert3/html/index.html\"
    RESULT_VARIABLE _result
  )
  if(NOT _result EQUAL 0)
    message(FATAL_ERROR \"Failed to create symlink\")
  endif()
")

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