include (CMakeDependentOption)

# Install the English masters via CHECK_MANPAGE. The util binaries
# (alc/alcc/cas/wxcas) install their own English masters from their own
# CMakeLists.txt — we only drive the daemon/GUI/etc. binaries here.
if (BUILD_AMULECMD)
	check_manpage ("amulecmd")
endif()

if (BUILD_DAEMON)
	check_manpage ("amuled")
endif()

if (BUILD_ED2K)
	check_manpage ("ed2k")
endif()

if (BUILD_MONOLITHIC)
	check_manpage ("amule")
endif()

if (BUILD_REMOTEGUI)
	check_manpage ("amulegui")
endif()

if (BUILD_WEBSERVER)
	check_manpage ("amuleweb")
endif()

# Translated manpages — rendered at build time via po4a from the English
# masters plus po/manpages-<lang>.po. The rendered files are not tracked in
# git; po4a writes them straight into the build dir based on absolute paths
# templated into po4a.config (see po4a.config.in).
#
# A second build-time pass substitutes @MAN_DATE@ and @PACKAGE_VERSION@ (which
# po4a passes through verbatim) so the final installed manpages carry the
# correct .TH header. The double pass is driven by add_custom_command so
# CMake handles incremental rebuilds via DEPENDS on the .po inputs — po4a's
# own mtime-incremental logic is bypassed entirely.
#
# Source-tarball consumers that don't want po4a as a build dep can grab the
# pre-rendered tarball attached to each GitHub Release (built by the
# source-bundle job in .github/workflows/release.yml).

cmake_dependent_option (TRANSLATED_MANPAGES
	"Render and install translated manpages via po4a (requires po4a at build time)"
	ON ENABLE_NLS OFF)

set (AMULE_MANPAGE_LANGUAGES de es fr hu it pt_BR ro ru tr zh_TW)

