find_package(Qt6 6.2 REQUIRED COMPONENTS Widgets)
find_package(X11 REQUIRED)
if(NOT X11_XTest_FOUND)
    message(FATAL_ERROR "Linux keyboard output needs libxtst-dev")
endif()
foreach(application configurator controller)
    if(application STREQUAL "configurator")
        set(app_target motion-input-grid-studio)
        set(run_target run-motion-input-grid-studio)
    else()
        set(app_target motion-input-grid-controller)
        set(run_target run-motion-input-grid-controller)
    endif()
    string(TOUPPER "${application}" application_option)
    if(MIG_BUILD_${application_option})
        set(app_sources main.cpp canvas.cpp capture.cpp keyboard.cpp theme.cpp)
        if(application STREQUAL "controller")
            list(APPEND app_sources controller_window.cpp controller_profiles.cpp controller_runtime.cpp)
        else()
            list(APPEND app_sources window.cpp)
        endif()
        add_executable(${app_target} ${app_sources})
        qt_add_resources(${app_target} app_icons
            PREFIX "/icons"
            BASE "${CMAKE_SOURCE_DIR}/assets/apps"
            FILES "${CMAKE_SOURCE_DIR}/assets/apps/${app_target}.png")
        if(application STREQUAL "controller")
            target_compile_definitions(${app_target} PRIVATE MIG_CONTROLLER)
            target_link_libraries(${app_target} PRIVATE mig-controller-profiles)
        endif()
        target_compile_definitions(${app_target} PRIVATE MIG_APP_NAME="${app_target}")
        target_link_libraries(${app_target} PRIVATE Qt6::Widgets mig-native mig-format
            X11::X11 X11::Xtst)
        set_target_properties(${app_target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
        if(MIG_INSTALL)
            install(TARGETS ${app_target} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
            install(FILES "${CMAKE_SOURCE_DIR}/assets/apps/${app_target}.desktop"
                DESTINATION "${CMAKE_INSTALL_DATADIR}/applications")
            install(FILES "${CMAKE_SOURCE_DIR}/assets/apps/${app_target}.png"
                DESTINATION "${CMAKE_INSTALL_DATADIR}/icons/hicolor/256x256/apps")
        endif()
    endif()
endforeach()
