# $Source$
# $Author: xmldoc $
# $Date: 2007-03-04 22:28:18 -0800 (Sun, 04 Mar 2007) $
# $Revision: 6666 $
# vim: number
#
# -----------------------------------------------------------------
#  ** Makefile.combine -- combine source files **
# -----------------------------------------------------------------
#
#   This file is part of the DocBook Project XSL Stylesheet
#   distribution.
#
#   See http://docbook.sourceforge.net/release/xsl/current/
#   for copyright and other information.
#
# This makefile creates "wrapper" files that combine sets of
# individual DocBook source files. The purpose of combining the
# files is to speed up processing time. By default it puts 20
# files into each wrapper. Use CHUNKSIZE to configure the number
# of files per wrapper.
#
# Currently, this makefile has only a "man" target and is mainly
# intended to speed up processing of large numbers of individual
# refentry instances.

# What file extension do you use for DocBook source files?
DOCBOOK_FILE_EXTENSION = .xml
SOURCE_FILES_DBK = $(wildcard *$(DOCBOOK_FILE_EXTENSION))

MAKEFILE_DOCBOOK = Makefile.DocBook

XSLTPROC=xsltproc
XSLTPROC_FLAGS=

SED=sed
SED_FLAGS=-i

CHUNKSIZE ?= 20

WRAPPER_ELEMENT = reference
WRAPPER_TITLE=Combined contents

COMBINE_XSL = <?xml version="1.0"?> \
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" \
                xmlns:exsl="http://exslt.org/common" \
                xmlns:xi="http://www.w3.org/2001/XInclude" \
                exclude-result-prefixes="exsl xi" \
                extension-element-prefixes="exsl" \
                version="1.0"> \
  <xsl:param name="files"/> \
  <xsl:param name="chunk.size"/> \
  <xsl:template match="/"> \
    <xsl:call-template name="make.file"/> \
  </xsl:template> \
 \
  <xsl:template name="make.file"> \
    <xsl:param name="count" select="1"/> \
    <xsl:param name="current.files" select="concat(normalize-space($$files), ^^ ^^)"/> \
    <xsl:param name="more.files" \
               select="concat(normalize-space(substring-after($$current.files, ^^ ^^)),^^ ^^)"/> \
    <xsl:param name="file.number" select="1"/> \
    <xsl:param name="filename" select="concat(^^./build/^^,$$file.number,^^.xml^^)"/> \
 \
    <xsl:choose> \
      <xsl:when test="$$more.files = ^^ ^^"/> \
      <xsl:when test="$$count mod $$chunk.size = 0"> \
        <xsl:variable name="fileset" select="concat($$current.files, ^^ ^^, \
          substring-before($$more.files, ^^ ^^))"/> \
        <exsl:document href="{$$filename}" \
                       method="xml" \
                       encoding="UTF-8" \
                       indent="yes" \
                       omit-xml-declaration="yes" \
                       media-type="" \
                       standalone="no"> \
          <$(WRAPPER_ELEMENT)> \
            <title>$(WRAPPER_TITLE)</title> \
            <xsl:call-template name="make.xinclude"> \
              <xsl:with-param name="file"> \
                <xsl:choose> \
                  <xsl:when test="contains($$fileset, ^^ ^^)"> \
                    <xsl:value-of \
                        select="normalize-space(substring-before($$fileset, ^^ ^^))"/> \
                  </xsl:when> \
                  <xsl:otherwise> \
                    <xsl:value-of select="$$fileset"/> \
                  </xsl:otherwise> \
                </xsl:choose> \
              </xsl:with-param> \
              <xsl:with-param \
                  name="remaining.files" \
                  select="concat(normalize-space(substring-after($$fileset, ^^ ^^)),^^ ^^)"/> \
            </xsl:call-template> \
          </$(WRAPPER_ELEMENT)> \
        </exsl:document> \
        <xsl:call-template name="make.file"> \
          <xsl:with-param name="count" select="1"/> \
          <xsl:with-param name="current.files"  \
                          select="$$more.files"/> \
          <xsl:with-param name="file.number" select="number($$file.number) + 1"/> \
          <xsl:with-param name="filename" select="concat(^^./build/^^,$$file.number,^^.xml^^)"/> \
        </xsl:call-template> \
      </xsl:when> \
      <xsl:otherwise> \
        <xsl:call-template name="make.file"> \
          <xsl:with-param name="count" select="$$count + 1"/> \
          <xsl:with-param name="current.files"> \
            <xsl:choose> \
              <xsl:when test="$$count = 1 and $$file.number = 1"> \
                <xsl:value-of  \
                    select="concat(substring-before($$current.files, ^^ ^^), \
                            ^^ ^^, \
                            substring-before($$more.files, ^^ ^^))"/> \
              </xsl:when> \
              <xsl:when test="$$count = 1"> \
                <xsl:value-of  \
                    select="substring-before($$more.files, ^^ ^^)"/> \
              </xsl:when> \
              <xsl:otherwise> \
                <xsl:value-of  \
                    select="concat($$current.files, ^^ ^^, \
                            substring-before($$more.files, ^^ ^^))"/> \
              </xsl:otherwise> \
            </xsl:choose> \
          </xsl:with-param> \
          <xsl:with-param name="more.files" \
                          select="substring-after($$more.files, ^^ ^^)"/> \
          <xsl:with-param name="file.number" select="$$file.number"/> \
        </xsl:call-template> \
      </xsl:otherwise> \
    </xsl:choose> \
  </xsl:template> \
 \
  <xsl:template name="make.xinclude"> \
    <xsl:param name="file"/> \
    <xsl:param name="remaining.files"/> \
    <xsl:param name="count" select="1"/> \
    <xsl:if test="not($$file = ^^^^)"> \
      <xi:include href="../{$$file}"/> \
      <xsl:call-template name="make.xinclude"> \
        <xsl:with-param \
            name="file" \
            select="substring-before($$remaining.files, ^^ ^^)"/> \
        <xsl:with-param \
            name="remaining.files" \
            select="substring-after($$remaining.files, ^^ ^^)"/> \
        <xsl:with-param name="count" select="$$count + 1"/> \
      </xsl:call-template> \
    </xsl:if> \
  </xsl:template> \
 \
</xsl:stylesheet>

all: man

man: build/man

build/Makefile:
	if [ ! -d build ]; then mkdir build; fi
	cp $(MAKEFILE_DOCBOOK) $@

combine.xsl: Makefile
	@echo '$(COMBINE_XSL)' > $@
	$(SED) $(SED_FLAGS) "s/\^\^/'/g" $@

build/1.xml: combine.xsl
	$(XSLTPROC) $(XSLTPROC_FLAGS) \
	--stringparam files "$(SOURCE_FILES_DBK)" \
	--stringparam chunk.size $(CHUNKSIZE) \
	$< $<

build/man: build/Makefile build/1.xml
	time $(MAKE) -C build man \
		MAN_PARAMS="--stringparam man.output.quietly 1 \
		   --stringparam refentry.meta.get.quietly 1 \
		   --stringparam man.charmap.enabled 0"

debug:

clean:
	$(RM) -r build
