###########################################################################
#
# This makefile fragment deals with generation and installation of message
# catalogs. It is a modified version of the Makefile.in.in distributed with
# GNU gettext (taken from version 0.10.35). It was modified to be used when
# distributing a package that contains several sub-packages and libraries,
# each with their own message catalogs, and each with support for different
# languages.
#
# Packages can have different directories or share the same one. This
# fragment assumes that the catalogs are in a `po' subdirectory of the
# source directory, but if this is not the case, set the `SOURCEDIR'
# directory to point to the sources.
#
# To use this, create a Makefile that looks like the following:
# 
# --cut-here--
# include <top directory>/config/config.mk
#
# LINGUAS = <set of languages supported by this package>
#
# # These are optional (see below)
# PACKAGE = <package name>
#
# POT_SOURCES = <base name of source files needing translation in `..'>
# POT_OTHER = <full name of other source files>
#
# include <top directory>/config/nls.mk
# --cut-here--
# 
# That's it. You may omit the PACKAGE variable if you have a file
# `package-info' in the source directory
#
###########################################################################

ifndef SOURCEDIR
SOURCEDIR = ..
endif

ifndef PACKAGE
include $(SOURCEDIR)/package-info
endif

ifndef LOCALEDIR
ifeq "@LOCALEDIR@" ""
LOCALEDIR = $(prefix)/locale
else
LOCALEDIR    = @LOCALEDIR@
endif
endif

GNU_LOCALEDIR = $(prefix)/share/locale

srcdir     = @srcdir@
top_srcdir = @TOPSRCDIR@

# Programs
GENCAT   = @GENCAT@
GMSGFMT  = @GMSGFMT@
MSGFMT   = @MSGFMT@
XGETTEXT = @XGETTEXT@
MSGMERGE = msgmerge

SOURCES   = cat-id-tbl.c
POFILES   = @POFILES@
GMOFILES  = @GMOFILES@

# Files for distribution
# DISTFILES = ChangeLog Makefile.in.in POTFILES.in $(PACKAGE).pot \
# stamp-cat-id $(POFILES) $(GMOFILES) $(SOURCES)

POTFILES = $(addprefix $(SOURCEDIR)/, $(POT_SOURCES)) $(POT_OTHER)

CATOBJEXT  = @CATOBJEXT@
INSTOBJEXT = @INSTOBJEXT@

CLEANABLE += core core.* *.pox $(PACKAGE).po *.old.po cat-id-tbl.tmp *.o
DISTCLEANABLE += *.mo *.msg *.cat *.cat.m
MAINCLEANABLE += $(GMOFILES)

CATALOGS := $(addsuffix $(CATOBJEXT), $(LINGUAS))

.SUFFIXES: .po .pox .gmo .mo .msg .cat

.po.pox: $(PACKAGE).pot
	# $(MAKE) $(PACKAGE).pot
	$(MSGMERGE) $< $(PACKAGE).pot -o $*.pox

.po.mo:
	$(MSGFMT) -o $@ $<

.po.gmo:
	file=$(srcdir)/`echo $* | sed 's,.*/,,'`.gmo \
	  && rm -f $$file && $(GMSGFMT) -o $$file $<

.po.cat:
	sed -f $(top_srcdir)/intl/po2msg.sed < $< > $*.msg \
	  && rm -f $@ && $(GENCAT) $@ $*.msg

.PHONY: all all-yes all-no
all: all-@USE_NLS@

all-yes: cat-id-tbl.c $(CATALOGS)
all-no:

$(PACKAGE).pot: $(POTFILES)
	@echo "Extracting messages from sources"
	@$(XGETTEXT) --default-domain=$(PACKAGE) \
	  --add-comments --keyword=_ --keyword=_G $(POTFILES) \
	&& test ! -f $(PACKAGE).po \
	   || ( rm -f $(srcdir)/$(PACKAGE).pot \
		&& mv $(PACKAGE).po $(srcdir)/$(PACKAGE).pot )

$(srcdir)/cat-id-tbl.c: stamp-cat-id; @:
$(srcdir)/stamp-cat-id: $(PACKAGE).pot
	@rm -f cat-id-tbl.tmp
	@sed -f $(top_srcdir)/intl/po2tbl.sed $(srcdir)/$(PACKAGE).pot \
		| sed -e "s/@PACKAGE NAME@/$(PACKAGE)/" > cat-id-tbl.tmp
	@if cmp -s cat-id-tbl.tmp $(srcdir)/cat-id-tbl.c; then \
	  rm cat-id-tbl.tmp; \
	else \
	  echo cat-id-tbl.c changed; \
	  rm -f $(srcdir)/cat-id-tbl.c; \
	  mv cat-id-tbl.tmp $(srcdir)/cat-id-tbl.c; \
	fi
	@cd $(srcdir) && rm -f stamp-cat-id && echo timestamp > stamp-cat-id

