# Additional make targets

.NOTPARALLEL:
DISTFILES.extra2 = Rules-others Makevars.in

#== Output statistics

l10n-stats:
	@catalogs='$(CATALOGS)'; \
	for cat in $$catalogs; do \
	  lang=`basename $$cat .gmo;`; \
	  stats=`msgfmt --statistics -o /dev/null $$lang.po 2>&1`; \
	  echo "    $$lang: $$stats"; \
	done

#== List translators

l10n-translators:
	@catalogs='$(CATALOGS)'; \
	for cat in $$catalogs; do \
	  lang=`basename $$cat .gmo;`; \
	  echo " $$lang:"; \
	  sed -n -E "/# Translators/,/msgid/s/# ([^<,]*).*/  \1/p" $$lang.po | tail -n +2 | sort -u; \
	done

#== Clean obsolete strings and clear fuzzy flags

define clean_po =
	msgattrib $(MSGCAT_OPTIONS) --no-obsolete --clear-fuzzy -o cleaned.po $(1); \
	if ! cmp -s cleaned.po $(1) ; then \
	  count_obsolete=`grep -c "#~ msgid" $(1)`; \
	  count_fuzzy=`grep -c "#, fuzzy" $(1)`; \
	  echo "Cleaning $(1): $$count_obsolete obsolete entries, $$count_fuzzy fuzzy entries"; \
	  rm $(1) && mv cleaned.po $(1); \
	else \
	  rm cleaned.po; \
	fi
endef

l10n-clean:
	@catalogs='$(CATALOGS)'; \
	for cat in $$catalogs; do \
	  lang=`basename $$cat .gmo;`; \
	  $(call clean_po,$$lang.po); \
	done

#== Regenerate POTFILES.in if the list of source files changed
#== (executed before the main update-po rule)

POTSRCS = $(foreach act, $(POTINCLUDES), -i $(act) $(foreach excl, $(POTEXCLUDES), -x $(act)/$(excl)))

update-po: update-pot
update-pot:
	@if [ -x $(top_srcdir)/po/update-potfiles ] ; then \
	   $(top_srcdir)/po/update-potfiles $(POTSRCS) > POTFILES.inT ; \
	   if ! cmp -s POTFILES.inT POTFILES.in ; then \
	     rm POTFILES.in && mv POTFILES.inT POTFILES.in; \
	     echo "POTFILES.in updated"; \
	   else \
	     rm POTFILES.inT; \
	   fi; \
	 fi

#== Auto-install catalogs locally

update-gmo: stamp-li
all: stamp-li

stamp-li: $(GMOFILES)
	@$(MAKE) local-install ; \
	 echo "touch stamp-li" && \
	 echo timestamp > stamp-liT && \
	 mv stamp-liT stamp-li; \

local-install:
	@$(mkdir_p) $(top_srcdir)/locale &>/dev/null; \
	 if [ $$? -eq 0 ] ; then \
	   catalogs='$(CATALOGS)'; \
	   for cat in $$catalogs; do \
	     cat=`basename $$cat`; \
	     lang=`echo $$cat | sed -e 's/\.gmo$$//'`; \
	     dir=$(top_srcdir)/locale/$$lang/LC_MESSAGES; \
	     $(mkdir_p) $$dir; \
	     if test -r $$cat; then realcat=$$cat; else realcat=$(srcdir)/$$cat; fi; \
	     $(INSTALL_DATA) $$realcat $$dir/$(DOMAIN).mo; \
	     echo "installing $$realcat as $$dir/$(DOMAIN).mo"; \
	   done; \
	 fi

.PHONY: local-install update-pot

#== Transifex synchronization

CURL     = curl
TX       = tx
TXORG    = artic-games
TXPROJ   = freedroid-rpg
TXPREFIX = src
TXPROPS  = '{"category":["src"]}'
TXCRED   = $(shell $(top_srcdir)/po/tx-credentials -t)
TXHOST   = $(shell $(top_srcdir)/po/tx-credentials -r)
TXRSRC   = freedroidrpg

comma:= ,
empty:=
space:= $(empty) $(empty)
TXLANGS = $(subst $(space),$(comma),$(foreach cata,$(CATALOGS),$(basename $(cata))))

# Update the transifex potfiles
#
# if the associated resource is not found in the tx config file:
#   - the resource is created,
#   - the pot file is pushed,
#   - and the category is changed.
# else:
#   - the pot file is pushed.
#
# Note: It is not yet possible to define a resource's category using tx-set
# (transifex-client version 0.10). We thus directly use the transifex remote
# API to change it once the resource has been pushed.

