set(LOMIRI_API_PKGCONFIG_SUFFIX)
if(ENABLE_QT6)
    string(APPEND LOMIRI_API_PKGCONFIG_SUFFIX "-qt6")
endif()
pkg_check_modules(NOTIFICATIONS_API REQUIRED lomiri-shell-notifications${LOMIRI_API_PKGCONFIG_SUFFIX}=3)

set(CORE_SRCS
ActionModel.cpp
../include/ActionModel.h
DBusTypes.cpp
../include/DBusTypes.h
Notification.cpp
../include/Notification.h
NotificationModel.cpp
../include/NotificationModel.h
NotificationServer.cpp
../include/NotificationServer.h
NotificationClient.cpp
../include/NotificationClient.h
${NOTIFICATIONS_API_INCLUDEDIR}/lomiri/shell/notifications/ModelInterface.h
)

set_source_files_properties(
    "org.freedesktop.Notifications.xml"
    PROPERTIES
    NO_NAMESPACE YES
    INCLUDE "DBusTypes.h"
)

qt_add_dbus_adaptor(
    CORE_SRCS
    "org.freedesktop.Notifications.xml"
    NotificationServer.h
    NotificationServer
    NotificationsAdaptor
)

qt_add_dbus_interface(
    CORE_SRCS
    "org.freedesktop.Notifications.xml"
    NotificationsInterface
)

add_library(notifybackend STATIC
${CORE_SRCS}
)
# Static library that's only used during the build, and linked into shared libraries with PIC enabled. Therefore needs to be PIC as well.
set_target_properties(notifybackend PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

add_library(notifyplugin MODULE
NotificationPlugin.cpp
../include/NotificationPlugin.h
)

set_target_properties(notifyplugin PROPERTIES
  SOVERSION ${SONAME}
  VERSION ${SOVERSION}
)

target_include_directories(notifybackend
  PUBLIC
  ${NOTIFICATIONS_API_INCLUDE_DIRS}
)

target_link_libraries(
  notifyplugin
  notifybackend
)

add_library(notifyclientplugin MODULE
NotificationClientPlugin.cpp
../include/NotificationClientPlugin.h
NotificationClient.cpp
../include/NotificationClient.h
)

set_target_properties(notifyclientplugin PROPERTIES
  SOVERSION ${SONAME}
  VERSION ${SOVERSION}
)

target_link_libraries(
  notifyclientplugin
  notifybackend
)

target_link_libraries(notifybackend Qt::Widgets Qt::DBus Qt::Qml)

execute_process(
    COMMAND
    ${PKG_CONFIG_EXECUTABLE} --modversion lomiri-shell-api${LOMIRI_API_PKGCONFIG_SUFFIX}
    OUTPUT_VARIABLE SHELL_API_VERSION
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(SHELL_API_VERSION STREQUAL "")
    message(FATAL_ERROR "Could not determine version of lomiri-shell-api${LOMIRI_API_PKGCONFIG_SUFFIX}.pc.")
else()
    message(STATUS "lomiri-shell-api${LOMIRI_API_PKGCONFIG_SUFFIX} version: ${SHELL_API_VERSION}")
endif()

set(SHELL_INSTALL_QML_PKGCONFIG_ARGS)
if(SHELL_API_VERSION VERSION_GREATER_EQUAL "1.1")
    list(APPEND SHELL_INSTALL_QML_PKGCONFIG_ARGS
        --define-variable=libdir=${CMAKE_INSTALL_FULL_LIBDIR}
        --variable=plugindir
    )
else()
    list(APPEND SHELL_INSTALL_QML_PKGCONFIG_ARGS
        --variable=plugindir_suffix
    )
endif()

execute_process(
    COMMAND
    ${PKG_CONFIG_EXECUTABLE} ${SHELL_INSTALL_QML_PKGCONFIG_ARGS} lomiri-shell-api${LOMIRI_API_PKGCONFIG_SUFFIX}
    OUTPUT_VARIABLE SHELL_INSTALL_QML
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(SHELL_INSTALL_QML STREQUAL "")
    message(FATAL_ERROR "Could not determine plugin installation dir.")
else()
    message(STATUS "Lomiri shell plugin installation dir: ${SHELL_INSTALL_QML}")
endif()

execute_process(
    COMMAND
    ${PKG_CONFIG_EXECUTABLE} --variable=plugindir lomiri-shell-api${LOMIRI_API_PKGCONFIG_SUFFIX}
    OUTPUT_VARIABLE SHELL_PLUGINDIR
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(SHELL_PLUGINDIR STREQUAL "")
    message(FATAL_ERROR "Could not determine plugin import dir.")
else()
    message(STATUS "Lomiri shell plugin import dir: ${SHELL_PLUGINDIR}")
endif()

install(
  TARGETS notifyplugin notifyclientplugin
  ARCHIVE DESTINATION ${SHELL_PLUGINDIR}/Lomiri/Notifications
  LIBRARY DESTINATION ${SHELL_PLUGINDIR}/Lomiri/Notifications
)

install(FILES qmldir
  DESTINATION ${SHELL_INSTALL_QML}/Lomiri/Notifications
)