.PHONY: install install-exec install-data install-data-yes install-data-no
install: install-exec install-data
install-exec:
install-data: install-data-@USE_NLS@
install-data-no: all
install-data-yes: all
	@catalogs="$(CATALOGS)"; \
	for cat in $$catalogs; do \
	  cat=`basename $$cat`; \
	  case "$$cat" in \
	    *.gmo) destdir=$(GNU_LOCALEDIR);; \
	    *)     destdir=$(LOCALEDIR);; \
	  esac; \
	  lang=`echo $$cat | sed 's/\$(CATOBJEXT)$$//'`; \
	  dir=$$destdir/$$lang/LC_MESSAGES; \
	  $(MKINSTALLDIRS) $$dir; \
	  if test -r $$cat; then \
	    $(INSTALL_DATA) $$cat $$dir/$(PACKAGE)$(INSTOBJEXT); \
	    echo "installing $$cat as $$dir/$(PACKAGE)$(INSTOBJEXT)"; \
	  else \
	    $(INSTALL_DATA) $(srcdir)/$$cat $$dir/$(PACKAGE)$(INSTOBJEXT); \
	    echo "installing $(srcdir)/$$cat as" \
		 "$$dir/$(PACKAGE)$(INSTOBJEXT)"; \
	  fi; \
	  if test -r $$cat.m; then \
	    $(INSTALL_DATA) $$cat.m $$dir/$(PACKAGE)$(INSTOBJEXT).m; \
	    echo "installing $$cat.m as $$dir/$(PACKAGE)$(INSTOBJEXT).m"; \
	  else \
	    if test -r $(srcdir)/$$cat.m ; then \
	      $(INSTALL_DATA) $(srcdir)/$$cat.m \
		$$dir/$(PACKAGE)$(INSTOBJEXT).m; \
	      echo "installing $(srcdir)/$$cat as" \
		   "$$dir/$(PACKAGE)$(INSTOBJEXT).m"; \
	    else \
	      true; \
	    fi; \
	  fi; \
	done

.PHONY: uninstall
uninstall:
	catalogs='$(CATALOGS)'; \
	for cat in $$catalogs; do \
	  cat=`basename $$cat`; \
	  lang=`echo $$cat | sed 's/\$(CATOBJEXT)$$//'`; \
	  rm -f $(LOCALEDIR)/$$lang/LC_MESSAGES/$(PACKAGE)$(INSTOBJEXT); \
	  rm -f $(LOCALEDIR)/$$lang/LC_MESSAGES/$(PACKAGE)$(INSTOBJEXT).m; \
	  rm -f $(GNU_LOCALEDIR)/$$lang/LC_MESSAGES/$(PACKAGE)$(INSTOBJEXT); \
	  rm -f $(GNU_LOCALEDIR)/$$lang/LC_MESSAGES/$(PACKAGE)$(INSTOBJEXT).m; \
	done

.PHONY: check
check: all

cat-id-tbl.o: $(top_srcdir)/intl/libgettext.h

.PHONY: update-po
update-po: Makefile $(PACKAGE).pot
	cd $(srcdir); \
	catalogs='$(CATALOGS)'; \
	for cat in $$catalogs; do \
	  cat=`basename $$cat`; \
	  lang=`echo $$cat | sed 's/\$(CATOBJEXT)$$//'`; \
	  mv $$lang.po $$lang.old.po; \
	  echo "$$lang:"; \
	  if $(MSGMERGE) $$lang.old.po $(PACKAGE).pot -o $$lang.po; then \
	    rm -f $$lang.old.po; \
	  else \
	    echo "msgmerge for $$cat failed!"; \
	    rm -f $$lang.po; \
	    mv $$lang.old.po $$lang.po; \
	  fi; \
	done

ifdef SUBDIR
Makefile: Makefile.in
	@cd $(TOPSRCDIR) && \
	  CONFIG_FILES=$(SUBDIR)/po/$@ CONFIG_HEADERS= \
	  ./config.status
endif

# Tell versions [3.59,3.63) of GNU make not to export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:

include $(FRAGDIR)/targets.mk