# po/Makefile.am for anaconda
#
# Based loosely on Makefiles generated by gettext
#
# Copyright (C) 2016 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file is intended to replace everything that gettextize or autopoint
# installs. It has a lot less weird crud and tries to cooperate with
# automake at least a little bit.
#
# To use it:
# - Edit COPYRIGHT_HOLDER and MSGID_BUGS_ADDRESS below
#
# - Add the file types you're interested in to POTFILE_SUFFIXES. This replaces
#   the old POTFILES.in since the list of files you want to select for
#   translation is invariably all of them.
#
# - If you have other files that can't be selected by suffix, or that need to
#   be built from source files, add them to POTFILE_EXTRA
#
# - Add po/Makefile to AC_CONFIG_FILES (there may be po/Makefile.in if
#   switching from gettextize, replace that)
#
# - Get rid of AM_GNU_GETTEXT and AM_GNU_GETTEXT_VERSION. You might want to
#   add checks for xgettext, msgmerge, msgfmt, and msgcat. Up to you.
#
# - Add the following to .gitignore:
#     po/*.mo
#     po/*.po
#     po/*.mpo
#     po/*.pot
#
# - Remove everything in the po/ directory from source control except this
#   file.
#
# The idea is something like this:
#   From a clean project, you do the automake stuff and download a bunch of
#   .po files from the external translation site. These are the "source" files.
#
#   From the source tree we create a template file, <package>.pot, of all the
#   translatable strings. This file is not saved or distributed because there's
#   really no reason to do that if translations are not part of the source
#   tree. The file is pushed to the translators when creating a new release,
#   and it is used during the build, and that's it.
#
#   The .po files might not match the .pot file, because this is the nature of
#   linear time and translations being a separate task. Maybe the translator
#   last touched the .po file yesterday. Maybe three years ago. To get the
#   translations in sync with the source, which usually means untranslating
#   some strings, we run msgmerge on all of the source files. The merged files
#   are saved in builddir as <lang>.mpo, which is a non-standard file extension
#   I just made up.
#
#   The merged .po files are then compiled into .mo files, which are what
#   get installed to /usr/share/locale.

# Variables used in xgettext arguments
COPYRIGHT_HOLDER = Red Hat, Inc.
MSGID_BUGS_ADDRESS = anaconda-devel-list@redhat.com

# What kind of files are we looking for?
POTFILE_SUFFIXES = py c glade desktop js

# Extra files to scan for strings
# This is the config file used by userhelper. The startup notification can be
# translated. Just pull the string out and stick in a C-style header.
POTFILE_EXTRA = $(top_builddir)/data/liveinst/console.apps/liveinst.h

$(top_builddir)/data/liveinst/console.apps/liveinst.h: $(top_srcdir)/data/liveinst/console.apps/liveinst
	$(AM_V_GEN)sed -n 's/^STARTUP_NOTIFICATION_NAME=\(.*\)/char *s = N_(\1)/p' < $< > $@

# Below this line is the part that shouldn't need to be changed for other
# projects

# Turn POTFILE_SUFFIXES into a list of files
POTFILE_INPUT = $(foreach s,$(POTFILE_SUFFIXES),\
		$(shell find $(top_srcdir) -type d -name .git -prune -o -type f -name '*.$(s)' -print))

# The template file, generated by from all files with translatable strings.
POTFILE = $(PACKAGE).pot

