# SPDX-FileCopyrightText: 2026 Didier Kryn <kryn@in2p3.fr>
#
# SPDX-License-Identifier: GPL-3.0-or-later

# Install and uninstall man page

# The install path is:
# $(INSTALL_ROOT)/share/man/man1/$(PGNAME) for the English version, which should
# always be installed, and
# $(INSTALL_ROOT)/share/man/$(LANG)/man1/$(PGNAME) for other languages
#
# Since we do not plan to provide all the complex combinations of language,
# region and encoding, we only provide utf8 encoding, and the base language,
# Eg: both "fr_BE.ISO8859-1" and "fr_CA.utf8" both lead to "fr" and the
# translation to French is encoded as utf8. We assume all other encoding are
# deprecated.

PGNAME ?= qmount
# Extract the language 
LANG_REG := $(firstword $(subst ., ,$(LANG)))
BASELANG := $(firstword $(subst _, ,$(LANG_REG)))

# Target of the man page in English:
MAN_TARGET := $(INSTALL_ROOT)/share/man/man1/$(PGNAME).1.gz

# If there is a man translation for the language, include it in target
ifeq ($(BASELANG), $(wildcard $(BASELANG)))
NL_TARGET := $(INSTALL_ROOT)/share/man/$(BASELANG)/man1/$(PGNAME).1.gz
else
undefine NL_TARGET
endif

# If the English version of the man page was stored under the name 'en', like
# the other ones, and the user's language was 'en' then the man page would be
# installed twice:
# at $(INSTALL_ROOT)/share/man/man1/$(PGNAME).1.gz.1
# at $(INSTALL_ROOT)/share/man/en/man1/$(PGNAME)
# This is why we name it 'jargon', so that it doesn't fit any language

# How to install a native language man page
# This line goes crazy if there is no selected native language
GENERIC_TARGET := $(INSTALL_ROOT)/share/man/%/man1/$(PGNAME).1.gz
$(GENERIC_TARGET): %
	sudo install -D -m 0644 $^ $(firstword $(subst ., ,$@))
	sudo gzip --suffix .1.gz $(firstword $(subst ., ,$@))

# Do install
install: $(MAN_TARGET) $(NL_TARGET)

$(MAN_TARGET): jargon
	sudo install -D -m 644 $^ $(firstword $(subst ., ,$@))
	sudo gzip --suffix .1.gz $(firstword $(subst ., ,$@))

uninstall:
	sudo rm -fv $(MAN_TARGET) $(NL_TARGET) 

debug:
	@echo 'LANG = ' $(LANG)
	@echo 'MAN_TARGET = ' $(MAN_TARGET)
	@echo 'NL_TARGET = ' $(NL_TARGET)
	@echo 'GENERIC_TARGET =' $(GENERIC_TARGET)

.PHONY: debug install uninstall
