# **********************************************************************
# * Copyright (C) 2017-2025 MX Authors
# *
# * Authors: Adrian
# *          MX Linux <http://mxlinux.org>
# *
# * This file is part of file-rewind.
# *
# * file-rewind is free software: you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation, either version 3 of the License, or
# * (at your option) any later version.
# *
# * file-rewind is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with file-rewind.  If not, see <http://www.gnu.org/licenses/>.
# **********************************************************************/

cmake_minimum_required(VERSION 3.16)

# Get version from debian/changelog
execute_process(
    COMMAND dpkg-parsechangelog -SVersion
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE PROJECT_VERSION_FROM_CHANGELOG
    OUTPUT_STRIP_TRAILING_WHITESPACE
    RESULT_VARIABLE DPKG_RESULT
)

if(NOT DPKG_RESULT EQUAL 0)
    message(FATAL_ERROR "Failed to get version from debian/changelog using dpkg-parsechangelog")
endif()

project(file-rewind
    VERSION ${PROJECT_VERSION_FROM_CHANGELOG}
    DESCRIPTION "File Rewind - Git-based checkpoint and restore utility"
    LANGUAGES CXX
)

# The Polkit action, privileged helper and system schedules use fixed system paths.
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Install prefix" FORCE)
endif()

# Set C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Enable compile commands export for IDEs and tools
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Optimize for Ninja builds
if(CMAKE_GENERATOR STREQUAL "Ninja")
    # Enable colored output for Ninja
    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        add_compile_options(-fdiagnostics-color=always)
    elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
        add_compile_options(-fcolor-diagnostics)
    endif()
endif()

# Option to use clang for testing builds
option(USE_CLANG "Use clang compiler" OFF)
if(USE_CLANG)
    set(CMAKE_C_COMPILER clang)
    set(CMAKE_CXX_COMPILER clang++)
    set(CMAKE_CXX_COMPILER_ID "Clang")
    message(STATUS "Using clang compiler")
endif()

# Find Qt6 components
find_package(Qt6 REQUIRED COMPONENTS
    Core
    DBus
    Gui
    Widgets
    LinguistTools
)

# Enable automatic MOC and RCC processing
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

# Define source files
set(SOURCES
    src/main.cpp
    src/mainwindow.cpp
    src/about.cpp
    src/cmd.cpp
    src/git.cpp
    src/reminder.cpp
    src/schedule.cpp
    src/widgets.cpp
)

set(HEADERS
    src/mainwindow.h
    src/about.h
    src/cmd.h
    src/git.h
    src/reminder.h
    src/schedule.h
    src/widgets.h
)

set(RESOURCE_FILES
    images.qrc
)

# Get all translation files
file(GLOB TRANSLATION_FILES "translations/*.ts")

# Create the executable
add_executable(file-rewind
    ${SOURCES}
    ${HEADERS}
    ${RESOURCE_FILES}
)

# Link Qt6 libraries
target_link_libraries(file-rewind
    Qt6::Core
    Qt6::DBus
    Qt6::Gui
    Qt6::Widgets
)

# Set compiler flags
target_compile_options(file-rewind PRIVATE
    -Wpedantic
    -pedantic
    -Werror=return-type
    -Werror=switch
    -Werror=uninitialized
    -Werror
)

# Add compiler-specific flags
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR USE_CLANG)
    target_compile_options(file-rewind PRIVATE -Werror=return-stack-address)
else()
    target_compile_options(file-rewind PRIVATE -Werror=return-local-addr)
endif()

# Set compile definitions
target_compile_definitions(file-rewind PRIVATE
    QT_DEPRECATED_WARNINGS
    QT_DISABLE_DEPRECATED_BEFORE=0x060000
    VERSION="${PROJECT_VERSION}"
)

# Release-specific optimizations
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    target_compile_definitions(file-rewind PRIVATE NDEBUG)
    target_compile_options(file-rewind PRIVATE -O3)

    # Add LTO - different flags for different compilers
    if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR USE_CLANG)
        target_compile_options(file-rewind PRIVATE -flto=thin)
        target_link_options(file-rewind PRIVATE -flto=thin)
    else()
        target_compile_options(file-rewind PRIVATE -flto=auto)
        target_link_options(file-rewind PRIVATE -flto=auto)
    endif()
endif()

# Handle translations
qt6_add_translations(file-rewind
    TS_FILES ${TRANSLATION_FILES}
    LRELEASE_OPTIONS -compress -nounfinished -removeidentical -silent
    QM_FILES_OUTPUT_VARIABLE qm_files
)

# Tests (not built by default): cmake -B build -DBUILD_TESTING=ON && ctest --test-dir build
option(BUILD_TESTING "Build the unit tests" OFF)
if(BUILD_TESTING)
    enable_testing()
    find_package(Qt6 REQUIRED COMPONENTS Test)
    set(TEST_SOURCES ${SOURCES})
    list(REMOVE_ITEM TEST_SOURCES src/main.cpp)
    add_executable(test_git tests/test_git.cpp ${TEST_SOURCES})
    target_include_directories(test_git PRIVATE src)
    target_compile_definitions(test_git PRIVATE HELPER_PATH="${CMAKE_SOURCE_DIR}/scripts/helper"
                                                SOURCE_DIR="${CMAKE_SOURCE_DIR}"
                                                FILE_REWIND_BINARY="$<TARGET_FILE:file-rewind>")
    add_dependencies(test_git file-rewind)
    target_link_libraries(test_git Qt6::Widgets Qt6::DBus Qt6::Test)
    add_test(NAME test_git COMMAND test_git)
endif()

# Set target properties
set_target_properties(file-rewind PROPERTIES
    OUTPUT_NAME "file-rewind"
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

install(CODE [[
    if(NOT CMAKE_INSTALL_PREFIX STREQUAL "/usr")
        message(FATAL_ERROR "File Rewind needs the system layout. Install with --prefix /usr and use DESTDIR to stage it.")
    endif()
]])
install(TARGETS file-rewind RUNTIME DESTINATION bin)
install(PROGRAMS scripts/helper DESTINATION lib/file-rewind)
install(FILES scripts/org.mxlinux.pkexec.file-rewind-helper.policy DESTINATION share/polkit-1/actions)
install(FILES ${qm_files} DESTINATION share/file-rewind/locale)
install(FILES docs/help.html docs/license.html debian/changelog DESTINATION share/doc/file-rewind)
install(FILES docs/file-rewind.1 DESTINATION share/man/man1)
install(FILES file-rewind.desktop DESTINATION share/applications)
install(FILES file-rewind-thunar-sendto.desktop DESTINATION share/Thunar/sendto)
install(FILES file-rewind-dolphin-servicemenu.desktop DESTINATION share/kio/servicemenus)
install(FILES file-rewind.png DESTINATION share/pixmaps)
install(FILES file-rewind.png DESTINATION share/icons/hicolor/64x64/apps)
install(FILES file-rewind.svg DESTINATION share/icons/hicolor/scalable/apps)
install(FILES file-rewind.conf DESTINATION /etc)
install(FILES file-rewind-reminder.desktop DESTINATION /etc/xdg/autostart)
