# plumb — plan9 plumbing client (9P2000 over the namespace socket).
#
# plumb_lib carries the pure codec + resolver and is unit tested without any
# GUI/9P I/O. The 9P client (ninep) and the Plumber facade add Qt Network and
# a reader thread; they talk to a live plumber and so are exercised by a live
# integration test guarded on $NAMESPACE being present.
find_package(Qt6 ${QT_MIN_VERSION} COMPONENTS Core Network REQUIRED)

add_library(plumb_lib STATIC
    plumbmsg.cpp
    plumbmsg.h
    plumbresolve.cpp
    plumbresolve.h
    ninep.cpp
    ninep.h
    plumber.cpp
    plumber.h
)
set_target_properties(plumb_lib PROPERTIES AUTOMOC ON)
target_link_libraries(plumb_lib PUBLIC Qt6::Core Qt6::Network)
target_include_directories(plumb_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

find_package(Qt6 ${QT_MIN_VERSION} COMPONENTS Test)
if(Qt6Test_FOUND)
    add_executable(test_plumbmsg test_plumbmsg.cpp)
    target_link_libraries(test_plumbmsg PRIVATE plumb_lib Qt6::Test)
    add_test(NAME plumbmsg COMMAND test_plumbmsg)

    add_executable(test_plumbresolve test_plumbresolve.cpp)
    target_link_libraries(test_plumbresolve PRIVATE plumb_lib Qt6::Test)
    add_test(NAME plumbresolve COMMAND test_plumbresolve)

    # Live round-trip against a running plumber. Skips itself (passing) when no
    # plumb socket is reachable, so it is safe in headless/CI environments.
    add_executable(test_plumb_live test_plumb_live.cpp)
    target_link_libraries(test_plumb_live PRIVATE plumb_lib Qt6::Test)
    add_test(NAME plumb_live COMMAND test_plumb_live)
endif()

# --- [deft:nineify] plumb — the Kate plugin --------------------------------
# plumb_lib is the pure codec + 9P client. The plugin layer (F2 action,
# edit-port reader wiring, open/jump glue) needs KF6::TextEditor; build it only
# when found.
if(KF6TextEditor_FOUND)
    kcoreaddons_add_plugin(deft_plumb
        SOURCES plumbplugin.cpp plumbplugin.h
        INSTALL_NAMESPACE "kf6/ktexteditor"
    )
    target_link_libraries(deft_plumb PRIVATE
        plumb_lib
        KF6::TextEditor
        KF6::CoreAddons
        KF6::I18n
        Qt6::Widgets
    )
endif()