# The translation files, provided by translators, pulled down from Zanata.
# Use whatever we got from Zanata, or nothing at all if no translations were
# pulled.
POFILES = $(wildcard $(srcdir)/*.po)

# The translations files after they have been merged with the latest copy of
# the .pot file. These are written to $(builddir) as <lang>.mpo.
MERGED_POFILES = $(patsubst %.po,%.mpo,$(notdir $(POFILES)))

# The MO files, which are the binary data built from the .po files.
MOFILES = $(patsubst %.mpo,%.mo,$(MERGED_POFILES))

# Special weirdness to auto-generate sr@latin.po if sr.po is present
MERGED_POFILES += $(if $(shell [ -f sr.po ] && echo y),sr@latin.mpo,)
sr@latin.mpo: $(srcdir)/sr.po
	$(AM_V_GEN)$(MSGFILTER) -i $(srcdir)/sr.po -o $@ recode-sr-latin

# The gettext programs and arguments
# xgettext eXtracts strings from translatable files and generates the template
# (.pot) file. We use a wrapper from translation-canary in order to catch
# warnings about strings that may not be translatable, and all the keyword
# arguments define the various functions that are used to mark a string as
# translatable
XGETTEXT = $(top_srcdir)/translation-canary/xgettext_werror.sh
XGETTEXT_OPTIONS = --keyword=_ --keyword=N_ --keyword=P_:1,2 \
	           --keyword=C_:1c,2 --keyword=CN_:1c,2 --keyword=CP_:1c,2,3 \
		   --add-comments=TRANSLATORS: \
		   --from-code=UTF-8 --package-name=$(PACKAGE) \
		   --package-version=$(PACKAGE_VERSION) \
		   --copyright-holder="$(COPYRIGHT_HOLDER)" \
		   --msgid-bugs-address="$(MSGID_BUGS_ADDRESS)"

# msgfmt compiles a .po file into a .mo file
# Do not include --check in the options, since those checks are more
# conveniently done by translation-canary
MSGFMT = msgfmt
MSGFMT_OPTIONS =

# msgmerge merges changes in the template (.pot) file back into the translation
# (.po) file
MSGMERGE = msgmerge
MSGMERGE_OPTIONS =

# msgfilter applies a filter to a .po file. We use this to automatically
# transliterate Serbian from Cyrillic (sr) to Latin (sr@latin).
MSGFILTER = msgfilter

# msgcat cats multiple .po (or .pot) files together. We use this to generate
# the .pot file from multiple parts.
MSGCAT = msgcat

# Automake magic for verbose/non-verbose build output
GETTEXT_V_EXTRACT   = $(GETTEXT_V_EXTRACT_$(V))
GETTEXT_V_EXTRACT_  = $(GETTEXT_V_EXTRACT_$(AM_DEFAULT_VERBOSITY))
GETTEXT_V_EXTRACT_0 = @echo "  EXTRACT " $@;

GETTEXT_V_FORMAT    = $(GETTEXT_V_FORMAT_$(V))
GETTEXT_V_FORMAT_   = $(GETTEXT_V_FORMAT_$(AM_DEFAULT_VERBOSITY))
GETTEXT_V_FORMAT_0  = @echo "  FORMAT  " $@;

GETTEXT_V_MERGE     = $(GETTEXT_V_MERGE_$(V))
GETTEXT_V_MERGE_    = $(GETTEXT_V_MERGE_$(AM_DEFAULT_VERBOSITY))
GETTEXT_V_MERGE_0   = @echo "  MERGE   " $@;

# If make was run with V=1, add verbose options to msgfmt
MSGFMT_V_OPTIONS    = $(MSGFMT_V_OPTIONS_$(V))
MSGFMT_V_OPTIONS_   = $(MSGFMT_V_OPTIONS_$(AM_DEFAULT_VERBOSITY))
MSGFMT_V_OPTIONS_0  = $(MSGFMT_OPTIONS)
MSGFMT_V_OPTIONS_1  = $(MSGFMT_OPTIONS) --statistics --verbose

# Same with msgmerge
MSGMERGE_V_OPTIONS  = $(MSGMERGE_V_OPTIONS_$(V))
MSGMERGE_V_OPTIONS_ = $(MSGMERGE_V_OPTIONS_$(AM_DEFAULT_VERBOSITY))
MSGMERGE_V_OPTIONS_0 = $(MSGMERGE_OPTIONS) --quiet
MSGMERGE_V_OPTIONS_1 = $(MSGMERGE_OPTIONS) --verbose

# Actually do stuff:
# .po files get distributed but not installed
dist_noinst_DATA = $(POFILES)

# Build the .mo files but don't actually do anything with them. The real
# install part is in the install-data-local target below. Build the .pot file
# as well, even if there are no .mo files to build, so it can be tested.
nodist_noinst_DATA = $(MOFILES) $(POTFILE)

# How to build the .pot file. This needs to be regenerated if anything that
# goes into it has changed.
# Build in two parts so that the bulk of the files are saved as relative to
# top_srcdir (via --directory). Leave $POTFILES_EXTRAS as-is so the extra
# targets don't need to worry about paths changing out from under them.

main.pot: $(POTFILE_INPUT)
	$(GETTEXT_V_EXTRACT)$(XGETTEXT) $(XGETTEXT_OPTIONS) --directory=$(top_srcdir) -o $@ \
	    $(patsubst $(top_srcdir)/%,%,$(POTFILE_INPUT))

extra.pot: $(POTFILE_EXTRA)
	$(GETTEXT_V_EXTRACT)$(XGETTEXT) $(XGETTEXT_OPTIONS) -o $@ $(POTFILE_EXTRA)

$(POTFILE): main.pot extra.pot
	$(AM_V_GEN)$(MSGCAT) -o $@ --use-first main.pot extra.pot

# Force a rebuild of the .pot file. Useful if something got removed, for
# example.
$(PACKAGE).pot-update:
	@rm -f $(POTFILE)
	@$(MAKE) $(POTFILE)

# How to build the merged .mpo files from the .po files
$(MERGED_POFILES): $(POFILES) $(POTFILE)

.po.mpo:
	$(GETTEXT_V_MERGE)$(MSGMERGE) $(MSGMERGE_V_OPTIONS) -o $@ $< $(POTFILE)

# How to build the .mo files from the .mpo files
$(MOFILES): $(MERGED_POFILES)

.mpo.mo:
	$(GETTEXT_V_FORMAT)$(MSGFMT) $(MSGFMT_V_OPTIONS) -o $@ $<

# Install the .mo files.
# .mo files get installed as $datadir/locale/<lang>/LC_MESSAGES/<package>.po
# which doesn't really fit well with the way make or automake do things but
# that is the world we live in
localedir = $(datadir)/locale
install-data-local:
	@for mo in $(MOFILES) ; do \
		lang="$$(basename "$$mo" .mo)" ; \
		$(MKDIR_P) $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES || exit $$? ; \
		$(INSTALL_DATA) "$$mo" "$(DESTDIR)$(localedir)/$$lang/LC_MESSAGES/$(PACKAGE).mo" || exit $$? ; \
	done

uninstall-local:
	@for mo in $(MOFILES) ; do \
		lang="$$(basename "$$mo" .mo)" ; \
		rm -f $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES/$(PACKAGE).mo ; \
	done

CLEANFILES = $(MERGED_POFILES) $(MOFILES) $(POTFILE) main.pot extra.pot
