find_package(OpenSSL REQUIRED COMPONENTS Crypto)

add_executable(EchoionUpdater echoion-updater.cpp ReleaseTree.cpp)
target_link_libraries(EchoionUpdater PRIVATE OpenSSL::Crypto Void::Update)
set_target_properties(EchoionUpdater PROPERTIES
    OUTPUT_NAME echoion-updater
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

if(UNIX AND NOT APPLE)
    set_target_properties(EchoionUpdater PROPERTIES
        INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}"
        INSTALL_RPATH_USE_LINK_PATH FALSE)
endif()

install(TARGETS EchoionUpdater
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    COMPONENT Runtime)

if(BUILD_TESTS)
    add_executable(EchoionReleaseTreeSecurityTests
        ReleaseTreeSecurityTests.cpp
        ReleaseTree.cpp)
    target_link_libraries(EchoionReleaseTreeSecurityTests PRIVATE OpenSSL::Crypto)

    add_test(NAME EchoionReleaseTreeSecurity
        COMMAND EchoionReleaseTreeSecurityTests)

    add_test(NAME EchoionUpdaterLifecycle
        COMMAND bash
            "${CMAKE_CURRENT_SOURCE_DIR}/../tests/updater_test.sh"
            "$<TARGET_FILE:EchoionUpdater>"
            "${CMAKE_SOURCE_DIR}")
endif()
