cmake_minimum_required(VERSION 3.10)

set(PROJECT_NAME ukui-monitor-overlay)
set(PROJECT_VERSION "0.0.2")

project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 查找 Qt
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)

set(PROJECT_SOURCES
    src/main.cpp
    src/monitor.cpp
    src/monitor_settings.cpp
    src/climonitor.cpp
    src/environmentdetector.cpp
    src/gpuvendordetector.cpp
    src/gpumonitorfactory.cpp
    src/gpu_feature_detector.cpp
    src/nvidiagpumonitor.cpp
    src/amdgpumonitor.cpp
    src/intelgpumonitor.cpp
    src/mttgpumonitor.cpp
    src/pluginmanager.cpp
    src/cpumonitor.cpp
    src/memmonitor.cpp
    src/netmonitor.cpp
    src/diskmonitor.cpp
    src/uptimemonitor.cpp
    src/terminaloutput.cpp
    src/plugin_management_widget.cpp
    include/monitor.h
    include/monitor_settings.h
    include/climonitor.h
    include/environmentdetector.h
    include/gpuvendordetector.h
    include/gpumonitorfactory.h
    include/gpu_monitor_interface.h
    include/gpu_feature_detector.h
    include/nvidiagpumonitor.h
    include/amdgpumonitor.h
    include/intelgpumonitor.h
    include/mttgpumonitor.h
    include/systeminfoplugininterface.h
    include/pluginmanager.h
    include/cpumonitor.h
    include/memmonitor.h
    include/netmonitor.h
    include/diskmonitor.h
    include/uptimemonitor.h
    include/terminaloutput.h
    include/plugin_management_widget.h
)


if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${PROJECT_NAME} MANUAL_FINALIZATION ${PROJECT_SOURCES})
else()
    add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} ${RESOURCES})
endif()

set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDE_DIR})
target_compile_definitions(${PROJECT_NAME} PRIVATE APP_VERSION="${PROJECT_VERSION}")

# 处理平台相关配置
set(os_id "")
if(EXISTS "/etc/os-release")
    file(READ "/etc/os-release" os_release)
    string(REGEX MATCH "ID=[^\n]*" os_id "${os_release}")
    string(REPLACE "ID=" "" os_id "${os_id}")
    string(STRIP "${os_id}" os_id)
    string(REGEX REPLACE "^\"(.*)\"$" "\\1" os_id "${os_id}")
else()
    message(STATUS "File /etc/os-release not found, skipping distro-specific build configuration")
endif()

message(STATUS "Operating System: ${os_id}")

# openKylin
if("${os_id}" STREQUAL "openkylin")
    message(STATUS "Building for openKylin")
    find_package(PkgConfig REQUIRED)

    pkg_check_modules(KYSDKHARDWARE REQUIRED kysdk-hardware)
    target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKHARDWARE_INCLUDE_DIRS})
    target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKHARDWARE_LIBRARY_DIRS})
    target_link_libraries(${PROJECT_NAME} PRIVATE ${KYSDKHARDWARE_LIBRARIES})

    pkg_check_modules(KYSDKQTWIDGETS_PKG kysdk-qtwidgets)
    target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_INCLUDE_DIRS})
    target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_LIBRARY_DIRS})

    pkg_check_modules(KYSDKWAYLANDHELPER_PKG kysdk-waylandhelper)
    target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKWAYLANDHELPER_PKG_INCLUDE_DIRS})
    target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKWAYLANDHELPER_PKG_LIBRARY_DIRS})

    find_package(KF5WindowSystem REQUIRED)

    # 只链接 openKylin 特有的库，Qt::Widgets 由末尾全局链接
    target_link_libraries(${PROJECT_NAME} PRIVATE
        ${KYSDKHARDWARE_LIBRARIES}
        ${KYSDKQTWIDGETS_PKG_LIBRARIES}
        ${KYSDKWAYLANDHELPER_PKG_LIBRARIES}
        KF5::WindowSystem
    )

    target_compile_definitions(${PROJECT_NAME} PRIVATE OPENKYLIN)
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

include(CTest)
if(BUILD_TESTING)
    add_executable(terminal-output-test
        tests/test_terminaloutput.cpp
        src/terminaloutput.cpp
        include/terminaloutput.h
    )
    target_include_directories(terminal-output-test PRIVATE ${INCLUDE_DIR})
    add_test(NAME terminal-output-mode COMMAND terminal-output-test)

    add_executable(gpu-feature-detector-test
        tests/test_gpu_feature_detector.cpp
        src/gpu_feature_detector.cpp
        include/gpu_feature_detector.h
        include/gpu_monitor_interface.h
    )
    target_include_directories(gpu-feature-detector-test PRIVATE ${INCLUDE_DIR})
    target_link_libraries(gpu-feature-detector-test PRIVATE Qt${QT_VERSION_MAJOR}::Core)
    add_test(NAME gpu-feature-detector COMMAND gpu-feature-detector-test)

    # 用真实可执行文件做启动定位冒烟，需要主程序先构建完成。
    add_executable(adjustposition-smoke-test
        tests/test_adjustposition_smoke.cpp
    )
    target_link_libraries(adjustposition-smoke-test PRIVATE Qt${QT_VERSION_MAJOR}::Core)
    add_test(NAME adjustposition-smoke
        COMMAND adjustposition-smoke-test $<TARGET_FILE:${PROJECT_NAME}>
    )
    add_dependencies(adjustposition-smoke-test ${PROJECT_NAME})
endif()

# 如果是 Qt6，则需要调用 finalize
if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(${PROJECT_NAME})
endif()

install(TARGETS ${PROJECT_NAME} DESTINATION bin)
# 将desktop文件安装到/usr/share/applications
install(FILES ./ukui-monitor-overlay.desktop DESTINATION /usr/share/applications)
# /etc/xdg/autostart/
install(FILES ./ukui-monitor-overlay.desktop DESTINATION /etc/xdg/autostart/)
