# Makefile.in for PO files of yash
# (C) 2007-2012 magicant
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# NOTE: In this Makefile it is assumed that the make implementation allows the
# use of hyphens in target names. This means that there may be a strictly
# POSIX-conforming implementation of make that rejects this Makefile. I have
# never seen such an implementation but if you know of one please let me know.

.POSIX:
.SUFFIXES: .po .mo .ih

@MAKE_SHELL@
topdir = ..
subdir = po

XGETTEXT = @XGETTEXT@
XGETTEXTFLAGS = @XGETTEXTFLAGS@
MSGINIT = @MSGINIT@
MSGFMT = @MSGFMT@
MSGFMTFLAGS = @MSGFMTFLAGS@
MSGMERGE = @MSGMERGE@
MSGMERGEFLAGS = @MSGMERGEFLAGS@
MSGCONV = @MSGCONV@
MSGFILTER = @MSGFILTER@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_DIR = @INSTALL_DIR@
POFILES_MAN = ja.po
POFILES_QUOT = en@quot.po en@boldquot.po
POFILES = $(POFILES_MAN) $(POFILES_QUOT)
MOFILES = $(POFILES:.po=.mo)
CATALOGS = @CATALOGS@
DOMAIN = @TARGET@
VERSION = @VERSION@
COPYRIGHT_HOLDER = Magicant

DESTDIR =
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
datarootdir = @datarootdir@
datadir = @datadir@
yashdatadir = $(datadir)/$(TARGET)
localedir = @localedir@
mandir = @mandir@
docdir = @docdir@
htmldir = @htmldir@

# stamp-po is a timestamp denoting the last time at which the $(CATALOGS) have
# been loosely updated. Its purpose is that when a developer or translator
# checks out the package via VCS, "make" will update the $(DOMAIN).pot and the
# $(CATALOGS), but subsequent invocations of "make" will do nothing. This
# timestamp would not be necessary if updating the $(CATALOGS) would always
# touch them; however, the rule for $(POFILES) has been designed to not touch
# files that don't need to be changed.
stamp-po: $(DOMAIN).pot
	@+[ -z "$(CATALOGS)" ] || $(MAKE) $(CATALOGS)
	touch $@

# This rule has no dependencies: we don't need to update $(DOMAIN).pot at
# every "make" invocation, only create it when it is missing.
# Only "make $(DOMAIN).pot-update", "make update-po" or "make dist" will force
# an update.
$(DOMAIN).pot: 
	@+$(MAKE) $(DOMAIN).pot-update

# This target rebuilds $(DOMAIN).pot; it is an expensive operation.
$(DOMAIN).pot-update:
	(cd $(topdir) && find . -name '*.[ch]') | \
	$(XGETTEXT) -d $(DOMAIN) -D $(topdir) -cTRANSLATORS: -f - -F --no-wrap \
		--package-name=$(DOMAIN) --package-version=$(VERSION) \
		--msgid-bugs-address=https://github.com/magicant/yash/issues \
		--copyright-holder='$(COPYRIGHT_HOLDER)' $(XGETTEXTFLAGS)
	mv $(DOMAIN).po $(DOMAIN).pot

# This target rebuilds a PO file if $(DOMAIN).pot has changed.
# Note that a PO file is not touched if it doesn't need to be changed.
$(POFILES_MAN): $(DOMAIN).pot
	$(MSGMERGE) -FU $(MSGMERGEFLAGS) $@ $(DOMAIN).pot

# This target creates PO files that are automatically created from the
# $(DOMAIN).pot file. This requires GNU sed (or a compatible one) that doesn't
# automatically append a missing newline at the end of input.
en@quot.po: $(DOMAIN).pot en@quot.ih en@quot.hd
	$(MSGINIT) -i $(DOMAIN).pot --no-translator -l en@quot -o - 2>/dev/null | \
	sed -f en@quot.ih | \
	$(MSGCONV) -t UTF-8 | \
	$(MSGFILTER) sed -f quot.sed >$@
en@boldquot.po: $(DOMAIN).pot en@boldquot.ih en@boldquot.hd
	$(MSGINIT) -i $(DOMAIN).pot --no-translator -l en@boldquot -o - 2>/dev/null | \
	sed -f en@boldquot.ih | \
	$(MSGCONV) -t UTF-8 | \
	$(MSGFILTER) sed -f boldquot.sed >$@
$(POFILES_QUOT:.po=.ih): insert-hd.sin
	target='$@'; lang=$${target%.*}; \
	sed -e '/^#/d' -e "s/HEADER/$${lang}.hd/g" insert-hd.sin >$@

# This target updates all PO files as well as $(DOMAIN).pot.
update-po: $(DOMAIN).pot-update
	@+$(MAKE) $(POFILES)

.po.mo:
	$(MSGFMT) $(MSGFMTFLAGS) -o t-$@ $<
	mv -f t-$@ $@

install: install-data
install-data: installdirs-data stamp-po
	@for lang in $(CATALOGS:.mo=); do \
		dir=$(localedir)/$$lang/LC_MESSAGES; \
		echo $(INSTALL_DATA) $$lang.mo $(DESTDIR)$$dir/$(DOMAIN).mo || true; \
		$(INSTALL_DATA) $$lang.mo $(DESTDIR)$$dir/$(DOMAIN).mo; \
	done
installdirs: installdirs-data
installdirs-data:
	@for lang in $(CATALOGS:.mo=); do \
		dir=$(localedir)/$$lang/LC_MESSAGES; \
		echo $(INSTALL_DIR) $(DESTDIR)$$dir || true; \
		$(INSTALL_DIR) $(DESTDIR)$$dir; \
	done
uninstall: uninstall-data
uninstall-data:
	@for lang in $(CATALOGS:.mo=); do \
		dir=$(localedir)/$$lang/LC_MESSAGES; \
		echo rm -f $(DESTDIR)$$dir/$(DOMAIN).mo || true; \
		rm -f $(DESTDIR)$$dir/$(DOMAIN).mo; \
	done

DISTTARGETFILES = $(DOMAIN).pot $(POFILES) $(MOFILES) Makefile.in \
	quot.sed boldquot.sed en@quot.ih en@quot.hd \
	en@boldquot.ih en@boldquot.hd insert-hd.sin
DISTFILES = $(DISTTARGETFILES) stamp-po
distfiles: $(DISTTARGETFILES)
	touch stamp-po
copy-distfiles: distfiles
	mkdir -p $(topdir)/$(DISTTARGETDIR)
	cp $(DISTFILES) $(TEST_SOURCES) $(topdir)/$(DISTTARGETDIR)

mostlyclean:
	rm -fr $(DOMAIN).po
clean: mostlyclean
distclean: clean
	rm -fr Makefile
maintainer-clean: distclean
	rm -fr $(DOMAIN).pot stamp-po $(POFILES_QUOT) $(POFILES_QUOT:.po=.ih) $(MOFILES)

Makefile: Makefile.in $(topdir)/config.status
	@+(cd $(topdir) && $(MAKE) config.status)
	@(cd $(topdir) && $(SHELL) config.status $(subdir)/$@)

.PHONY: $(DOMAIN).pot-update update-po install install-data installdirs installdirs-data uninstall uninstall-data distfiles copy-distfiles mostlyclean clean distclean maintainer-clean
