cmake_minimum_required(VERSION 3.21)

project(kate-custom VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)

set(QT_MIN_VERSION 6.5.0)
set(KF_MIN_VERSION 6.0.0)

# Default to an optimized build when the user did not pick one. The palette
# re-rank and indexing are measurably ~5-8x slower under -O0 (see docs/PERF.md),
# so an unqualified `cmake -B build` should not ship a Debug plugin.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
        "Build type (Debug, Release, RelWithDebInfo, MinSizeRel)" FORCE)
endif()

# Remember whether the user picked an install prefix *before* ECM's
# KDECMakeSettings runs: that module resets an unspecified prefix back to
# /usr/local, so we cannot rely on CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT
# after including it. We re-apply the user-local default below, post-include.
set(_deft_prefix_was_default ${CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT})

find_package(ECM ${KF_MIN_VERSION} REQUIRED NO_MODULE)
list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH})

include(KDEInstallDirs)
include(KDECMakeSettings)
include(KDECompilerSettings NO_POLICY_SCOPE)

# Default to a user-local install prefix when the user did not pick one. These
# are user KTextEditor plugins, not system packages: the stock /usr/local
# default needs root and is the wrong place for them. Applied here, after
# KDECMakeSettings, so it is not clobbered.
if(_deft_prefix_was_default)
    set(CMAKE_INSTALL_PREFIX "$ENV{HOME}/.local" CACHE PATH "Install prefix" FORCE)
endif()

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(Qt6 ${QT_MIN_VERSION} REQUIRED COMPONENTS Core Gui Widgets Concurrent)
find_package(Qt6 ${QT_MIN_VERSION} COMPONENTS Test)
find_package(KF6 ${KF_MIN_VERSION} COMPONENTS TextEditor XmlGui CoreAddons I18n)

# KDE's KDE_INSTALL_PLUGINDIR defaults to lib/<arch>/plugins/, but Qt6 plugins
# (which is where Kate scans for KTextEditor plugins) live under
# lib/<arch>/qt6/plugins/. Point the plugin install base at Qt's own plugin
# dir (QT6_INSTALL_PLUGINS is prefix-relative) so
# kcoreaddons_add_plugin(INSTALL_NAMESPACE kf6/ktexteditor) lands where Kate
# actually looks. kcoreaddons_add_plugin reads the *normal* variable, so set
# it directly (not just the cache entry, which the normal value would shadow).
set(KDE_INSTALL_PLUGINDIR "${QT6_INSTALL_PLUGINS}")
set(KDE_INSTALL_PLUGINDIR "${QT6_INSTALL_PLUGINS}" CACHE PATH "" FORCE)

# Qt only searches QT_PLUGIN_PATH for plugins. A user-prefix install (the
# default) puts the plugins outside the system search path, so Kate will not
# find them until that directory is added to QT_PLUGIN_PATH. Generate a Plasma
# login environment snippet that does exactly that, and — for a user-local
# install — drop it into ~/.config/plasma-workspace/env/ automatically so the
# lookup path is wired up without a manual step. For a system install the user
# installs it themselves (printed below); packagers usually don't want it.
get_filename_component(DEFT_PLUGIN_PATH
    "${CMAKE_INSTALL_PREFIX}/${QT6_INSTALL_PLUGINS}" ABSOLUTE)
configure_file("${CMAKE_SOURCE_DIR}/99-deft.sh.in"
    "${CMAKE_BINARY_DIR}/99-deft.sh" @ONLY)

# Is the chosen prefix inside the user's home (a personal install)?
string(FIND "${CMAKE_INSTALL_PREFIX}" "$ENV{HOME}" _deft_prefix_in_home)
if(_deft_prefix_in_home EQUAL 0)
    set(_deft_plasma_env_dir "$ENV{HOME}/.config/plasma-workspace/env")
    install(CODE "
        file(MAKE_DIRECTORY \"${_deft_plasma_env_dir}\")
        file(INSTALL \"${CMAKE_BINARY_DIR}/99-deft.sh\"
             DESTINATION \"${_deft_plasma_env_dir}\")
        message(STATUS \"Deft: installed Qt plugin-path snippet to ${_deft_plasma_env_dir}/99-deft.sh\")
        message(STATUS \"Deft: log out and back in (or run: export QT_PLUGIN_PATH=\\\"${DEFT_PLUGIN_PATH}:\\\$QT_PLUGIN_PATH\\\") so Kate finds the plugins now.\")
    ")
else()
    install(CODE "
        message(STATUS \"Deft: plugins installed under a system prefix; QT_PLUGIN_PATH already covers it if that prefix is standard.\")
        message(STATUS \"Deft: if Kate does not list them, add this to the Qt plugin path: ${DEFT_PLUGIN_PATH}\")
        message(STATUS \"Deft: a ready-made login snippet is at ${CMAKE_BINARY_DIR}/99-deft.sh (copy to ~/.config/plasma-workspace/env/).\")
    ")
endif()

enable_testing()

add_subdirectory(src)
