<?xml version='1.0'?>
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:d="http://docbook.org/ns/docbook"
  exclude-result-prefixes="d"
  version="1.0" >
<!-- xsl:import takes place at compile time. For this reason, I sed in the correct
     primary xsl pdf, and then run xsltproc -->
<xsl:import href="PRIMARYXSL"/>
<xsl:output method='xml'/>

<xsl:param name="fop1.extensions" select="1"></xsl:param>
<xsl:param name="chapter.autolabel" select="1"></xsl:param>
<xsl:param name="section.autolabel" select="1"></xsl:param>
<xsl:param name="appendix.autolabel" select="1"></xsl:param>
<xsl:param name="autotoc.label.in.hyperlink" select="1"></xsl:param>
<xsl:param name="section.label.includes.component.label" select="1"></xsl:param>

<!-- Wrap line in screen e programlisting
    This has been copied from xsl-ns-stylesheets/fo/param.xsl and
    modified to permit wrap on verbatim line.
-->
<xsl:attribute-set name="monospace.verbatim.properties" use-attribute-sets="verbatim.properties monospace.properties">
  <xsl:attribute name="text-align">start</xsl:attribute>
  <xsl:attribute name="wrap-option">wrap</xsl:attribute>
</xsl:attribute-set>


<!-- textdecoration_1: 
    A template to highlight a regions of text refers to a lustre specific version.
-->
<xsl:template name='textdecoration_1'>
  <xsl:param name='version'/>
  <xsl:param name='chunkid'/>

  <fo:block-container id="{$chunkid}" padding='0pt' border-color='gray' border-style='solid' border-width='1pt'>
    <fo:block-container float='left' text-indent='5px' start-indent='0px'>
      <fo:block background-color="lightgray">
        <xsl:value-of select='$version'/>
      </fo:block>
    </fo:block-container>
    <fo:block text-indent='3px' start-indent='3px' end-indent='3px'>
        <xsl:apply-templates/>
    </fo:block>
  </fo:block-container>
</xsl:template>


<!-- mychapter:
    A template to highlight a chapter refers to a lustre specific version.

    This has been copied from xsl-ns-stylesheets/fo/component.xsl and
    modified consistently with the highlighting provided by textdecoration_1.
-->
<xsl:template name="mychapter">
  <xsl:param name='version'/>
  <xsl:param name='chunkid'/>

  <xsl:variable name="master-reference">
    <xsl:call-template name="select.pagemaster"/>
  </xsl:variable>

  <fo:page-sequence hyphenate="{$hyphenate}"
                    master-reference="{$master-reference}">
    <xsl:attribute name="language">
      <xsl:call-template name="l10n.language"/>
    </xsl:attribute>
    <xsl:attribute name="format">
      <xsl:call-template name="page.number.format">
        <xsl:with-param name="master-reference" select="$master-reference"/>
      </xsl:call-template>
    </xsl:attribute>
    <xsl:attribute name="initial-page-number">
      <xsl:call-template name="initial.page.number">
        <xsl:with-param name="master-reference" select="$master-reference"/>
      </xsl:call-template>
    </xsl:attribute>

    <xsl:attribute name="force-page-count">
      <xsl:call-template name="force.page.count">
        <xsl:with-param name="master-reference" select="$master-reference"/>
      </xsl:call-template>
    </xsl:attribute>

    <xsl:attribute name="hyphenation-character">
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'hyphenation-character'"/>
      </xsl:call-template>
    </xsl:attribute>
    <xsl:attribute name="hyphenation-push-character-count">
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'hyphenation-push-character-count'"/>
      </xsl:call-template>
    </xsl:attribute>
    <xsl:attribute name="hyphenation-remain-character-count">
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'hyphenation-remain-character-count'"/>
      </xsl:call-template>
    </xsl:attribute>

    <xsl:apply-templates select="." mode="running.head.mode">
      <xsl:with-param name="master-reference" select="$master-reference"/>
    </xsl:apply-templates>

    <xsl:apply-templates select="." mode="running.foot.mode">
      <xsl:with-param name="master-reference" select="$master-reference"/>
    </xsl:apply-templates>

    <fo:flow flow-name="xsl-region-body">
      <xsl:call-template name="set.flow.properties">
        <xsl:with-param name="element" select="local-name(.)"/>
        <xsl:with-param name="master-reference" select="$master-reference"/>
      </xsl:call-template>

      <fo:block-container margin-left="0pt" id="{$chunkid}" padding='5pt' border-color='gray' border-style='solid' border-width='1pt'>
        <fo:block-container float='left' text-indent='5px' start-indent='0px'>
          <fo:block background-color="lightgray">
            <xsl:value-of select='$version'/>
          </fo:block>
        </fo:block-container>
        <fo:block margin-left="0.5in">
          <fo:block text-indent='3px' start-indent='3px' end-indent='3px'
            xsl:use-attribute-sets="component.titlepage.properties">
            <xsl:call-template name="chapter.titlepage"/>
          </fo:block>

      <!-- RHEL and SLES versions have an older docbook xsl install
           that fails on this call: 
           xsl:call-template name="make.component.tocs"/ -->

          <xsl:apply-templates/>
        </fo:block>
      </fo:block-container>
    </fo:flow>
  </fo:page-sequence>
