StrictCompile()

add_executable(bongocollector
	collector.c
	external_store.c
	external_transport.c
	pending-count.c
)

target_link_libraries(bongocollector
    bongoxpl
    bongoconnio
    bongoutil
    bongostreamio
    bongomsgapi
    bongojson
    bongocollectoraccounts
    CURL::libcurl
    SQLite3::SQLite3
)

install(TARGETS bongocollector DESTINATION ${SBIN_INSTALL_DIR})

if(BUILD_TESTING)
    add_executable(collector-transport-test
        tests/external-transport-test.c
        external_transport.c
    )
    target_include_directories(collector-transport-test PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
    )
    target_link_libraries(collector-transport-test CURL::libcurl)
    add_test(NAME collector-external-transport COMMAND collector-transport-test)

    add_executable(collector-pending-count-test
        tests/pending-count-test.c
        pending-count.c
    )
    target_include_directories(collector-pending-count-test PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
    )
    add_test(NAME collector-pending-count COMMAND collector-pending-count-test)

    add_executable(collector-remote-transport-probe
        tests/vtestmail-transport-test.c
        external_transport.c)
    target_include_directories(collector-remote-transport-probe PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR})
    target_link_libraries(collector-remote-transport-probe CURL::libcurl)

    add_test(NAME collector-cyrus-fixture-messages
        COMMAND "${Python3_EXECUTABLE}"
            "${CMAKE_SOURCE_DIR}/contrib/testing/tests/cyrus-fixture-message-test.py")
    set_tests_properties(collector-cyrus-fixture-messages PROPERTIES
        LABELS "collector;fixture")

    set(BONGO_VTESTMAIL_JAR "" CACHE FILEPATH
        "Path to vtestmail-core 1.0.0 for the optional Collector integration test")
    if(BONGO_VTESTMAIL_JAR)
        if(NOT EXISTS "${BONGO_VTESTMAIL_JAR}")
            message(FATAL_ERROR
                "BONGO_VTESTMAIL_JAR does not exist: ${BONGO_VTESTMAIL_JAR}")
        endif()

        file(SHA256 "${BONGO_VTESTMAIL_JAR}" BONGO_VTESTMAIL_SHA256)
        set(BONGO_VTESTMAIL_EXPECTED_SHA256
            "9e330f028e646265e1ef81f9687ed21a2e0a2f3b69e04099b8e8a55f06b9d7f4")
        if(NOT BONGO_VTESTMAIL_SHA256 STREQUAL
                BONGO_VTESTMAIL_EXPECTED_SHA256)
            message(FATAL_ERROR
                "BONGO_VTESTMAIL_JAR is not the pinned vtestmail-core 1.0.0 artifact\n"
                "Expected SHA-256: ${BONGO_VTESTMAIL_EXPECTED_SHA256}\n"
                "Actual SHA-256:   ${BONGO_VTESTMAIL_SHA256}")
        endif()

        find_package(Java 11 COMPONENTS Runtime Development REQUIRED)
        set(BONGO_VTESTMAIL_CLASSES
            "${CMAKE_CURRENT_BINARY_DIR}/vtestmail-classes")
        set(BONGO_VTESTMAIL_FIXTURE_CLASS
            "${BONGO_VTESTMAIL_CLASSES}/net/markwalder/vtestmail/imap/BongoVtestmailFixture.class")

        add_custom_command(
            OUTPUT "${BONGO_VTESTMAIL_FIXTURE_CLASS}"
            COMMAND "${CMAKE_COMMAND}" -E make_directory
                    "${BONGO_VTESTMAIL_CLASSES}"
            COMMAND "${Java_JAVAC_EXECUTABLE}"
                    -encoding UTF-8
                    -classpath "${BONGO_VTESTMAIL_JAR}"
                    -d "${BONGO_VTESTMAIL_CLASSES}"
                    "${CMAKE_CURRENT_SOURCE_DIR}/tests/BongoVtestmailFixture.java"
            DEPENDS
                "${CMAKE_CURRENT_SOURCE_DIR}/tests/BongoVtestmailFixture.java"
                "${BONGO_VTESTMAIL_JAR}"
            VERBATIM)
        add_custom_target(collector-vtestmail-fixture-classes
            DEPENDS "${BONGO_VTESTMAIL_FIXTURE_CLASS}")

        add_dependencies(collector-remote-transport-probe
            collector-vtestmail-fixture-classes)

        add_test(NAME collector-vtestmail-integration
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tests/vtestmail-integration.py"
                --java "${Java_JAVA_EXECUTABLE}"
                --classpath "${BONGO_VTESTMAIL_CLASSES}:${BONGO_VTESTMAIL_JAR}"
                --probe "$<TARGET_FILE:collector-remote-transport-probe>")
        set_tests_properties(collector-vtestmail-integration PROPERTIES
            LABELS "collector;integration"
            TIMEOUT 60)
    endif()
endif()
