include(cli/CMakeLists.txt)
include(client/CMakeLists.txt)
include(common/CMakeLists.txt)
include(core/CMakeLists.txt)
include(helper/CMakeLists.txt)
include(ui/CMakeLists.txt)
include(model/CMakeLists.txt)
include(widgets/CMakeLists.txt)

set(BTRFS_ASSISTANT_WARNINGS -Wall -Wextra -Wconversion)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    list(APPEND BTRFS_ASSISTANT_WARNINGS -Werror)
endif()

# Plain data types and helpers shared by the GUI and the privileged backend.
add_library(btrfs-assistant-common STATIC ${COMMON_SRC})
target_include_directories(btrfs-assistant-common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(btrfs-assistant-common PUBLIC Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::DBus)
target_compile_options(btrfs-assistant-common PRIVATE ${BTRFS_ASSISTANT_WARNINGS})

# Everything that needs root.
find_library(BTRFSUTIL_LIB btrfsutil)
include(CheckSymbolExists)
set(CMAKE_REQUIRED_LIBRARIES ${BTRFSUTIL_LIB})
check_symbol_exists(btrfs_util_delete_subvolume_by_id_fd btrfsutil.h HAVE_BTRFS_UTIL_DELETE_SUBVOLUME_BY_ID_FD)
unset(CMAKE_REQUIRED_LIBRARIES)
add_library(btrfs-assistant-core STATIC ${CORE_SRC})
if(HAVE_BTRFS_UTIL_DELETE_SUBVOLUME_BY_ID_FD)
    target_compile_definitions(btrfs-assistant-core PRIVATE HAVE_BTRFS_UTIL_DELETE_SUBVOLUME_BY_ID_FD)
endif()
target_include_directories(btrfs-assistant-core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(btrfs-assistant-core PUBLIC btrfs-assistant-common Qt${QT_VERSION_MAJOR}::Core PRIVATE ${BTRFSUTIL_LIB})
target_compile_options(btrfs-assistant-core PRIVATE ${BTRFS_ASSISTANT_WARNINGS})

set_property(SOURCE main.cpp APPEND PROPERTY COMPILE_DEFINITIONS "VERSION=\"1.5\"")

set(btrfsassistant_SRCS
    main.cpp
    ${CLI_SRC}
    ${CLIENT_SRC}
    ${UI_SRC}
    ${MODEL_SRC}
    ${WIDGETS_SRC}
    ${PROJECT_SOURCE_DIR}/icons/icons.qrc
)

file(GLOB TS_FILES ${PROJECT_SOURCE_DIR}/translations/*.ts)

qt6_create_translation(FILES_TS ${PROJECT_SOURCE_DIR}/src ${TS_FILES})

set(HELPER_PATH ${BTRFS_ASSISTANT_HELPER_DIR}/btrfs-assistant-helper)

add_executable(btrfs-assistant ${btrfsassistant_SRCS} ${FILES_TS})
target_compile_definitions(btrfs-assistant PRIVATE BTRFS_ASSISTANT_HELPER_PATH="${HELPER_PATH}")
target_link_libraries(btrfs-assistant PRIVATE Qt${QT_VERSION_MAJOR}::Widgets btrfs-assistant-common)
target_compile_options(btrfs-assistant PRIVATE ${BTRFS_ASSISTANT_WARNINGS})

# The helper's command line mode is the fallback for root when there is no system bus, so it runs the CLI on the core
add_executable(btrfs-assistant-helper ${HELPER_SRC} ${CLI_SRC} client/CliBackend.h)
target_link_libraries(btrfs-assistant-helper PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::DBus btrfs-assistant-common
                                                     btrfs-assistant-core)
target_compile_options(btrfs-assistant-helper PRIVATE ${BTRFS_ASSISTANT_WARNINGS})

configure_file(${PROJECT_SOURCE_DIR}/data/dbus-1/system-services/org.btrfsassistant.Helper1.service.in
               ${CMAKE_CURRENT_BINARY_DIR}/org.btrfsassistant.Helper1.service @ONLY)
configure_file(${PROJECT_SOURCE_DIR}/data/systemd/btrfs-assistant-helper.service.in ${CMAKE_CURRENT_BINARY_DIR}/btrfs-assistant-helper.service
               @ONLY)

install(TARGETS btrfs-assistant RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS btrfs-assistant-helper RUNTIME DESTINATION ${BTRFS_ASSISTANT_HELPER_DIR})
install(FILES ${FILES_TS} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/btrfs-assistant/translations)
install(FILES btrfs-assistant.conf DESTINATION ${CMAKE_INSTALL_FULL_SYSCONFDIR})
install(FILES btrfs-assistant.desktop DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications)
install(FILES btrfs-assistant.metainfo.xml DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/metainfo)
install(FILES ${PROJECT_SOURCE_DIR}/data/dbus-1/system.d/org.btrfsassistant.Helper1.conf DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/dbus-1/system.d)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/org.btrfsassistant.Helper1.service DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/dbus-1/system-services)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/btrfs-assistant-helper.service DESTINATION ${BTRFS_ASSISTANT_SYSTEMD_UNIT_DIR})
install(FILES ${PROJECT_SOURCE_DIR}/data/polkit-1/actions/org.btrfsassistant.policy DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/polkit-1/actions)
