# CPack configuration for package generation
# This file contains all CPack settings for generating deb and rpm packages

# Set package metadata using project variables
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
    "Cognitive robotics runtime")
set(CPACK_PACKAGE_VENDOR "Salmanoff Project")
set(CPACK_PACKAGE_CONTACT "maintainer@salmanoff.org")

# Set package description.
# Use a single joined string so CMake does not serialize it as a semicolon list
# in the generated Debian control file.
string(JOIN "\n "
    CPACK_PACKAGE_DESCRIPTION
    "Comprehensive sensor management and control runtime."
    ""
    "Salmanoff provides unified interfaces for various sensor devices,"
    "including LiDAR systems."
    "It features modular architecture with support for multiple device"
    "types, asynchronous processing, and real-time data handling.")

# License information
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")

# Debian-specific packaged documentation.
set(SALMANOFF_CHANGELOG_TXT "${CMAKE_CURRENT_BINARY_DIR}/changelog")
set(SALMANOFF_CHANGELOG_GZ "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz")
file(WRITE "${SALMANOFF_CHANGELOG_TXT}"
    "salmanoff (${PROJECT_VERSION}) unstable; urgency=medium\n\n"
    "  * Package release.\n\n"
    " -- Salmanoff Project <maintainer@salmanoff.org>  Fri, 06 Mar 2026 00:00:00 +0000\n")
execute_process(
    COMMAND gzip -n -9 -c "${SALMANOFF_CHANGELOG_TXT}"
    OUTPUT_FILE "${SALMANOFF_CHANGELOG_GZ}"
)

set(SALMANOFF_MANPAGE_GZ "${CMAKE_CURRENT_BINARY_DIR}/salmanoff.1.gz")
execute_process(
    COMMAND gzip -n -9 -c "${CMAKE_CURRENT_SOURCE_DIR}/docs/salmanoff.1"
    OUTPUT_FILE "${SALMANOFF_MANPAGE_GZ}"
)

install(FILES "${SALMANOFF_CHANGELOG_GZ}" DESTINATION ${CMAKE_INSTALL_DOCDIR})
install(FILES "${SALMANOFF_MANPAGE_GZ}" DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
install(FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR} RENAME copyright)

# Enable deb and rpm generators
set(CPACK_GENERATOR "DEB;RPM")

# DEB package specific settings (Ubuntu)
set(CPACK_DEBIAN_PACKAGE_MAINTAINER
    "Salmanoff Project <maintainer@salmanoff.org>")
set(CPACK_DEBIAN_PACKAGE_SECTION "science")
set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
# Target Ubuntu distribution
set(CPACK_DEBIAN_PACKAGE_DISTRIBUTION "ubuntu")
# Build dependencies (from builddeps file)
# These are needed to build the package from source
set(CPACK_DEBIAN_PACKAGE_BUILD_DEPENDS
    "build-essential, cmake (>= 3.16), libboost-all-dev, flex, bison, ocl-icd-opencl-dev, liburing-dev")

# Runtime dependencies.
# Let dpkg-shlibdeps derive the actual ELF dependencies from the built
# binaries/libraries. Hard-coding old Boost soname packages here makes the
# package unnecessarily pull obsolete runtimes on newer distros, and in this
# tree the generated binaries are not currently linked against Boost DSOs.
set(CPACK_DEBIAN_PACKAGE_DEPENDS "")
set(CPACK_DEBIAN_PACKAGE_RECOMMENDS "libxcb1, libx11-6")
set(CPACK_DEBIAN_PACKAGE_SUGGESTS "livox-sdk")

# RPM package specific settings
set(CPACK_RPM_PACKAGE_LICENSE "Proprietary")
set(CPACK_RPM_PACKAGE_GROUP "Applications/Engineering")
set(CPACK_RPM_PACKAGE_URL "https://github.com/salmanoff/salmanoff")
set(CPACK_RPM_PACKAGE_REQUIRES "boost-system >= 1.72.0, boost-log >= 1.72.0, glibc, libstdc++, ocl-icd, liburing")
set(CPACK_RPM_PACKAGE_SUGGESTS "xcb, libX11, livox-sdk")

# Package file naming using Debian's architecture naming when available.
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "${CMAKE_SYSTEM_PROCESSOR}")
if(EXISTS "/usr/bin/dpkg")
    execute_process(
        COMMAND /usr/bin/dpkg --print-architecture
        OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
endif()

# Package file naming using project variables
set(CPACK_PACKAGE_FILE_NAME
    "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}")

# Enable automatic dependency detection for Debian packages
# This uses dpkg-shlibdeps to automatically detect shared library dependencies
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)

# Set compression
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_RPM_COMPONENT_INSTALL ON)

# Generate standard Debian shared-library metadata and use triggers instead of
# maintainer scripts for ldconfig handling.
set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS ON)
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/debian/postinst"
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/debian/postrm"
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/debian/triggers")

# Ship stripped binaries in the primary package.
set(CPACK_STRIP_FILES ON)
