# SPDX-FileCopyrightText: 2026 Dr. Tobias Quathamer <toddy@debian.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

cmake_minimum_required(VERSION 3.23)

project(
  manpages-l10n
  VERSION 4.32.2
  SPDX_LICENSE GPL-3.0-or-later
  DESCRIPTION "This project provides an infrastructure for translating manpages to different languages."
  HOMEPAGE_URL https://manpages-l10n-team.pages.debian.net/manpages-l10n/index.html
  LANGUAGES NONE
)


#####################################################################
# Required dependencies
#####################################################################

find_program(PO4A po4a REQUIRED)
find_program(PYTHON3 python3 REQUIRED)


#####################################################################
# Handle distribution
#####################################################################

set(DISTRIBUTION "debian-unstable" CACHE STRING
    "Enable translations for a specific distribution. Default is debian-unstable.")

message(STATUS "Wanted distribution: ${DISTRIBUTION}")

if(NOT IS_DIRECTORY "${CMAKE_SOURCE_DIR}/upstream/${DISTRIBUTION}")
    message(FATAL_ERROR
        "There are no translations for \"${DISTRIBUTION}\". "
        "Make sure the directory upstream/${DISTRIBUTION} exists.")
endif()


#####################################################################
# Handle compression
#####################################################################

set(COMPRESSION "gzip" CACHE STRING
    "Compression for translated manpages. Supported: gzip, bzip2, xz, none. Default is gzip.")

message(STATUS "Compression for translated manpages: ${COMPRESSION}")

if(COMPRESSION STREQUAL "gzip")
    find_program(COMPRESSOR gzip)
    if(NOT COMPRESSOR)
        message(FATAL_ERROR "You need to install gzip.")
    endif()
    set(COMP_EXTENSION ".gz")

elseif(COMPRESSION STREQUAL "bzip2")
    find_program(COMPRESSOR bzip2)
    if(NOT COMPRESSOR)
        message(FATAL_ERROR "You need to install bzip2.")
    endif()
    set(COMP_EXTENSION ".bz2")

elseif(COMPRESSION STREQUAL "xz")
    find_program(COMPRESSOR xz)
    if(NOT COMPRESSOR)
        message(FATAL_ERROR "You need to install xz.")
    endif()
    set(COMP_EXTENSION ".xz")

elseif(COMPRESSION STREQUAL "none")
    set(COMPRESSOR "none")
    set(COMP_EXTENSION "")

else()
    message(FATAL_ERROR
        "Compression \"${COMPRESSION}\" is not supported by manpages-l10n. "
        "Supported values: gzip, bzip2, xz, none.")
endif()

message(STATUS "Using compressor: ${COMPRESSOR}")
message(STATUS "Compression extension: ${COMP_EXTENSION}")


#####################################################################
# Main build target
#####################################################################

set(BUILD_MANPAGES_STAMP ${CMAKE_BINARY_DIR}/build-manpages.stamp)

add_custom_command(
    OUTPUT ${BUILD_MANPAGES_STAMP}
    COMMAND python3 ${CMAKE_SOURCE_DIR}/bin/build-manpages.py
      --sourcedir ${CMAKE_SOURCE_DIR}
      --builddir ${CMAKE_BINARY_DIR}/_gen
    COMMAND ${CMAKE_COMMAND} -E touch ${BUILD_MANPAGES_STAMP}
    DEPENDS ${CMAKE_SOURCE_DIR}/bin/build-manpages.py
    COMMENT "Generating manpages"
    VERBATIM
)

add_custom_target(build-manpages ALL
    DEPENDS ${BUILD_MANPAGES_STAMP}
)


#####################################################################
# Installation of manpages
#####################################################################

# This is a no-op for us, but suppresses a CMake warning when
# including GNUInstallDirs with the project LANGUAGE set to none.
set(CMAKE_SIZEOF_VOID_P 8)
include(GNUInstallDirs)

# These variables are not propagated to the install script
# automatically, so we need to set them manually.
install(CODE "set(SOURCE_DIR \"${CMAKE_SOURCE_DIR}\")")
install(CODE "set(MAN_DIR \"${CMAKE_INSTALL_MANDIR}\")")

# Add installation script
install(SCRIPT ${CMAKE_SOURCE_DIR}/install.cmake)

#####################################################################
# Create tarball
#####################################################################

# Recreate the "git archive" behaviour from the original dist-hook:
add_custom_target(dist
    COMMAND git archive --format=tar ${PROJECT_VERSION}
            --prefix=${PROJECT_NAME}-${PROJECT_VERSION}/
            | xz -c > ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}.tar.xz
    COMMENT "Creating source tarball ${PROJECT_NAME}-${PROJECT_VERSION}.tar.xz"
    VERBATIM
)