</xsl:template>


<!-- mysection:
    A template to highlight a section refers to a lustre specific version.

    This has been copied from xsl-ns-stylesheets/fo/sections.xsl and
    modified consistently with the highlighting provided by textdecoration_1.
-->
<xsl:template name="mysection">
  <xsl:param name='version'/>
  <xsl:param name='chunkid'/>

  <fo:block-container margin-left="0pt" id="{$chunkid}" padding='5pt' border-color='gray' border-style='solid' border-width='1pt'>
    <fo:block-container float='left' text-indent='5px' start-indent='0px'>
      <fo:block background-color="lightgray">
        <xsl:value-of select='$version'/>
      </fo:block>
    </fo:block-container>

    <fo:block margin-left="0.5in">
      <xsl:choose>
        <xsl:when test="$rootid = @id or $rootid = @xml:id">
          <xsl:call-template name="section.page.sequence"/>
        </xsl:when>
        <xsl:otherwise>

          <xsl:variable name="renderas">
            <xsl:choose>
              <xsl:when test="@renderas = 'sect1'">1</xsl:when>
              <xsl:when test="@renderas = 'sect2'">2</xsl:when>
              <xsl:when test="@renderas = 'sect3'">3</xsl:when>
              <xsl:when test="@renderas = 'sect4'">4</xsl:when>
              <xsl:when test="@renderas = 'sect5'">5</xsl:when>
              <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <xsl:variable name="level">
            <xsl:choose>
              <xsl:when test="$renderas != ''"> 
                <xsl:value-of select="$renderas"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:call-template name="section.level"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <!-- xsl:use-attribute-sets takes only a Qname, not a variable -->
          <xsl:choose>
            <xsl:when test="$level = 1">
              <xsl:element name="fo:{$section.container.element}"
                           use-attribute-sets="section.level1.properties">
                <xsl:call-template name="section.content"/>
              </xsl:element>
            </xsl:when>
            <xsl:when test="$level = 2">
              <xsl:element name="fo:{$section.container.element}"
                           use-attribute-sets="section.level2.properties">
                <xsl:call-template name="section.content"/>
              </xsl:element>
            </xsl:when>
            <xsl:when test="$level = 3">
              <xsl:element name="fo:{$section.container.element}"
                           use-attribute-sets="section.level3.properties">
                <xsl:call-template name="section.content"/>
              </xsl:element>
            </xsl:when>
            <xsl:when test="$level = 4">
              <xsl:element name="fo:{$section.container.element}"
                           use-attribute-sets="section.level4.properties">
                <xsl:call-template name="section.content"/>
              </xsl:element>
            </xsl:when>
            <xsl:when test="$level = 5">
              <xsl:element name="fo:{$section.container.element}"
                           use-attribute-sets="section.level5.properties">
                <xsl:call-template name="section.content"/>
              </xsl:element>
            </xsl:when>
            <xsl:otherwise>
              <xsl:element name="fo:{$section.container.element}"
                           use-attribute-sets="section.level6.properties">
                <xsl:call-template name="section.content"/>
              </xsl:element>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </fo:block>
  </fo:block-container>
</xsl:template>


<!-- "condition" matching template:
    This calls text decoration template with the correct variables.
