# EchoionStandalone — the daemon that runs Core without a TeamSpeak client.
#
# It uses the host-common CoreLoader/FileWatcher to load and hot-reload Core,
# and implements IPluginInterface with the reusable VoidTeamspeak client.

cmake_minimum_required(VERSION 4.0.0)
project(EchoionStandalone VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(PkgConfig REQUIRED)
pkg_check_modules(SWRESAMPLE REQUIRED IMPORTED_TARGET libswresample libavutil)
pkg_check_modules(OPUS REQUIRED IMPORTED_TARGET opus)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)

add_executable(EchoionStandalone
    src/main.cpp
    src/StandaloneClientInterface.cpp
    src/AudioEncoder.cpp
    src/MessageChunker.cpp
	src/ReleaseManifest.cpp
	src/EventCallbackGate.cpp
)

target_include_directories(EchoionStandalone PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/include"
)

target_link_libraries(EchoionStandalone PRIVATE
	Void::TeamSpeak
    Echoion::Host
    Echoion::ApiBase
    Echoion::ApiPlugin
    Echoion::ApiOpaque
    Echoion::ApiPluginInterface
    Echoion::ApiCoreInterface
    Echoion::ApiLogger
    Void::Data
    Void::Core
    Void::Log
    Void::System
    Void::Stacktrace
    Threads::Threads
    PkgConfig::SWRESAMPLE
    PkgConfig::OPUS
    OpenSSL::Crypto
)

set_target_properties(EchoionStandalone PROPERTIES
    OUTPUT_NAME "echoiond"
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

install(TARGETS EchoionStandalone RUNTIME DESTINATION bin)

if(BUILD_TESTS)
	add_executable(echoion_core_loader_reload_tests
		tests/test_core_loader_reload.cpp)
	target_link_libraries(echoion_core_loader_reload_tests PRIVATE
		Echoion::Host Echoion::ApiCoreInterface)
	target_compile_features(echoion_core_loader_reload_tests PRIVATE cxx_std_26)
	target_compile_definitions(echoion_core_loader_reload_tests PRIVATE
		"ECHOION_TEST_CORE_PATH=\"$<TARGET_FILE:EchoionCore>\"")
	add_dependencies(echoion_core_loader_reload_tests EchoionCore)
	add_test(NAME echoion_core_loader_reload_tests
		COMMAND echoion_core_loader_reload_tests)

    add_executable(echoion_standalone_message_tests
        tests/test_message_chunker.cpp
        src/MessageChunker.cpp
    )
    target_include_directories(echoion_standalone_message_tests PRIVATE
        "${CMAKE_CURRENT_SOURCE_DIR}/include"
    )
    target_compile_features(echoion_standalone_message_tests PRIVATE cxx_std_20)
    add_test(NAME echoion_standalone_message_tests
        COMMAND echoion_standalone_message_tests)

    add_executable(echoion_release_manifest_tests
        tests/test_release_manifest.cpp
        src/ReleaseManifest.cpp
    )
    target_include_directories(echoion_release_manifest_tests PRIVATE
        "${CMAKE_CURRENT_SOURCE_DIR}/include"
    )
    target_compile_features(echoion_release_manifest_tests PRIVATE cxx_std_26)
    target_link_libraries(echoion_release_manifest_tests PRIVATE
        Echoion::ApiBase Void::Data Void::System OpenSSL::Crypto)
	add_test(NAME echoion_release_manifest_tests
		COMMAND echoion_release_manifest_tests)

	add_executable(echoion_event_callback_gate_tests
		tests/test_event_callback_gate.cpp
		src/EventCallbackGate.cpp
	)
	target_include_directories(echoion_event_callback_gate_tests PRIVATE
		"${CMAKE_CURRENT_SOURCE_DIR}/include"
	)
	target_compile_features(echoion_event_callback_gate_tests PRIVATE cxx_std_26)
	target_link_libraries(echoion_event_callback_gate_tests PRIVATE
		Echoion::ApiBase Echoion::ApiConfig Echoion::ApiPlugin
		Void::Core Threads::Threads)
	add_test(NAME echoion_event_callback_gate_tests
		COMMAND echoion_event_callback_gate_tests)

	add_test(NAME echoion_stage_release_tests
		COMMAND bash "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_stage_release.sh"
			"${CMAKE_SOURCE_DIR}/scripts/stage-release.sh"
			$<TARGET_FILE:EchoionStandalone>)
endif()