tx-update: $(DOMAIN).pot
	@test " $(TXCRED)" = " " \
	 && { \
	      echo "You do not have a Transifex account"; \
	      exit 1; \
	    }; \
	$(mkdir_p) tx-$(TXPREFIX); \
	rsrc=$(TXRSRC); \
	cp $(DOMAIN).pot tx-$(TXPREFIX)/$$rsrc.pot; \
	if test -r tx-$(TXPREFIX)/$$rsrc.pot; then \
	  if ! $(TX) status $(TXPROJ).$(TXPREFIX)-$$rsrc >/dev/null 2>/dev/null; then \
	    echo "Creating $(TXPREFIX)-$$rsrc"; \
	    $(TX) add --file-filter="tx-$(TXPREFIX)/$$rsrc-<lang>.po" --type=PO --organization=$(TXORG) --project=$(TXPROJ) --resource=$(TXPREFIX)-$$rsrc tx-$(TXPREFIX)/$$rsrc.pot; \
	    if $(TX) push -s $(TXPROJ).$(TXPREFIX)-$$rsrc >/dev/null 2>/dev/null; then \
	      curl_output=`$(CURL) --request PATCH \
	                        -s -o /dev/null -w "%{http_code}" \
	                        --url $(TXHOST)/resources/o%3A$(TXORG)%3Ap%3A$(TXPROJ)%3Ar%3A$(TXPREFIX)-$$rsrc \
	                        --header 'Accept: application/vnd.api+json' \
	                        --header 'Authorization: Bearer $(TXCRED)' \
	                        --header 'Content-Type: application/vnd.api+json' \
	                        --data '{ "data": { "attributes": $(TXPROPS), \
	                                            "id": "o:$(TXORG):p:$(TXPREFIX):r:$(TXPREFIX)-$$rsrc", \
	                                            "type": "resources" } \
	                                }'`; \
	      rtn=$$?; \
	      if test $$rtn -ne 0 -o " $$curl_output" != " 200"; then echo "Warning. Failed to set properties: error code $$curl_output"; fi; \
	    fi; \
	  else \
	    echo "Updating $(TXPREFIX)-$$rsrc"; \
	    $(TX) push -s $(TXPROJ).$(TXPREFIX)-$$rsrc; \
	  fi; \
	fi; \
	rm -rf tx-$(TXPREFIX)

# Pull the translations from transifex.
#
# For each lang:
# - the translation is fetched from transifex
# - merged into the game's po file of that lang
# - merged with the game's pot file (to synchronize comments and file positions)
#
# Note: the translated strings of a transifex file will override the
# translated strings of the game's file.

tx-pull:
	@test " $(TXCRED)" = " " \
	 && { \
	      echo "You do not have a Transifex account"; \
	      exit 1; \
	    }; \
	$(mkdir_p) tx-$(TXPREFIX); \
	rsrc=$(TXRSRC); \
	echo "Pulling $$rsrc-<lang>.po from transifex"; \
	if $(TX) pull -l $(TXLANGS) $(TXPROJ).$(TXPREFIX)-$$rsrc; then \
	  catalogs='$(CATALOGS)'; \
	  for cat in $$catalogs; do \
	    cat=`basename $$cat`; \
	    lang=`echo $$cat | sed -e 's/\.gmo$$//'`; \
	    if test -r tx-$(TXPREFIX)/$$rsrc-$$lang.po; then \
	      echo "Merging with $$lang.po"; \
	      msgcat $(MSGCAT_OPTIONS) --use-first -o $$lang.po tx-$(TXPREFIX)/$$rsrc-$$lang.po $$lang.po; \
	    else \
	      echo "Nothing to merge"; \
	    fi; \
	    $(MSGMERGE_UPDATE) $(MSGMERGE_OPTIONS) $$lang.po $(DOMAIN).pot; \
	    $(call clean_po,$$lang.po); \
	  done; \
	fi; \
	rm -rf tx-$(TXPREFIX)

# Push the translations to transifex.
#
# For each lang:
# - the game's po is pushed to transifex

tx-push:
	@test " $(TXCRED)" = " " \
	 && { \
	      echo "You do not have a Transifex account"; \
	      exit 1; \
	    }; \
	test -e tx-push-granted \
	|| { \
	     echo "tx-push is a risky command."; \
	     echo "Create a 'tx-push-granted' file if you really want to execute it."; \
	     exit 1; \
	   }; \
	rm -f tx-push-granted; \
	$(mkdir_p) tx-$(TXPREFIX); \
	catalogs='$(CATALOGS)'; \
	rsrc=$(TXRSRC); \
	for cat in $$catalogs; do \
	  cat=`basename $$cat`; \
	  lang=`echo $$cat | sed -e 's/\.gmo$$//'`; \
	  echo "Pushing $$rsrc-$$lang.po to transifex"; \
	  cp $$lang.po tx/$$rsrc-$$lang.po; \
	  $(TX) push  -l $$lang -t $(TXPROJ).$(TXPREFIX)-$$rsrc; \
	done; \
	rm -rf tx-$(TXPREFIX)

.PHONY: l10n-stats l10n-translators
.PHONY: tx-update tx-pull tx-push