-->
<xsl:template match="*[@condition]">
  <xsl:variable name="id">
    <xsl:call-template name="object.id"/>
  </xsl:variable>
  <xsl:variable name="versionstr">
    <xsl:choose>
      <xsl:when test="@condition = 'l21'">Introduced in Lustre 2.1</xsl:when>
      <xsl:when test="@condition = 'l22'">Introduced in Lustre 2.2</xsl:when>
      <xsl:when test="@condition = 'l23'">Introduced in Lustre 2.3</xsl:when>
      <xsl:when test="@condition = 'l24'">Introduced in Lustre 2.4</xsl:when>
      <xsl:when test="@condition = 'l25'">Introduced in Lustre 2.5</xsl:when>
      <xsl:when test="@condition = 'l26'">Introduced in Lustre 2.6</xsl:when>
      <xsl:when test="@condition = 'l27'">Introduced in Lustre 2.7</xsl:when>
      <xsl:when test="@condition = 'l28'">Introduced in Lustre 2.8</xsl:when>
      <xsl:when test="@condition = 'l29'">Introduced in Lustre 2.9</xsl:when>
      <xsl:otherwise>Documentation Error: unrecognised condition attribute</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="name() = 'chapter'">
      <xsl:call-template name='mychapter'>
        <xsl:with-param name='version' select="$versionstr"/>
        <xsl:with-param name='chunkid' select="$id"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="name() = 'section'">
      <xsl:call-template name='mysection'>
        <xsl:with-param name='version' select="$versionstr"/>
        <xsl:with-param name='chunkid' select="$id"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name='textdecoration_1'>
        <xsl:with-param name='version' select="$versionstr"/>
        <xsl:with-param name='chunkid' select="$id"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- toc.line:
    This template over loads the behavior of creating the table of contents.
    It adds in a small entry to identify lustre version specific features.
    For more information, see this page:
    http://xml.web.cern.ch/XML/www.sagehill.net/xml/docbookxsl/PrintCustomEx.html#PrintTocEntries
-->
<xsl:template name="toc.line">
  <xsl:variable name="id">
    <xsl:call-template name="object.id"/>
  </xsl:variable>
  <xsl:variable name="lustrecond">
    <xsl:choose>
      <xsl:when test="@condition='l21'">L 2.1</xsl:when>
      <xsl:when test="@condition='l22'">L 2.2</xsl:when>
      <xsl:when test="@condition='l23'">L 2.3</xsl:when>
      <xsl:when test="@condition='l24'">L 2.4</xsl:when>
      <xsl:when test="@condition='l25'">L 2.5</xsl:when>
      <xsl:when test="@condition='l26'">L 2.6</xsl:when>
      <xsl:when test="@condition='l27'">L 2.7</xsl:when>
      <xsl:when test="@condition='l28'">L 2.8</xsl:when>
      <xsl:when test="@condition='l29'">L 2.9</xsl:when>
      <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="label">
    <xsl:apply-templates select="." mode="label.markup"/>
  </xsl:variable>

  <fo:block text-align-last="justify"
      end-indent="{$toc.indent.width}pt"
      last-line-end-indent="-{$toc.indent.width}pt">
    <fo:inline keep-with-next.within-line="always">
      <xsl:choose>
        <xsl:when test="local-name(.) = 'chapter'">
          <xsl:attribute name="font-weight">bold</xsl:attribute>
        </xsl:when>
      </xsl:choose>
      <fo:basic-link internal-destination="{$id}">
        <xsl:if test="$label != ''">
          <xsl:copy-of select="$label"/>
          <xsl:value-of select="$autotoc.label.separator"/>
        </xsl:if>
        <xsl:apply-templates select="." mode="title.markup"/>
      </fo:basic-link>
    </fo:inline>
    <fo:inline keep-together.within-line="always">
      <xsl:text> </xsl:text>
      <fo:leader leader-pattern="dots"
          leader-pattern-width="3pt"
          leader-alignment="reference-area"
          keep-with-next.within-line="always"/>
      <xsl:text>  </xsl:text>
      <xsl:value-of select='$lustrecond'/>
      <xsl:text>  </xsl:text>
      <fo:basic-link internal-destination="{$id}">
        <fo:page-number-citation ref-id="{$id}"/>
      </fo:basic-link>
    </fo:inline>
  </fo:block>
</xsl:template>

</xsl:stylesheet>
