# 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.4
  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 languages
#####################################################################

set(LANGS "" CACHE STRING
    "Only build and install these languages. Default is all languages.")

set(LANGS_ARG "")
if(LANGS)
    set(LANGS_ARG --language "${LANGS}")
    message(STATUS "Wanted languages: ${LANGS}")
else()
    message(STATUS "Wanted languages: all")
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()

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

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

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

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

message(STATUS "Using compression: ${COMPRESSION}")
message(STATUS "Using program: ${COMPRESSOR}")


#####################################################################
# 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
      --distribution ${DISTRIBUTION}
      ${LANGS_ARG}
    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}\")")
install(CODE "set(COMPRESSION \"${COMPRESSION}\")")
install(CODE "set(COMPRESSOR \"${COMPRESSOR}\")")

# 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
            --prefix=${PROJECT_NAME}-${PROJECT_VERSION}/
            -o ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}.tar
            ${PROJECT_VERSION}
    COMMAND xz -f ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}.tar
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    COMMENT "Creating source tarball ${PROJECT_NAME}-${PROJECT_VERSION}.tar.xz"
    VERBATIM
)
