# $Id: Makefile 48743 2009-03-28 02:55:10Z raimue@macports.org $

# Makefile to generate the MacPorts HTML guide and the man pages.
# The ports 'docbook-xsl', 'docbook-xml' and 'libxslt' have to be installed.

# If your MacPorts isn't installed in /opt/local you have to change PREFIX
# here.


# Prefix of the MacPorts installation.
PREFIX = /opt/local

# Command abstraction variables.
MKDIR    = /bin/mkdir
CP       = /bin/cp
RM       = /bin/rm
LN       = /bin/ln
SED      = /usr/bin/sed
TCLSH    = /usr/bin/tclsh
XSLTPROC = $(PREFIX)/bin/xsltproc
XMLLINT  = $(PREFIX)/bin/xmllint

# Data directories.
GUIDE = guide
MAN   = man
# Source directories.
GUIDE_SRC = $(GUIDE)/xml
MAN_SRC   = $(MAN)/xml
# Result directories.
GUIDE_RESULT       = $(GUIDE)/html
GUIDE_RESULT_CHUNK = $(GUIDE_RESULT)/chunked
MAN_RESULT         = $(MAN)/man/
# Man temporary directory.
MAN_TMP = $(MAN)/tmp

# Path to the DocBook XSL files.
DOCBOOK         = $(PREFIX)/share/xsl/docbook-xsl
GUIDE_XSL       = $(GUIDE)/resources/single-page.xsl
GUIDE_XSL_CHUNK = $(GUIDE)/resources/chunk.xsl
MAN_XSL         = $(MAN)/resources/macports.xsl

# DocBook HTML stylesheet for the guide.
STYLESHEET = docbook.css

.PHONY: all guide guide-chunked man clean validate

all: guide guide-chunked man

# Generate the HTML guide using DocBook from the XML sources in $(GUIDE_SRC).
guide:
	$(MKDIR) -p $(GUIDE_RESULT)
	$(CP) $(GUIDE)/resources/$(STYLESHEET) $(GUIDE_RESULT)/$(STYLESHEET)
	$(CP) $(GUIDE)/resources/images/* $(GUIDE_RESULT)/
	$(LN) -sfh $(DOCBOOK) $(GUIDE)/resources/xsl
	$(XSLTPROC) --xinclude \
	    --output $(GUIDE_RESULT)/index.html \
	    $(GUIDE_XSL) $(GUIDE_SRC)/guide.xml
	# Convert all sections (h1-h9) to a link so it's easy to link to them.
	# If someone knows a better way to do this please change it.
	$(SED) -i "" -E \
	    's|(<h[0-9] [^>]*><a id="([^"]*)"></a>)([^<]*)(</h[0-9]>)|\1<a href="#\2">\3</a>\4|g' \
	    $(GUIDE_RESULT)/index.html

# Generate the chunked HTML guide with one section per file.
guide-chunked:
	$(MKDIR) -p $(GUIDE_RESULT_CHUNK)
	$(CP) $(GUIDE)/resources/$(STYLESHEET) $(GUIDE_RESULT_CHUNK)/$(STYLESHEET)
	$(CP) $(GUIDE)/resources/images/* $(GUIDE_RESULT_CHUNK)/
	$(LN) -sfh $(DOCBOOK) $(GUIDE)/resources/xsl
	$(XSLTPROC) --xinclude \
	    --output $(GUIDE_RESULT_CHUNK)/index.html \
	    $(GUIDE_XSL_CHUNK) $(GUIDE_SRC)/guide.xml
	# Convert all sections (h1-h9) to a link so it's easy to link to them.
	# If someone knows a better way to do this please change it.
	$(SED) -i "" -E \
	    's|(<h[0-9] [^>]*><a id="([^"]*)"></a>)([^<]*)(</h[0-9]>)|\1<a href="#\2">\3</a>\4|g' \
	    $(GUIDE_RESULT_CHUNK)/*.html
	# Add the table of contents to every junked HTML file.
	# If someone knows a better way to do this please change it.
	$(TCLSH) toc-for-chunked.tcl $(GUIDE_RESULT_CHUNK)


# Generate the man pages using DocBook from the XML source in $(MAN_SRC).
# The portfile-*.xml and portgroup-*.xml files in $(GUIDE_SRC) are copied to
# $(MAN_TMP) and modified (section -> refsection) so they can be used as man
# XML source files and then xincluded in the real man XML files in $(MAN_SRC).
man: $(MAN_XSL)
	$(MKDIR) -p $(MAN_RESULT)
	$(MKDIR) -p $(MAN_TMP)
	$(CP) $(GUIDE_SRC)/portfile-*.xml $(MAN_TMP)
	$(CP) $(GUIDE_SRC)/portgroup-*.xml $(MAN_TMP)
	$(SED) -i "" 's|<section|<refsection|g' $(MAN_TMP)/*
	$(SED) -i "" 's|</section>|</refsection>|g' $(MAN_TMP)/*
	$(XSLTPROC) --xinclude --output $(MAN_RESULT) $(MAN_XSL) \
	    $(MAN_SRC)/port.1.xml \
	    $(MAN_SRC)/portfile.7.xml \
	    $(MAN_SRC)/portgroup.7.xml \
	    $(MAN_SRC)/porthier.7.xml
	$(RM) -r $(MAN_TMP)

# Create XSL from template for man pages.
$(MAN_XSL):
	$(SED) 's:@PREFIX@:$(PREFIX):' $@.in > $@

# Remove all temporary files generated by guide: and man:.
clean:
	$(RM) -rf $(GUIDE)/resources/xsl
	$(RM) -rf $(GUIDE_RESULT)
	$(RM) -rf $(MAN_RESULT)
	$(RM) -rf $(MAN_TMP)
	$(RM) -rf $(MAN_XSL)
	$(RM) -f  guide.tmp.xml

# Validate the XML files for the guide.
# These two steps are necessary as otherwise xmllint complains about missing
# ids.
validate:
	$(XMLLINT) --xinclude $(GUIDE_SRC)/guide.xml > guide.tmp.xml
	$(XMLLINT) --loaddtd --valid --noout guide.tmp.xml
