# 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>=4
    libavutil>=57
)
pkg_check_modules(OPUS REQUIRED IMPORTED_TARGET opus)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)

set(ECHOION_UPDATE_FEED_URL "" CACHE STRING
	"Build-time default URL for the official signed update feed")
set(ECHOION_UPDATE_PUBLIC_KEYS_JSON "[]" CACHE STRING
	"Build-time Ed25519 trust anchors as [{\"id\":...,\"ed25519\":...}]")
set(ECHOION_RUNTIME_VERSION "${PROJECT_VERSION}" CACHE STRING
	"Canonical SemVer reported when no installed release manifest is present")
string(LENGTH "${ECHOION_RUNTIME_VERSION}" _runtime_version_length)
if(_runtime_version_length GREATER 64 OR
		NOT ECHOION_RUNTIME_VERSION MATCHES
		"^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$")
	message(FATAL_ERROR
		"ECHOION_RUNTIME_VERSION must be canonical SemVer without build metadata")
endif()
if(ECHOION_RUNTIME_VERSION MATCHES "-([^+]+)$")
	set(_runtime_prerelease "${CMAKE_MATCH_1}")
	string(REPLACE "." ";" _runtime_prerelease_identifiers
		"${_runtime_prerelease}")
	foreach(_identifier IN LISTS _runtime_prerelease_identifiers)
		if(_identifier MATCHES "^0[0-9]+$")
			message(FATAL_ERROR
				"ECHOION_RUNTIME_VERSION has a non-canonical numeric prerelease identifier")
		endif()
	endforeach()
endif()
foreach(_update_default IN ITEMS
		ECHOION_UPDATE_FEED_URL ECHOION_UPDATE_PUBLIC_KEYS_JSON
		ECHOION_RUNTIME_VERSION)
	if("${${_update_default}}" MATCHES "\\)ECHOION_UPDATE\\\"")
		message(FATAL_ERROR "${_update_default} contains the generated-header delimiter")
	endif()
endforeach()
set(ECHOION_TOOLCHAIN_ABI
	"${CMAKE_CXX_COMPILER_ID}-${CMAKE_CXX_COMPILER_VERSION}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")
configure_file(
	"${CMAKE_CURRENT_SOURCE_DIR}/include/echoion/standalone/UpdateBuildDefaults.hpp.in"
	"${CMAKE_CURRENT_BINARY_DIR}/generated/echoion/standalone/UpdateBuildDefaults.hpp"
	@ONLY)

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

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

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
	Void::Archive
	Void::Crypto
	Void::Net
	Void::Plugin
	Void::Update
    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_runtime_paths_tests
		tests/test_runtime_paths.cpp
		src/RuntimePaths.cpp
	)
	target_include_directories(echoion_runtime_paths_tests PRIVATE
		"${CMAKE_CURRENT_SOURCE_DIR}/include"
	)
	target_compile_features(echoion_runtime_paths_tests PRIVATE cxx_std_26)
	add_test(NAME echoion_runtime_paths_tests
		COMMAND echoion_runtime_paths_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_executable(echoion_update_service_tests
		tests/test_update_service.cpp
		src/UpdateService.cpp
	)
	target_include_directories(echoion_update_service_tests PRIVATE
		"${CMAKE_CURRENT_SOURCE_DIR}/include"
	)
	target_compile_features(echoion_update_service_tests PRIVATE cxx_std_26)
	target_link_libraries(echoion_update_service_tests PRIVATE
		Echoion::ApiBase Echoion::ApiConfig
		Void::Archive Void::Crypto Void::Data Void::Log Void::Net Void::Plugin
		Void::System Void::Update Threads::Threads)
	add_test(NAME echoion_update_service_tests
		COMMAND echoion_update_service_tests)

	add_executable(echoion_update_build_defaults_tests
		tests/test_update_build_defaults.cpp)
	target_include_directories(echoion_update_build_defaults_tests PRIVATE
		"${CMAKE_CURRENT_BINARY_DIR}/generated")
	target_compile_features(echoion_update_build_defaults_tests PRIVATE
		cxx_std_20)
	target_compile_definitions(echoion_update_build_defaults_tests PRIVATE
		"ECHOION_EXPECTED_RUNTIME_VERSION=\"${ECHOION_RUNTIME_VERSION}\"")
	add_test(NAME echoion_update_build_defaults_tests
		COMMAND echoion_update_build_defaults_tests)

	if(TARGET EchoionUpdater)
		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:EchoionUpdater>
				"${CMAKE_SOURCE_DIR}/packaging/service")
	endif()
endif()
