# Additional make targets

.NOTPARALLEL:
DISTFILES.extra2 = Rules-others

#== 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

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

update-po: update-pot
update-pot:
	@if [ -x $(top_srcdir)/po/update-potfiles ] ; then \
	   $(top_srcdir)/po/update-potfiles -i map -x map/script_helpers.lua > 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:
	@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

.PHONY: local-install update-pot

#== Transifex synchronization

TXPROJ   = freedroid-rpg
TXFILES  = $(shell grep -v -E "^\#" POTFILES.in)
TXPREFIX = data
TXPROPS  = '{"category":"data"}'
TXCRED   = $(shell $(top_srcdir)/po/tx-credentials)

# Update the transifex potfiles
#
# The single game's pot file is split into individual pot files (as defined by TXFILES),
# and for each individual file:
# - 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; \
	files='$(TXFILES)'; \
	for file in $$files; do \
	  base=`basename $$file`; \
	  rsrc=`echo $$base | sed -e 's/\..*$$//'`; \
	  msggrep $(MSGCAT_OPTIONS) --location=$$file -o tx/$$rsrc.pot $(DOMAIN).pot; \
	  if test -r tx/$$rsrc.pot; then \
	    if ! tx -q status -r $(TXPROJ).$(TXPREFIX)-$$rsrc 2&>/dev/null; then \
	      echo "Creating $(TXPREFIX)-$$rsrc"; \
	      tx -q set --auto-local -r $(TXPROJ).$(TXPREFIX)-$$rsrc "tx/$$rsrc-<lang>.po" -t PO --source-lang en --source-file tx/$$rsrc.pot --execute; \
	      if tx -q push -r $(TXPROJ).$(TXPREFIX)-$$rsrc -s; then \
	        curl_output=`curl -s -L $(TXCRED) -X PUT -d $(TXPROPS) -H "Content-Type: application/json" https://www.transifex.com/api/2/project/$(TXPROJ)/resource/$(TXPREFIX)-$$rsrc;` \
	        rtn=$$?; \
	        if test $$rtn -ne 0 -o " $$curl_output" != " OK"; then echo "Warning. Failed to set properties: $$curl_output (error code $$rtn)"; fi; \
	      fi; \
	    else \
	      echo "Updating $(TXPREFIX)-$$rsrc"; \
	      tx -q push -r $(TXPROJ).$(TXPREFIX)-$$rsrc -s; \
	    fi; \
	  fi; \
	done; \
	rm -rf tx

# Pull the translations from transifex.
#
# For each lang and each individual pot file (as defined by TXFILES):
# - 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; \
	catalogs='$(CATALOGS)'; \
	files='$(TXFILES)'; \
	for cat in $$catalogs; do \
	  cat=`basename $$cat`; \
	  lang=`echo $$cat | sed -e 's/\.gmo$$//'`; \
	  for file in $$files; do \
	    base=`basename $$file`; \
	    rsrc=`echo $$base | sed -e 's/\..*$$//'`; \
	    echo "Pulling $$rsrc-$$lang.po from transifex"; \
	    if tx -q pull -r $(TXPROJ).$(TXPREFIX)-$$rsrc -l $$lang; then \
	      if test -r tx/$$rsrc-$$lang.po; then \
	        echo "Merging with $$lang.po"; \
	        msgcat $(MSGCAT_OPTIONS) --use-first -o $$lang.po tx/$$rsrc-$$lang.po $$lang.po; \
	      else \
	        echo "Nothing to merge"; \
	      fi; \
	    fi; \
	  done; \
	  $(MSGMERGE_UPDATE) $(MSGMERGE_OPTIONS) $$lang.po $(DOMAIN).pot; \
	done; \
	rm -rf tx

# Push the translations to transifex.
#
# For each lang:
# - the game's po file is split into individual po files (as defined by TXFILES)
# - for each individual po file:
#   - the po file is pushed to transifex

tx-push:
	@test " $(TXCRED)" = " " \
	 && { \
	      echo "You do not have a Transifex account"; \
	      exit 1; \
	    }; \
	$(mkdir_p) tx; \
	catalogs='$(CATALOGS)'; \
	files='$(TXFILES)'; \
	for cat in $$catalogs; do \
	  cat=`basename $$cat`; \
	  lang=`echo $$cat | sed -e 's/\.gmo$$//'`; \
	  for file in $$files; do \
	    base=`basename $$file`; \
	    rsrc=`echo $$base | sed -e 's/\..*$$//'`; \
	    msggrep $(MSGCAT_OPTIONS) --location=$$file -o tx/$$rsrc-$$lang.po $$lang.po; \
	    echo "Pushing $$rsrc-$$lang.po to transifex"; \
	    tx -q push -r $(TXPROJ).$(TXPREFIX)-$$rsrc -l $$lang -t; \
	  done; \
	done; \
	rm -rf tx

.PHONY: tx-update tx-pull tx-push
