cmake_minimum_required(VERSION 3.20)
project(InspacyCExamples LANGUAGES C)
find_package(InspacyGameC 1.3 CONFIG REQUIRED)
foreach(role player publisher reporter)
    add_executable(inspacy-c-${role} ${role}.c)
    target_link_libraries(inspacy-c-${role} PRIVATE Inspacy::GameC)
endforeach()
# Pin reviewed public roots and the game ID into the executable at build time.
if(WIN32 AND INSPACY_RECEIVER_ROOTS AND INSPACY_RECEIVER_GAME_ID MATCHES "^[1-9][0-9]*$")
    file(READ "${INSPACY_RECEIVER_ROOTS}" roots)
    string(REPLACE "\\" "\\\\" roots "${roots}")
    string(REPLACE "\"" "\\\"" roots "${roots}")
    string(REPLACE "\n" "" roots "${roots}")
    string(REPLACE "\r" "" roots "${roots}")
    configure_file(receiver_config.h.in receiver_config.h @ONLY)
    add_executable(inspacy-c-receiver receiver.c)
    target_include_directories(inspacy-c-receiver PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
    target_link_libraries(inspacy-c-receiver PRIVATE Inspacy::GameC)
endif()
if(WIN32)
    add_custom_target(inspacy-example-runtime ALL
        COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:inspacy-c-player>"
        COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:Inspacy::GameC>" "$<TARGET_FILE_DIR:inspacy-c-player>"
        VERBATIM)
    foreach(role player publisher reporter receiver)
        if(TARGET inspacy-c-${role})
            add_dependencies(inspacy-c-${role} inspacy-example-runtime)
        endif()
    endforeach()
endif()
