###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2025 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

all:: objects languages

COPYRIGHT_HOLDER = The BRLTTY Developers
LANGUAGE_CODES = ar de fr it ka ru zh

LOCALE_SUBDIRECTORY = locale
MESSAGES_SUBDIRECTORY = LC_MESSAGES

TEMPLATE_EXTENSION = pot
SOURCE_EXTENSION = po
OBJECT_EXTENSION = mo

LANGUAGES_FILE = LINGUAS
TEMPLATE_FILE = $(SRC_DIR)/$(PACKAGE_TARNAME).$(TEMPLATE_EXTENSION)
OBJECT_FILE = $(PACKAGE_TARNAME).$(OBJECT_EXTENSION)

SOURCE_FILES = $(LANGUAGE_CODES:=.$(SOURCE_EXTENSION))
OBJECT_FILES = $(LANGUAGE_CODES:=.$(OBJECT_EXTENSION))

languages: $(LANGUAGES_FILE)
$(LANGUAGES_FILE): FORCE
	: >$@; \
	for language in $(LANGUAGE_CODES); do \
	echo $$language >>$@; \
	done

template $(TEMPLATE_EXTENSION): $(TEMPLATE_FILE)
$(TEMPLATE_FILE): $(BLD_TOP)$(PGM_DIR)/cmds.auto.h FORCE
	$(SRC_TOP)mkpot -p "$(PACKAGE_TARNAME)" -n "$(PACKAGE_VERSION)" -b "$(PACKAGE_BUGREPORT)" -c "$(COPYRIGHT_HOLDER)" -o $@ -- $(SRC_TOP) $(BLD_TOP)

sources: $(SOURCE_FILES)
%.$(SOURCE_EXTENSION): FORCE
	$(MSGMERGE) --quiet --update --force-po --no-wrap --no-fuzzy-matching -- $(SRC_DIR)/$@ $(TEMPLATE_FILE)
	set -- `date --utc +"%Y %m %d %H %M %z"` && \
	sed -e "/^.PO-Revision-Date:/s/....-..-.. ..:..[+-]..../$$1-$$2-$$3 $$4:$$5$$6/" -i $(SRC_DIR)/$@

objects: $(OBJECT_FILES)
%.$(OBJECT_EXTENSION): FORCE
	$(MSGFMT) --output-file $@ -- $(SRC_DIR)/$(@F:.$(OBJECT_EXTENSION)=.$(SOURCE_EXTENSION))
	directory="$(LOCALE_SUBDIRECTORY)/$(@:.$(OBJECT_EXTENSION)=)/$(MESSAGES_SUBDIRECTORY)"; \
	$(INSTALL_DIRECTORY) $${directory}; \
	cd $${directory}; \
	test -f $(OBJECT_FILE) || $(SYMLINK) ../../../$@ $(OBJECT_FILE);

clean::
	-rm -f -- *.$(OBJECT_EXTENSION) *~
	-rm -f -r -- $(LOCALE_SUBDIRECTORY)

install:: objects install-locale-directory
	for language in $(LANGUAGE_CODES); do \
	directory="$(INSTALL_LOCALE_DIRECTORY)/$${language}/$(MESSAGES_SUBDIRECTORY)" && \
        $(INSTALL_DIRECTORY) "$${directory}" && \
	$(INSTALL_DATA) "$${language}.$(OBJECT_EXTENSION)" "$${directory}/$(OBJECT_FILE)"; \
	done

uninstall::
	-rm -f -- $(INSTALL_LOCALE_DIRECTORY)/*/*/$(OBJECT_FILE)

