# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2025 ai-ad4 and the FreePDFEditor contributors
# src/CMakeLists.txt — application sources.
#
# At this stage (pre-M1) the only target is the empty-window application shell
# described in §14 step 2: a Qt Widgets main window with the chrome layout from
# §9 so the packaging pipeline and the platform integrations have something to
# sign, install, and notarize from day one. The sandboxed document process and
# the canvas land in M1/M2.

# Locate Qt. The vcpkg manifest does not currently include Qt (it is huge and
# often built system-wide); we use find_package so a system Qt or a vcpkg-built
# Qt both work.
find_package(Qt6 6.7 COMPONENTS Core Gui Widgets Network Concurrent QUIET)

if(NOT Qt6_FOUND)
    message(WARNING
        "Qt6 not found — the application target 'freepdfeditor' will not be built.\n"
        "Install Qt 6.7+ (system or via vcpkg) to build the GUI. The QPDF-based\n"
        "spikes and the pixel-diff harness do not require Qt and still build.")
    return()
endif()

qt_standard_project_setup()

# The M1 IPC layer (sandboxed document process + Cap'n Proto RPC). Qt-free so
# it is unit-testable; built before the app so the app can link it. Gated on
# the IPC deps (QPDF/Cap'n Proto/libseccomp) being available.
add_subdirectory(ipc)

add_executable(freepdfeditor
    app/main.cpp
    app/MainWindow.cpp
    app/MainWindow.h
)

target_compile_features(freepdfeditor PRIVATE cxx_std_20)
target_link_libraries(freepdfeditor PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Network Qt6::Concurrent)

# Link the IPC layer when it built (the app spawns the sandboxed document
# process and talks Cap'n Proto RPC over a socketpair — §2.1).
if(TARGET freepdfeditor_ipc)
    target_link_libraries(freepdfeditor PRIVATE freepdfeditor_ipc)
    target_compile_definitions(freepdfeditor PRIVATE FREEPDFEDITOR_HAVE_IPC=1)
endif()

freepdfeditor_apply_warnings(freepdfeditor)
freepdfeditor_apply_hardening(freepdfeditor)

# --- macOS bundle -----------------------------------------------------------
set_target_properties(freepdfeditor PROPERTIES
    MACOSX_BUNDLE TRUE
    MACOSX_BUNDLE_BUNDLE_NAME "FreePDFEditor"
    MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}"
    MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}"
    MACOSX_BUNDLE_GUI_IDENTIFIER "org.freepdfeditor.FreePDFEditor"
    MACOSX_BUNDLE_COPYRIGHT "© 2025 ai-ad4 and contributors, GPL-3.0-or-later"
    WIN32_EXECUTABLE TRUE
)

# --- install rules (drive CPack) -------------------------------------------
include(GNUInstallDirs)
install(TARGETS freepdfeditor
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    BUNDLE  DESTINATION .)

# Linux .desktop entry, hicolor icons, AppStream metainfo, man page, completions
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    install(FILES  "${CMAKE_SOURCE_DIR}/packaging/linux/freepdfeditor.desktop"
            DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications")
    install(DIRECTORY "${CMAKE_SOURCE_DIR}/packaging/linux/icons/"
            DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor")
    install(FILES  "${CMAKE_SOURCE_DIR}/packaging/linux/org.freepdfeditor.FreePDFEditor.metainfo.xml"
            DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/metainfo")
    install(FILES  "${CMAKE_SOURCE_DIR}/packaging/linux/freepdfeditor.1"
            DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
    install(FILES  "${CMAKE_SOURCE_DIR}/packaging/linux/freepdfeditor.bash-completion"
            DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions"
            RENAME "freepdfeditor")
endif()