# -*- Makefile -*-
# This file was generated by the i18n leiningen plugin
# Do not edit this file; it will be overwritten the next time you run
#   lein i18n init
#

# The name of the package into which the translations bundle will be placed
BUNDLE=puppetlabs.puppetdb

# The name of the POT file into which the gettext code strings (msgid) will be placed
POT_NAME=puppetdb.pot

# The list of names of packages covered by the translation bundle;
# by default it contains a single package - the same where the translations
# bundle itself is placed - but this can be overridden - preferably in
# the top level Makefile
PACKAGES?=$(BUNDLE)
LOCALES=$(basename $(notdir $(wildcard locales/*.po)))
BUNDLE_DIR=$(subst .,/,$(BUNDLE))
BUNDLE_FILES=$(patsubst %,resources/$(BUNDLE_DIR)/Messages_%.class,$(LOCALES))
FIND_SOURCES=find src -name \*.clj
# xgettext before 0.19 does not understand --add-location=file. Even CentOS
# 7 ships with an older gettext. We will therefore generate full location
# info on those systems, and only file names where xgettext supports it
LOC_OPT=$(shell xgettext --add-location=file -f - </dev/null >/dev/null 2>&1 && echo --add-location=file || echo --add-location)

LOCALES_CLJ=resources/locales.clj
define LOCALES_CLJ_CONTENTS
{
  :locales  #{$(patsubst %,"%",$(LOCALES))}
  :packages [$(patsubst %,"%",$(PACKAGES))]
  :bundle   $(patsubst %,"%",$(BUNDLE).Messages)
}
endef
export LOCALES_CLJ_CONTENTS


i18n: msgfmt

# Update locales/<project-name>.pot
update-pot: locales/$(POT_NAME)

locales/$(POT_NAME): $(shell $(FIND_SOURCES)) | locales
	@tmp=$$(mktemp $@.tmp.XXXX);                                            \
	$(FIND_SOURCES)                                                         \
	    | xgettext --from-code=UTF-8 --language=lisp                        \
	               --copyright-holder='Puppet <docs@puppet.com>'            \
	               --package-name="$(BUNDLE)"                               \
	               --package-version="$(BUNDLE_VERSION)"                    \
	               --msgid-bugs-address="docs@puppet.com"                   \
	               -k                                                       \
	               -kmark:1 -ki18n/mark:1                                   \
	               -ktrs:1 -ki18n/trs:1                                     \
	               -ktru:1 -ki18n/tru:1                                     \
	               -ktrun:1,2 -ki18n/trun:1,2                               \
	               -ktrsn:1,2 -ki18n/trsn:1,2                               \
	               $(LOC_OPT)                                               \
	               --add-comments --sort-by-file                            \
	               -o $$tmp -f -;                                           \
	sed -i.bak -e 's/charset=CHARSET/charset=UTF-8/' $$tmp;                 \
	sed -i.bak -e 's/POT-Creation-Date: [^\\]*/POT-Creation-Date: /' $$tmp; \
	rm -f $$tmp.bak;                                                        \
	if ! diff -q -I POT-Creation-Date $$tmp $@ >/dev/null 2>&1; then        \
	    mv $$tmp $@;                                                        \
	else                                                                    \
	    rm $$tmp; touch $@;                                                 \
	fi

# Run msgfmt over all .po files to generate Java resource bundles
# and create the locales.clj file
msgfmt: $(BUNDLE_FILES) $(LOCALES_CLJ) clean-orphaned-bundles

# Force rebuild of locales.clj if its contents is not the the desired one. The
# shell echo is used to add a trailing newline to match the one from `cat`
ifneq ($(shell cat $(LOCALES_CLJ) 2> /dev/null),$(shell echo '$(LOCALES_CLJ_CONTENTS)'))
.PHONY: $(LOCALES_CLJ)
endif
$(LOCALES_CLJ): | resources
	@echo "Writing $@"
	@echo "$$LOCALES_CLJ_CONTENTS" > $@

# Remove every resource bundle that wasn't generated from a PO file.
# We do this because we used to generate the english bundle directly from the POT.
.PHONY: clean-orphaned-bundles
clean-orphaned-bundles:
	@for bundle in resources/$(BUNDLE_DIR)/Messages_*.class; do                                  \
	  locale=$$(basename "$$bundle" | sed -E -e 's/\$$?1?\.class$$/_class/' | cut -d '_' -f 2;); \
	  if [ ! -f "locales/$$locale.po" ]; then                                                    \
	    rm "$$bundle";                                                                           \
	  fi                                                                                         \
	done

resources/$(BUNDLE_DIR)/Messages_%.class: locales/%.po | resources
	msgfmt --java2 -d resources -r $(BUNDLE).Messages -l $(*F) $<

# Use this to initialize translations. Updating the PO files is done
# automatically through a CI job that utilizes the scripts in the project's
# `bin` file, which themselves come from the `clj-i18n` project.
locales/%.po: | locales
	@if [ ! -f $@ ]; then                                         \
	    touch $@ && msginit --no-translator -l $(*F) -o $@ -i locales/$(POT_NAME); \
	fi

resources locales:
	@mkdir $@

help:
	$(info $(HELP))
	@echo

.PHONY: help

define HELP
This Makefile assists in handling i18n related tasks during development. Files
that need to be checked into source control are put into the locales/ directory.
They are

  locales/$(POT_NAME)   - the POT file generated by 'make update-pot'
  locales/$$LANG.po       - the translations for $$LANG

Only the $$LANG.po files should be edited manually; this is usually done by
translators.

You can use the following targets:

  i18n:             refresh all the files in locales/ and recompile resources
  update-pot:       extract strings and update locales/$(POT_NAME)
  locales/LANG.po:  create translations for LANG
  msgfmt:           compile the translations into Java classes; this step is
                    needed to make translations available to the Clojure code
                    and produces Java class files in resources/
endef
# @todo lutter 2015-04-20: for projects that use libraries with their own
# translation, we need to combine all their translations into one big po
# file and then run msgfmt over that so that we only have to deal with one
# resource bundle
