cmake_minimum_required(VERSION 3.24)

project(g19daemon)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX /usr CACHE PATH "Install prefix" FORCE)
endif()

include(FetchContent)

set(CPACK_RPM_PACKAGE_SUMMARY "G19daemon provides multiple virtual screens for the LCD on the Logitech G19 Keyboard")
set(CPACK_RPM_PACKAGE_NAME "g19deamon")
set(CPACK_RPM_PACKAGE_DESCRIPTION "G19daemon provides multiple virtual screens for the LCD on the Logitech G19 Keyboard")
set(CPACK_PACKAGE_VERSION_MAJOR 0)
set(CPACK_PACKAGE_VERSION_MINOR 6)
set(CPACK_PACKAGE_VERSION_PATCH 1)
include(CPack)

#set(VERSION_MAJOR 0 CACHE STRING "Major version" FORCE)
#set(VERSION_MINOR 1 CACHE STRING "Minor version" FORCE)
#set(VERSION_REVISION 2 CACHE STRING "Revison" FORCE)

set(PLUGINS_DIR lib/g19daemon/plugins CACHE PATH "Path to installed plugins")
set(THEMES_DIR lib/g19daemon/themes CACHE PATH "Path to installed themes")

# Forbid in-tree building
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
    message(STATUS "Please do an out-of-tree build:")
    message(STATUS "rm -f CMakeCache.txt && mkdir build && cd build; cmake .. && make")
    message(FATAL_ERROR "In-tree-build detected!")
endif (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")

find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets DBus)
qt_standard_project_setup()

find_package(libusb-1.0 REQUIRED)
find_package(PulseAudio REQUIRED)
find_package(KF6Wallet REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBEI REQUIRED IMPORTED_TARGET libei-1.0>=1.4)
pkg_check_modules(XKBCOMMON REQUIRED IMPORTED_TARGET xkbcommon)

include_directories(${Qt6Core_INCLUDE_DIRS} ${Qt6Gui_INCLUDE_DIRS} ${Qt6Widgets_INCLUDE_DIRS} ${LIBUSB_1_INCLUDE_DIRS})

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -ggdb")

configure_file(src/config.h.in config.h)

set(g19daemon_SRC
        src/main.cpp
        src/g19daemon.cpp
        src/g19device.cpp
        src/gscreen.cpp
        src/waylandinput.cpp
)

qt_add_resources(g19daemon_QRC src/g19daemon.qrc)

set(QAPPLICATION_CLASS QApplication CACHE STRING "Inheritance class for SingleApplication")
# If using Qt6, override DEFAULT_MAJOR_VERSION
set(QT_DEFAULT_MAJOR_VERSION 6 CACHE STRING "Qt version to use, defaults to 6")

# Declare how is the source going to be obtained
FetchContent_Declare(
        SingleApplication
        GIT_REPOSITORY https://github.com/itay-grudev/SingleApplication
        GIT_TAG        a218603d76a55dddb65b84f2b49ecaa8efc074ea
        #GIT_TAG        e22a6bc235281152b0041ce39d4827b961b66ea6
)

# Fetch the repository and make it available to the build
FetchContent_MakeAvailable(SingleApplication)

# Then simply use find_package as usual



add_executable(g19daemon ${g19daemon_SRC} ${g19daemon_QRC})
target_link_libraries(g19daemon ${Qt6Core_LIBRARIES} ${Qt6Gui_LIBRARIES} ${Qt6Widgets_LIBRARIES} ${Qt6DBus_LIBRARIES} ${LIBUSB_1_LIBRARIES} ${PULSEAUDIO_LIBRARY} ${PULSEAUDIO_MAINLOOP_LIBRARY} KF6::Wallet SingleApplication::SingleApplication)
target_link_libraries(g19daemon PkgConfig::LIBEI PkgConfig::XKBCOMMON)

install(TARGETS g19daemon RUNTIME DESTINATION bin)
install(PROGRAMS src/g19daemon.desktop DESTINATION share/applications)
install(FILES helper/70-g19.rules DESTINATION /etc/udev/rules.d)
install(FILES src/images/g19daemon.png DESTINATION /usr/share/icons)

#add_subdirectory(src/plugins/helloworld)
add_subdirectory(src/plugins/keybacklight)
add_subdirectory(src/plugins/backlight)
add_subdirectory(src/plugins/pavolume)
add_subdirectory(src/plugins/mpris)
add_subdirectory(src/plugins/clock)
add_subdirectory(src/plugins/hardwaremonitor)
add_subdirectory(src/plugins/sparklinegraph)
add_subdirectory(src/plugins/httpnotification)
add_subdirectory(src/plugins/processmonitor)
add_subdirectory(src/plugins/macroeditor)
add_subdirectory(src/plugins/musicviewer)


foreach(plugin clock backlight keybacklight pavolume mpris hardwaremonitor sparklinegraph httpnotification processmonitor macroeditor musicviewer)
    target_link_options(${plugin} PRIVATE "LINKER:--no-undefined")
endforeach()

include(CTest)
if(BUILD_TESTING)
    add_subdirectory(tests)
endif()
