# SPDX-FileCopyrightText: none
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.27)
set(PIM_VERSION "6.6.40")

project(KLdap VERSION ${PIM_VERSION})

# ECM setup
set(KF_MIN_VERSION "6.21.0")
set(QT_REQUIRED_VERSION "6.9.0")
set(QT6KEYCHAIN_LIB_VERSION "0.15.0")
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
find_package(ECM ${KF_MIN_VERSION} CONFIG REQUIRED)
set(CMAKE_MODULE_PATH
    ${KLdap_SOURCE_DIR}/cmake
    ${ECM_MODULE_PATH}
)

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

include(GenerateExportHeader)
include(ECMGenerateHeaders)

include(ECMSetupVersion)
file(
    GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES
    src/*.cpp
    src/*.h
    kioworker/*.cpp
    kioworker/*.h
)

set(PRE_COMMIT_INSTALLED FALSE)
if(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
    if(EXISTS "${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit")
        file(READ "${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit" FILE_CONTENTS)
        string(
            FIND "${FILE_CONTENTS}"
            "File generated by pre-commit"
            INDEX
        )
        if(${INDEX} GREATER_EQUAL 0)
            set(PRE_COMMIT_INSTALLED TRUE)
        endif()
    endif()
endif()
if(NOT ${PRE_COMMIT_INSTALLED})
    find_program(PRE_COMMIT_EXECUTABLE pre-commit)
    if(PRE_COMMIT_EXECUTABLE)
        message(STATUS "This module has pre-commit support. pre-commit binary was found. Installing pre-commit hook")
        execute_process(
            COMMAND
                "${PRE_COMMIT_EXECUTABLE}" install -f
            ERROR_QUIET
            RESULT_VARIABLE _precommitresult
            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
        )
        if(_precommitresult EQUAL 0)
            message(STATUS "Pre-commit installed.")
        else()
            message(
                WARNING
                "Error during installing pre-commit hook: ${PRE_COMMIT_EXECUTABLE} project source dir ${PROJECT_SOURCE_DIR}"
            )
            include(KDEGitCommitHooks)
            include(KDEClangFormat)
            kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
        endif()
    else()
        message(WARNING "This module has pre-commit support. pre-commit binary was not found.")
        include(KDEGitCommitHooks)
        include(KDEClangFormat)
        kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
    endif()
endif()

include(ECMQtDeclareLoggingCategory)
include(ECMDeprecationSettings)
include(ECMFeatureSummary)
include(ECMGenerateQDoc)
include(ECMCheckOutboundLicense)
set(KLDAP_LIB_VERSION ${PIM_VERSION})

find_package(KF6KIO ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6I18n ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6DocTools ${KF_MIN_VERSION} CONFIG)

find_package(Qt6Keychain ${QT6KEYCHAIN_LIB_VERSION} CONFIG)
set_package_properties(
    Qt6Keychain
    PROPERTIES
        DESCRIPTION
            "Provides support for secure credentials storage"
        URL
            "https://github.com/frankosterfeld/qtkeychain"
        TYPE
            REQUIRED
)

# tell what is missing without doctools
set_package_properties(
    KF6DocTools
    PROPERTIES
        DESCRIPTION
            "Provides tools to generate documentation in various format from DocBook files"
        TYPE
            OPTIONAL
        PURPOSE
            "Required to build documentation"
)

option(USE_UNITY_CMAKE_SUPPORT "Use UNITY cmake support (speedup compile time)" OFF)
set(COMPILE_WITH_UNITY_CMAKE_SUPPORT OFF)
if(USE_UNITY_CMAKE_SUPPORT)
    set(COMPILE_WITH_UNITY_CMAKE_SUPPORT ON)
endif()

########### Find packages ###########
find_package(KF6WidgetsAddons ${KF_MIN_VERSION} CONFIG REQUIRED)

add_definitions(-DQT_NO_CONTEXTLESS_CONNECT)
ecm_set_disabled_deprecation_versions(QT 6.11.0 KF 6.22.0)

find_package(Ldap)
set_package_properties(
    Ldap
    PROPERTIES
        TYPE
            RECOMMENDED
        PURPOSE
            "Needed to provide LDAP functionality in KDE"
)

find_package(Sasl2)
set_package_properties(
    Sasl2
    PROPERTIES
        TYPE
            REQUIRED
)

if(Ldap_FOUND)
    set(LDAP_FOUND 1)
endif()

add_definitions(-DTRANSLATION_DOMAIN=\"libkldap6\")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    # Not setting for GNU due to too many warnings related to private members of base classes or around lambdas
    # see e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56556 or https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79328
    string(APPEND CMAKE_CXX_FLAGS " -Wshadow")
endif()

option(WARNINGS_AS_ERRORS "Warnings are errors -Werror" OFF)
if(WARNINGS_AS_ERRORS)
    if(MSVC)
        add_compile_options(/WX)
        add_compile_options(/wd4996)
    else()
        add_compile_options(-Werror)
        add_compile_options(-Wno-error=deprecated-declarations)
    endif()
endif()

########### Targets ###########
add_subdirectory(src)
add_subdirectory(kioworker)

ecm_qt_install_logging_categories(
        EXPORT KLDAP
        FILE kldap.categories
        DESTINATION ${KDE_INSTALL_LOGGINGCATEGORIESDIR}
)

if(DEFINED kde_configure_git_pre_commit_hook)
    kde_configure_git_pre_commit_hook(CHECKS CLANG_FORMAT)
endif()

ki18n_install(po)
if(KF6DocTools_FOUND)
    kdoctools_install(po)
endif()
ecm_feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
file(
    GLOB_RECURSE ALL_SOURCE_FILES
    src/*.cpp
    src/*.h
    kioworker/*.cpp
    kioworker/*.h
)
ecm_check_outbound_license(LICENSES GPL-2.0-only  FILES ${ALL_SOURCE_FILES})
