cmake_minimum_required( VERSION 3.20 )

set(PROJECT_NAME plugin)
project(${PROJECT_NAME} VERSION 2.1.2.0)

option(TESTING "Compile Test Targets" ON)
option(EXAMPLE "Compile Example Targets" ON)
option(APPS    "Compile CLI signing apps (requires OpenSSL)" ON)

# ON only when plugin is the top-level project. An embedding host (FFS) sets it
# OFF (or simply never adds docs/) so the `docs` Doxygen target does not
# collide with the host's own.
if(NOT DEFINED PLUGIN_STANDALONE)
    if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
        set(PLUGIN_STANDALONE ON)
    else()
        set(PLUGIN_STANDALONE OFF)
    endif()
endif()

find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()

include(GNUInstallDirs)

set(CMAKE_VERBOSE_MAKEFILE OFF)
set(CMAKE_CXX_STANDARD 20)
# Leave CMAKE_BUILD_TYPE to the caller / toolchain (Conan sets it per profile).
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Debug)
endif()

# Setup gTest
if (${TESTING})
find_package(GTest QUIET)

if(${GTest_FOUND})
  enable_testing()
  include_directories(${GTEST_INCLUDE_DIR})
else()
  message(STATUS "GTest not found, disabling tests")
  set(TESTING OFF)
endif()
endif()

include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src")
include_directories("${CMAKE_CURRENT_BINARY_DIR}")

# The project

add_subdirectory(src)

if(${EXAMPLE} AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/example")
    add_subdirectory(example)
    message(STATUS "The example directory added")
endif()

if(${APPS} AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/apps")
    add_subdirectory(apps)
    message(STATUS "The apps directory added (dso-keygen, dso-sign, dso-verify)")
endif()

if(${TESTING} AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test")
    add_subdirectory(test)
    message(STATUS "The test directory added")
endif()

# ── API documentation (Doxygen) ─────────────────────────────────────────────
# The `docs` target lives in docs/CMakeLists.txt. `cmake --build <dir>
# --target docs` writes the HTML tree to <dir>/docs/html/, published at
# https://plugin.fedem.eu/api. Standalone builds only — an embedding host
# would collide on the `docs` target name. Doxygen absent → the subdir
# creates no target, nothing else changes.
if(PLUGIN_STANDALONE AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/docs/CMakeLists.txt")
    add_subdirectory(docs)
endif()

#
# installation package
#
configure_file(plugin.pc.in ${CMAKE_CURRENT_BINARY_DIR}/plugin.pc @ONLY)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/plugin.pc 
        COMPONENT plugin_development
        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

## create packages
set(CPACK_COMPONENTS_ALL ${CPACK_COMPONENTS_ALL} plugin)

set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
set(CPACK_PACKAGE_RELEASE "1")
set(CPACK_PACKAGE_DESCRIPTION "An add-on (plugin) utility library")
set(CPACK_PACKAGE_VENDOR "Fedem Software")
set(CPACK_PACKAGE_CONTACT "Fehmi Demiralp <fehmi.demiralp@fedem.tr>")
set(CPACK_PACKAGE_FILE_NAME "B${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_RELEASE}.${CMAKE_SYSTEM_PROCESSOR}")
set(CPACK_PACKAGING_INSTALL_DIRECTORY ${CMAKE_INSTALL_PREFIX})
set(CPACK_PACKAGE_HOMEPAGE_URL "https://plugin.fedem.tr")
set(CPACK_PACKAGE_LICENSE "MIT")
set(CPACK_GENERATOR 7Z TGZ ZIP DEB RPM)

# RPM-specific settings
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_COMPONENT_PLUGIN_DISPLAY_NAME "Plugin Package")
set(CPACK_COMPONENT_PLUGIN_DESCRIPTION "${CPACK_PACKAGE_DESCRIPTION}")
set(CPACK_RPM_PLUGIN_PACKAGE_GROUP "Development/Tools")
set(CPACK_RPM_PLUGIN_PACKAGE_LICENSE "MIT")
set(CPACK_RPM_PLUGIN_PACKAGE_URL "https://plugin.fedem.tr")
set(CPACK_RPM_PLUGIN_PACKAGE_ARCHITECTURE "x86_64")
set(CPACK_RPM_PLUGIN_PACKAGE_RELEASE ${CPACK_PACKAGE_RELEASE})
set(CPACK_RPM_PLUGIN_PACKAGE_REQUIRES "gcc >= 12.0")

set(CPACK_ARCHIVE_PLUGIN_FILE_NAME "A${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_RELEASE}.${CMAKE_SYSTEM_PROCESSOR}")
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)

# Debian-specific settings
set(CPACK_DEBIAN_PACKAGE_DEPENDS "")

include(CPack)
