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

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

########################## mzml2mzcbor ##########################
add_executable(
    mzml2mzcbor
    mzml2mzcborcli.cpp
)

target_include_directories(
    mzml2mzcbor
    PUBLIC ${PappsoMSpp_INCLUDE_DIRS}
)

target_link_libraries(
    mzml2mzcbor Qt6::Core Qt6::Qml Core
)

add_dependencies(mzml2mzcbor Core)


########################## mzcbor2mzml ##########################

add_executable(
    mzcbor2mzml
    mzcbor2mzmlcli.cpp
)

target_include_directories(
    mzcbor2mzml
    PUBLIC ${PappsoMSpp_INCLUDE_DIRS}
)

target_link_libraries(
    mzcbor2mzml Qt6::Core Qt6::Qml Core
)

add_dependencies(mzcbor2mzml Core)

########################## sage2psmcborcli ##########################

add_executable(
    sage2psmcbor
    sage2psmcborcli.cpp
)

target_include_directories(
    sage2psmcbor
    PUBLIC ${PappsoMSpp_INCLUDE_DIRS}
)

target_link_libraries(
    sage2psmcbor Qt6::Core Qt6::Qml Core
)

########################## mzcbor-spectrum2json ##########################

add_executable(
    mzcbor-spectrum2json
    mzcborspectrum2jsoncli.cpp
)

target_include_directories(
    mzcbor-spectrum2json
    PUBLIC ${PappsoMSpp_INCLUDE_DIRS}
)

target_link_libraries(
    mzcbor-spectrum2json Qt6::Core Qt6::Qml Core
)

add_dependencies(mzcbor-spectrum2json Core)


########################## Install CLI tools ##########################
#
install(
    PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/mzml2mzcbor
    DESTINATION bin
)


install(
    PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/mzcbor2mzml
    DESTINATION bin
)


install(
    PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/sage2psmcbor
    DESTINATION bin
)
install(
    PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/mzcbor-spectrum2json
    DESTINATION bin
)
########################## The man page ##########################

if(UNIX AND NOT APPLE)

    find_program(rst2man_program rst2man)
    if(NOT rst2man_program)
        message(FATAL_ERROR "rst2man not found!")
    endif()

    set(mzml2mzcbor_manpage ${CMAKE_CURRENT_BINARY_DIR}/mzml2mzcbor.1)

    add_custom_command(
        OUTPUT ${mzml2mzcbor_manpage}
        COMMAND ${rst2man_program} ${CMAKE_CURRENT_SOURCE_DIR}/mzml2mzcbor.1.rst ${mzml2mzcbor_manpage}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/mzml2mzcbor.1.rst
        COMMENT "Generating man page mzml2mzcbor.1 from mzml2mzcbor.rst"
        VERBATIM
    )


    set(mzcbor2mzml_manpage ${CMAKE_CURRENT_BINARY_DIR}/mzcbor2mzml.1)
    add_custom_command(
        OUTPUT ${mzcbor2mzml_manpage}
        COMMAND ${rst2man_program} ${CMAKE_CURRENT_SOURCE_DIR}/mzcbor2mzml.1.rst ${mzcbor2mzml_manpage}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/mzcbor2mzml.1.rst
        COMMENT "Generating man page mzcbor2mzml.1 from mzcbor2mzml.rst"
        VERBATIM
    )

    set(sage2psmcbor_manpage ${CMAKE_CURRENT_BINARY_DIR}/sage2psmcbor.1)
    add_custom_command(
        OUTPUT ${sage2psmcbor_manpage}
        COMMAND ${rst2man_program} ${CMAKE_CURRENT_SOURCE_DIR}/sage2psmcbor.1.rst ${sage2psmcbor_manpage}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/sage2psmcbor.1.rst
        COMMENT "Generating man page sage2psmcbor.1 from sage2psmcbor.rst"
        VERBATIM
    )
    ########################## mzcbor-spectrum2json generate man page ##########################
    set(mzcbor-spectrum2json_manpage ${CMAKE_CURRENT_BINARY_DIR}/mzcbor-spectrum2json.1)
    add_custom_command(
        OUTPUT ${mzcbor-spectrum2json_manpage}
        COMMAND ${rst2man_program} ${CMAKE_CURRENT_SOURCE_DIR}/mzcbor-spectrum2json.1.rst ${mzcbor-spectrum2json_manpage}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/mzcbor-spectrum2json.1.rst
        COMMENT "Generating man page v.1 from mzcbor-spectrum2json.rst"
        VERBATIM
    )

    # define manpage target
    add_custom_target(manpage ALL DEPENDS ${mzml2mzcbor_manpage} ${mzcbor2mzml_manpage} ${sage2psmcbor_manpage} ${mzcbor-spectrum2json_manpage} )

    # Install the generated file
    install(
        FILES ${mzml2mzcbor_manpage} ${mzcbor2mzml_manpage} ${sage2psmcbor_manpage} ${mzcbor-spectrum2json_manpage}
        DESTINATION share/man/man1
    )

endif()

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