SHELL=@SHELL@
prefix=@prefix@
mandir=$(DESTDIR)@mandir@
INSTALL=@INSTALL@
PACKAGE_NAME=@PACKAGE_NAME@
MANCAT=1
MAN=$(PACKAGE_NAME).$(MANCAT)
MAN_LANGS=$(patsubst $(PACKAGE_NAME).%.xml,%,$(wildcard $(PACKAGE_NAME).??.xml))
MAN_I18N=$(patsubst %,$(PACKAGE_NAME).%.$(MANCAT),$(MAN_LANGS))

#Use db2x_* directly, because docbook2x-0.8.5 is broken
DB2X_XSLTPROC=@DB2X_XSLTPROC@
DB2X_MANXML=@DB2X_MANXML@
XSLTPROC=@XSLTPROC@
#those are only used for xsltproc
XSL_MAN=http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl
XSL_MAN_UTF8=docbook-man-utf8.xsl
XSL_HTML=http://docbook.sourceforge.net/release/xsl/current/xhtml/docbook.xsl

.PHONY: all
all: man-doc html-doc

.PHONY: man-doc
man-doc: $(MAN_I18N)

# If doxbook2x programs are not present, try the N. Walsh stylesheets.
# The xsltproc --output option is broken/ignored (probably forced by the XSL file)
# output always goes to $(MAN) as specified by the XML file

%.ru.$(MANCAT): %.ru.xml
	@-( test "$(DB2X_XSLTPROC)" && test "$(DB2X_MANXML)" && \
	    echo "docbook2x: $< -> $@" && \
	    $(DB2X_XSLTPROC) -s man $< | $(DB2X_MANXML) --encoding=UTF-8 --to-stdout >$@ ) || \
	  ( ( rm $(MAN) 2>/dev/null || true ) && \
	    test ! "$(DB2X_XSLTPROC)" && test ! "$(DB2X_MANXML)" && test "$(XSLTPROC)" && \
	    echo "xsltproc: $< -> $@" && \
	    $(XSLTPROC) $(XSL_MAN_UTF8) $< && test -f $(MAN) && mv $(MAN) $@ )

%.$(MANCAT): %.xml
	@-( test "$(DB2X_XSLTPROC)" && test "$(DB2X_MANXML)" && \
	    echo "docbook2x: $< -> $@" && \
	    $(DB2X_XSLTPROC) -s man $< | $(DB2X_MANXML) --to-stdout >$@ ) || \
	  ( ( rm $(MAN) 2>/dev/null || true ) && \
	    test ! "$(DB2X_XSLTPROC)" && test ! "$(DB2X_MANXML)" && test "$(XSLTPROC)" && \
	    echo "xsltproc: $< -> $@" && \
	    $(XSLTPROC) $(XSL_MAN) $< && test -f $(MAN) && mv $(MAN) $@ )

.PHONY: html-doc
html-doc: $(patsubst %,$(PACKAGE_NAME).%.html,$(MAN_LANGS))

%.html: %.xml
#default output for HTML is to stdout
	@-test "$(XSLTPROC)" && \
	  echo "xsltproc: $< -> $@" && \
	  $(XSLTPROC) $(XSL_HTML) $< >$@

.PHONY: clean
clean:
	@echo "To remove the troff and html files, run the distclean make target."

.PHONY: distclean
distclean:
	rm -f $(patsubst %,$(PACKAGE_NAME).%.1,$(MAN_LANGS));
	rm -f $(patsubst %,$(PACKAGE_NAME).%.html,$(MAN_LANGS));

.PHONY: install-$(PACKAGE_NAME).en.$(MANCAT)
install-$(PACKAGE_NAME).en.$(MANCAT): $(PACKAGE_NAME).en.$(MANCAT)
	$(INSTALL) -d $(mandir)/man$(MANCAT); \
	$(INSTALL) -m 644 $(PACKAGE_NAME).en.$(MANCAT) $(mandir)/man$(MANCAT)/$(MAN); \

.PHONY: install-$(PACKAGE_NAME).%.$(MANCAT)
install-$(PACKAGE_NAME).%.$(MANCAT): $(PACKAGE_NAME).%.$(MANCAT)
	$(INSTALL) -d $(mandir)/$*/man$(MANCAT); \
	$(INSTALL) -m 644 $(PACKAGE_NAME).$*.$(MANCAT) $(mandir)/$*/man$(MANCAT)/$(MAN); \

.PHONY: install
install: $(patsubst %,install-$(PACKAGE_NAME).%.$(MANCAT),$(MAN_LANGS))

.PHONY: uninstall
uninstall:
	-rm -f $(mandir)/man$(MANCAT)/$(MAN)
	-rm -f $(mandir)/*/man$(MANCAT)/$(MAN);
