# vim:set ts=2:noet:
include ../common.mk
bdir=generated

.PHONY: all install clean

all: $(bdir)/manpages.stamp

install:: $(bdir)/manpages.stamp
	for man in $(bdir)/apt-listchanges*.1; do \
		test -s "$$f"; \
		lang=`echo $$man | sed -e 's/^.*apt-listchanges\.*// ; s/\.*1//'`; \
		install -d $(DESTDIR)/usr/share/man/$$lang/man1;                \
		install -m 644 $$man $(DESTDIR)/usr/share/man/$$lang/man1/apt-listchanges.1; \
	done

install:: $(bdir)/what_to_display.html
	mkdir -p $(DESTDIR)/usr/share/doc/apt-listchanges
	install -m 444 $< $(DESTDIR)/usr/share/doc/apt-listchanges/

$(bdir)/what_to_display.html: design_notes/what_to_display.md
	rm -f $@.tmp
	markdown $< > $@.tmp
	mv $@.tmp $@

clean:
	rm -rf $(bdir) mantmp

PO4A := PERL_HASH_SEED=0 PERL_PERTURB_KEYS=0 po4a $(XGETTEXT_COMMON_OPTIONS) --previous

# params: input output

# Note that the output file name passed into this function needs to match the
# output file name determined automatically by xsltproc.
#
# We are creating the output file in a temporary directory and then
# transforming it into its final location with a sed command because there is
# a bug in the DocBook XSL stylesheets which cause the apostrophes in section
# headers to be turned into invalid macros. First, the stylesheet converts the
# apostrophe into \*(Aq, which is a valid macro, but then it upper-cases the
# entire header, turning the macro into \*(AQ, which is invalid. See
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1082554.
define xml2man
	set -- xmllint --valid --noout --noent --nonet "$(1)"; \
	echo "$$@"; \
	"$$@"; \
	lang="`echo "$(1)" | sed -e 's/^.*apt-listchanges//; s/\.xml$$// ;s/^\.//'`"; \
	[ "$$lang" ] && langparam="--stringparam l10n.gentext.default.language $$lang" || langparam=""; \
	set -- xsltproc --nonet --xinclude \
						$$langparam \
						--stringparam man.authors.section.enabled 0 \
						--stringparam man.output.lang.in.name.enabled 1 \
						--stringparam man.charmap.enabled 0 \
						--stringparam man.hyphenate 0 \
						--stringparam man.justify 0 \
						--output "mantmp/" \
					/usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl \
					"$(1)"; \
	echo "$$@"; \
	"$$@"; \
	sed -e 's/\\\*(AQ/\\*(Aq/g' < "mantmp/$(2)" > "$(bdir)/$(2).tmp"; \
	mv "$(bdir)/$(2).tmp" "$(bdir)/$(2)"; \
	rm -f "mantmp/$(2)"; \
	echo
endef


update-po:
	$(PO4A) --force -v po4a.cfg

$(bdir)/po4a.stamp:  apt-listchanges.xml po/*.po po/*.add po4a.cfg
	$(PO4A)  -v po4a.cfg
	touch "$@"

$(bdir)/manpages.stamp: $(bdir)/po4a.stamp
	mkdir -p mantmp
	@ set -e; \
	for f in apt-listchanges.xml $(bdir)/apt-listchanges.*.xml; do \
		test -f "$$f" || continue; \
		g="`basename $$f .xml`.1"; \
		$(call xml2man,$$f,$$g) ;  \
	done
	rm -rf mantmp
	touch "$@"
