MOD_SOURCES = $(shell echo *.mod.xml)
MOD_MANPAGES = $(patsubst %.mod.xml,%.7ctrlproxy,$(MOD_SOURCES))
MOD_MANPAGES_HTML = $(patsubst %.mod.xml,%.7ctrlproxy.html,$(MOD_SOURCES))
MODS = $(patsubst %.mod.xml,%.xml,$(MOD_SOURCES))
DOCS = ctrlproxy.1 ctrlproxyrc.5 ctrlproxy.pdf ctrlproxy.html $(MOD_MANPAGES)
XSLTPROC = @XSLTPROC@ --xinclude
PDFLATEX = @PDFLATEX@
XMLTO = @XMLTO@
docdir = @prefix@/share/doc/ctrlproxy
INSTALL = @INSTALL@
XMLLINT = @XMLLINT@
prefix = @prefix@
mandir = @mandir@
man1dir = @mandir@/man1
man5dir = @mandir@/man5
man7dir = @mandir@/man7
@NODB2LATEX@DB2LATEXPATH = @DB2LATEXPATH@
DIA = @DIA@
@DOCBUILDE@ENABLED_DOC_BUILD = 1
@DOCINSTALLE@ENABLED_DOC_INSTALL = 1
EPSTOPDF = @EPSTOPDF@

all: @DOCBUILDE@$(DOCS)

manpages: ctrlproxy.1 ctrlproxyrc.5 $(MOD_MANPAGES)
htmlmanpages: $(MOD_MANPAGES_HTML)

%.7ctrlproxy.xml: %.mod.xml module-man.xsl
	$(XSLTPROC) -o $@ module-man.xsl $<

%.xml: %.mod.xml module.xsl
	$(XSLTPROC) -o $@ module.xsl $<
	
%.fo: %.xml $(MODS)
	$(XMLTO) fo $<

%.7ctrlproxy.html: %.7ctrlproxy.xml
	$(XMLTO) html-nochunks $<

%.html: %.xml $(MODS) overview.png overview2.png
	$(XMLTO) html-nochunks $<

%.ps: %.xml $(MODS) overview.eps overview2.eps
	$(XMLTO) ps $<

lint: ctrlproxy.xml $(MODS)
	$(XMLLINT) --xinclude ctrlproxy.xml

install: all
ifeq ($(ENABLED_DOC_INSTALL), 1)
	$(INSTALL) -d $(DESTDIR)$(man1dir)
	$(INSTALL) -d $(DESTDIR)$(man5dir)
	$(INSTALL) -d $(DESTDIR)$(man7dir)
	$(INSTALL) -d $(DESTDIR)$(docdir)
	$(INSTALL) ctrlproxy.1 $(DESTDIR)$(man1dir)
	$(INSTALL) ctrlproxyrc.5 $(DESTDIR)$(man5dir)
	$(INSTALL) $(MOD_MANPAGES) $(DESTDIR)$(man7dir)
	$(INSTALL) ctrlproxy.pdf $(DESTDIR)$(docdir)
	$(INSTALL) ctrlproxy.html $(DESTDIR)$(docdir)
else
	@echo
	@echo "---------------------------------------------------------------"
	@echo "You are using ctrlproxy from CVS and have not enabled the docs"
	@echo "(./configure --enable-docs). No documentation will be installed"
	@echo "---------------------------------------------------------------"
	@echo
endif

%.1: %.1.xml
	$(XMLTO) man $<

%.7ctrlproxy: %.7ctrlproxy.xml
	$(XMLTO) man $<

%.5: %.5.xml
	$(XMLTO) man $<

ifdef DB2LATEXPATH

%.tex: %.xml $(MODS)
	$(XSLTPROC) --stringparam l10n.gentext.default.language en --stringparam latex.documentclass.common english --stringparam latex.babel.language english $(DB2LATEXPATH) $< > $@

%.pdf: %.tex overview.png overview2.png
	$(PDFLATEX) $<
	$(PDFLATEX) $<
	$(PDFLATEX) $<

else 

%.pdf: %.xml $(MODS) overview.pdf overview2.pdf
	$(XMLTO) pdf $<
endif

%.png: %.dia
	$(DIA) -n -e $@ $<

%.eps: %.dia
	$(DIA) -n -e $@ $<

%.pdf: %.eps
	$(EPSTOPDF) $<

clean: 
@DOCBUILDE@	rm -f *.html *.pdf $(DOCS) $(MODS) overview{2,}.{eps,png,pdf}

distclean: clean
	rm -f Makefile
