# For the following variable, if its value is set in the environment,
# use it.  Otherwise, use value specified below.  Note that if the
# default doesn't work, you must specify an override value in the
# environment. For example, you'll likely need to provide a catalog
# file elsewhere (such as in your home directory) that provides
# pathnames that are correct for your system.

XML_CATALOG_FILES ?= ./catalog.xml

# This string must be mapped in your XML catalog file to the root
# directory of your local DocBook XSL files.  For example, you might
# have a catalog entry that looks like this:
# <rewriteSystem
#   systemIdStartString="docbook://xsl-current/"
#   rewritePrefix="file:///usr/local/sgml/docbook-xsl-1.69.1/" />

DOCBOOK_XSL = docbook://xsl-current


LN = ln -s -f
RM = rm -f

XML_VALIDATE = ./xml-validate

# Basename of main document (used for default targets of common ops)

MAINDOC_BASE = xmlformat

all::
	@echo "You must say what you want to build"
	@echo "make dist-prep: build documents for distribution"
	@echo "make doc.valid: valid XML source document"
	@echo "make doc.html: generate HTML version of document"
	@echo "make doc.pdf: generate PDF version of document"

# Generate documents in output formats that are included in distributions

dist-prep::
	make xmlformat.html xmlformat.html-chunk xmlformat.pdf
	make tutorial.html tutorial.html-chunk tutorial.pdf

valid:: $(MAINDOC_BASE).valid
html:: $(MAINDOC_BASE).html
pdf:: $(MAINDOC_BASE).pdf

# reformat document
reformat::
	../xmlformat.rb -i -b.bak $(MAINDOC_BASE).xml

# show diff between old and previous versions
diff::
	-diff -u $(MAINDOC_BASE).xml.bak $(MAINDOC_BASE).xml

all:: $(OUTPUTS) 

%.html: %.xml
	xsltproc \
		--novalid \
		--param generate.section.toc.level 2 \
		--param section.autolabel 1 \
		--param section.label.includes.component.label 1 \
		--output $@ $(DOCBOOK_XSL)/html/docbook.xsl \
		$<

%.html-chunk: %.xml
	@-mkdir $@/
	xsltproc \
		--novalid \
		--param generate.section.toc.level 8 \
		--param section.autolabel 1 \
		--param section.label.includes.component.label 1 \
		--param use.id.as.filename 1 \
		--param chunk.quietly 1 \
		--param chunk.first.sections 1 \
		--param chunk.fast 1 \
		--stringparam chunker.output.encoding "utf-8" \
		--output $@/ $(DOCBOOK_XSL)/html/chunkfast.xsl \
		$<

%.txt: %.html
	lynx -dump $< > $@

# for page margins, note that you can't set left and right as they
# will be overwritten. The default value is 1in for both.

%.fo: %.xml
	xsltproc \
		--novalid \
		--param fop.extensions 1 \
		--param section.autolabel 1 \
		--param section.label.includes.component.label 1 \
		--stringparam page.margin.inner "1in" \
		--stringparam page.margin.outer "1in" \
		--stringparam draft.mode "no" \
		--output $@ $(DOCBOOK_XSL)/fo/docbook.xsl \
		$<

%.pdf: %.fo
	fop -q $< $@

# reformat document
%.format:: %.xml
	../xmlformat.rb -i -b.bak $<

# show diff between old and previous versions
%.diff:: %.xml
	-diff -u $<.bak $<

# validate input files
%.valid: %.xml
	$(XML_VALIDATE) $<

clean:
	$(RM) *.fo *.pdf *.html
	$(RM) -R *.html-chunk

realclean: clean
	$(RM) *.bak