if (TRANSLATED_MANPAGES)
	find_program (PO4A_EXECUTABLE po4a)

	if (NOT PO4A_EXECUTABLE)
		message (STATUS "po4a not found — translated manpages will not be installed. "
			"Install po4a (https://po4a.org) and reconfigure, or pass "
			"-DTRANSLATED_MANPAGES=NO to silence this notice.")
	else()
		configure_file (
			"${CMAKE_CURRENT_SOURCE_DIR}/po4a.config.in"
			"${CMAKE_CURRENT_BINARY_DIR}/po4a.config"
			@ONLY
		)

		# Per-binary install destination + build-system gate. The util
		# binaries have their rendered outputs land in their own build
		# subdirs because po4a.config.in points them there explicitly.
		set (MANPAGE_BINARIES amule amulecmd amuled amulegui amuleweb ed2k alc alcc cas wxcas)
		set (MANPAGE_BUILD_DIR_amule     "${CMAKE_CURRENT_BINARY_DIR}")
		set (MANPAGE_BUILD_DIR_amulecmd  "${CMAKE_CURRENT_BINARY_DIR}")
		set (MANPAGE_BUILD_DIR_amuled    "${CMAKE_CURRENT_BINARY_DIR}")
		set (MANPAGE_BUILD_DIR_amulegui  "${CMAKE_CURRENT_BINARY_DIR}")
		set (MANPAGE_BUILD_DIR_amuleweb  "${CMAKE_CURRENT_BINARY_DIR}")
		set (MANPAGE_BUILD_DIR_ed2k      "${CMAKE_CURRENT_BINARY_DIR}")
		set (MANPAGE_BUILD_DIR_alc       "${CMAKE_BINARY_DIR}/src/utils/aLinkCreator/docs")
		set (MANPAGE_BUILD_DIR_alcc      "${CMAKE_BINARY_DIR}/src/utils/aLinkCreator/docs")
		set (MANPAGE_BUILD_DIR_cas       "${CMAKE_BINARY_DIR}/src/utils/cas/docs")
		set (MANPAGE_BUILD_DIR_wxcas     "${CMAKE_BINARY_DIR}/src/utils/wxCas/docs")
		set (MANPAGE_GATE_amule     BUILD_MONOLITHIC)
		set (MANPAGE_GATE_amulecmd  BUILD_AMULECMD)
		set (MANPAGE_GATE_amuled    BUILD_DAEMON)
		set (MANPAGE_GATE_amulegui  BUILD_REMOTEGUI)
		set (MANPAGE_GATE_amuleweb  BUILD_WEBSERVER)
		set (MANPAGE_GATE_ed2k      BUILD_ED2K)
		set (MANPAGE_GATE_alc       BUILD_ALC)
		set (MANPAGE_GATE_alcc      BUILD_ALCC)
		set (MANPAGE_GATE_cas       BUILD_CAS)
		set (MANPAGE_GATE_wxcas     BUILD_WXCAS)

		# Build the complete output list so add_custom_command's OUTPUT
		# enumerates every file po4a will write. Listing them all makes
		# Ninja / Make track each as a real artifact (each can satisfy a
		# downstream install() target).
		set (PO4A_RAW_OUTPUTS "")
		foreach (LANG ${AMULE_MANPAGE_LANGUAGES})
			foreach (BIN ${MANPAGE_BINARIES})
				list (APPEND PO4A_RAW_OUTPUTS
					"${MANPAGE_BUILD_DIR_${BIN}}/${BIN}.${LANG}.1.in")
			endforeach()
		endforeach()

		# Collect po-file inputs for DEPENDS so a translator edit triggers
		# a rebuild. manpages.pot itself is regenerated by the maintainer
		# via scripts/update-manpages-po.sh and committed.
		file (GLOB PO4A_PO_INPUTS
			"${CMAKE_CURRENT_SOURCE_DIR}/po/manpages-*.po"
			"${CMAKE_CURRENT_SOURCE_DIR}/po/manpages-*.add"
			"${CMAKE_CURRENT_SOURCE_DIR}/po/manpages.pot"
		)

		# --no-update: render translated manpages from the existing .po
		# files but do NOT regenerate manpages.pot or msgmerge into
		# manpages-*.po. Keeping these source-tree files unmodified is
		# what scripts/update-manpages-po.sh is for; a plain `cmake
		# --build` should never dirty the working tree.
		add_custom_command (
			OUTPUT ${PO4A_RAW_OUTPUTS}
			COMMAND ${PO4A_EXECUTABLE} --no-update "${CMAKE_CURRENT_BINARY_DIR}/po4a.config"
			DEPENDS
				${PO4A_PO_INPUTS}
				"${CMAKE_CURRENT_BINARY_DIR}/po4a.config"
			COMMENT "Rendering translated manpages via po4a"
			VERBATIM
		)

		# Second pass: substitute @MAN_DATE@ / @PACKAGE_VERSION@ in each
		# rendered .lang.1.in -> .lang.1, then install the .lang.1 under
		# the right localized man dir. install() doesn't need its source
		# to exist at configure time -- it only checks at install time --
		# so as long as the custom_target runs as part of ALL the file
		# is materialized before `cmake --install`.
		set (PO4A_FINAL_OUTPUTS "")
		foreach (LANG ${AMULE_MANPAGE_LANGUAGES})
			foreach (BIN ${MANPAGE_BINARIES})
				set (_raw   "${MANPAGE_BUILD_DIR_${BIN}}/${BIN}.${LANG}.1.in")
				set (_final "${MANPAGE_BUILD_DIR_${BIN}}/${BIN}.${LANG}.1")

				add_custom_command (
					OUTPUT "${_final}"
					COMMAND ${CMAKE_COMMAND}
						"-DIN=${_raw}"
						"-DOUT=${_final}"
						"-DMAN_DATE=${MAN_DATE}"
						"-DPACKAGE_VERSION=${PACKAGE_VERSION}"
						-P "${CMAKE_SOURCE_DIR}/cmake/configure_translated_manpage.cmake"
					DEPENDS "${_raw}"
					VERBATIM
				)
				list (APPEND PO4A_FINAL_OUTPUTS "${_final}")

				if (${MANPAGE_GATE_${BIN}} AND TRANSLATION_${LANG})
					install (FILES "${_final}"
						DESTINATION "${CMAKE_INSTALL_MANDIR}/${LANG}/man1"
						RENAME "${BIN}.1"
					)
				endif()
			endforeach()
		endforeach()

		add_custom_target (translated_manpages ALL DEPENDS ${PO4A_FINAL_OUTPUTS})
	endif()
endif()
