message("\n${BoldGreen}Now configuring src/doc/javascript for ${PROJECT_NAME}${ColourReset}\n")

################ JavaScript reference text handling #################
################ JavaScript reference text handling #################

find_package(Python3 REQUIRED)

############ List all the files that contain the class JS reference tag
# that is like so:
# /* BEGIN CLASS JS REFERENCE
# * namespace: pappso
# * class name: MzIntegrationParams
# */
# The jsExtract.py program lists the files to stdout and that output
# is redirected to OUTPUT_FILE.

set(TOP_JS_REF_DIR ${CMAKE_CURRENT_SOURCE_DIR}/js_reference)
set(SCRIPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/scripts)

set(CORE_SRC_DIR ${CMAKE_SOURCE_DIR}/source/XpertMassCore)
set(GUI_SRC_DIR ${CMAKE_SOURCE_DIR}/source/XpertMassGui)

set(CORE_OUTPUT_DIR ${TOP_JS_REF_DIR}/Core)
set(GUI_OUTPUT_DIR ${TOP_JS_REF_DIR}/Gui)

set(CORE_CLASS_JS_REF_FILE_LIST ${CORE_OUTPUT_DIR}/class_js_reference_file_list.txt)
set(CORE_CLASS_JS_REF_TEXT ${CORE_OUTPUT_DIR}/class_js_reference_text.txt)

set(GUI_CLASS_JS_REF_FILE_LIST ${GUI_OUTPUT_DIR}/class_js_reference_file_list.txt)
set(GUI_CLASS_JS_REF_TEXT ${GUI_OUTPUT_DIR}/class_js_reference_text.txt)

# When run (see add_custom_target(list_class_js_ref_files below)
# this command will list all the header files in the Core lib that have
# a class JS reference text in them.
add_custom_command(
    OUTPUT ${CORE_CLASS_JS_REF_FILE_LIST}
    COMMAND ${Python3_EXECUTABLE} ${SCRIPTS_DIR}/jsExtract.py
        --list-files class
        -d ${CORE_SRC_DIR}
        -t "  " > ${CORE_CLASS_JS_REF_FILE_LIST}
    COMMENT "List Core files containing class JS reference text"
    VERBATIM
)

# When run (see add_custom_target(list_class_js_ref_files below)
# this command will list all the header files in the Widget lib that have
# a class JS reference text in them.
add_custom_command(
    OUTPUT ${GUI_CLASS_JS_REF_FILE_LIST}
    COMMAND ${Python3_EXECUTABLE} ${SCRIPTS_DIR}/jsExtract.py
        --list-files class
        -d ${GUI_SRC_DIR}
        -t "  " > ${GUI_CLASS_JS_REF_FILE_LIST}
    COMMENT "List Widget files containing class JS reference text"
    VERBATIM
)

# Runs the commands above that create the files listed here as DEPENDS
add_custom_target(list_class_js_ref_files
    DEPENDS ${CORE_CLASS_JS_REF_FILE_LIST} ${GUI_CLASS_JS_REF_FILE_LIST}
    COMMENT "Listing Core and Widget files containing class JS reference text"
)

# When run (see add_custom_target(extract_class_js_ref_text below)
# this command will extract from the files listed in the lists generated above
# all the class JS reference text.
#
# Core
add_custom_command(
    OUTPUT ${CORE_CLASS_JS_REF_TEXT}
    DEPENDS ${CORE_CLASS_JS_REF_FILE_LIST}
    COMMAND ${Python3_EXECUTABLE} ${SCRIPTS_DIR}/jsExtract.py
      --tab "  "
      --extract class
      --listfile ${CORE_CLASS_JS_REF_FILE_LIST} > ${CORE_CLASS_JS_REF_TEXT}
    COMMENT "Extract class JS reference text from Core header files"
    VERBATIM
)
#
# Widget
add_custom_command(
    OUTPUT ${GUI_CLASS_JS_REF_TEXT}
    DEPENDS ${GUI_CLASS_JS_REF_FILE_LIST}
    COMMAND ${Python3_EXECUTABLE} ${SCRIPTS_DIR}/jsExtract.py
      --tab "  "
      --extract class
      --listfile ${GUI_CLASS_JS_REF_FILE_LIST} > ${GUI_CLASS_JS_REF_TEXT}
    COMMENT "Extract class JS reference text from Widget header files"
    VERBATIM
)

# Runs the commands above that create the files listed here as DEPENDS
add_custom_target(extract_class_js_ref_text
    DEPENDS ${CORE_CLASS_JS_REF_TEXT} ${GUI_CLASS_JS_REF_TEXT}
    COMMENT "Extracting class JavaScript reference text into ${CORE_CLASS_JS_REF_TEXT} and ${GUI_CLASS_JS_REF_TEXT}"
)

message("\n${BoldGreen}Done configuring src/doc/javascript for ${PROJECT_NAME}${ColourReset}\n")
