<!--
Copyright 2004 The Apache Software Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project default="site"
         xmlns:j="jelly:core">

  <preGoal name="site">
    <attainGoal name= "docbook"/>
    <attainGoal name= "docbook:copy"/>
  </preGoal>

  <goal name="docbook"
        prereqs="docbook:generate-html, docbook:generate-pdf"
        description="Generate HTML and PDF from docbook sources in ${maven.docbook.src.dir}">
  </goal>

  <goal name="docbook:stylesheets-installed"
        description="Test for docbook directory">
    <j:set var="docBookStyleSheetDirX" value="${docbook.stylesheet.dir}X"/>
    <j:if test="${docBookStyleSheetDirXi == 'X'}" >
      <echo>Property docbook.stylesheet.dir not set.  Please down load the
            DocBook stylesheet from http://docbook.sourceforge.net and set
            the property in you build.properties
      </echo>
      <echo> $docbook.stylesheet.dir=${docbook.stylesheet.dir}</echo>
    </j:if>
  </goal>

  <goal name="docbook:generate-html"
        prereqs="docbook:stylesheets-installed"
        description="Generate HTML from docbook sources in ${maven.docbook.src.dir}">

    <!-- create target directory for generated HTML -->
    <mkdir dir="${maven.docbook.target.html}"/>

    <!-- get all *.xml and *.sgml -->  
    <fileScanner var="docbook_files">
      <fileset dir="${maven.docbook.src.dir}" includes="**/*.xml, **/*.sgml"/>
    </fileScanner>

    <j:forEach var="file" items="${docbook_files.iterator()}">

      <echo>Processing ${file.absolutePath}</echo>

      <!-- strip the extension from the file name -->      
      <j:set var="index" value="${file.name.lastIndexOf('.')}"/>
      <j:set var="basename" value="${file.name.substring(0,index)}"/>

      <!-- Use XSLT processor to transform DOCBOOK into HTML -->      
      <java classname="org.apache.xalan.xslt.Process" fork="yes">
        <classpath>
          <pathelement location="${pom.getDependencyPath('xalan')}"/>
          <pathelement location="${pom.getDependencyPath('xerces')}"/>
          <pathelement location="${pom.getDependencyPath('xml-apis')}"/>
        </classpath>
        <arg value="-in"/>
        <arg value="${file.absolutePath}"/>
        <arg value="-out"/>
        <arg value="${maven.docbook.target.html}/${basename}.html"/>
        <arg value="-xsl"/>
        <arg value="${docbook.stylesheet.dir}/html/docbook.xsl"/>
      </java>
        
    </j:forEach>
  </goal>

  <goal name="docbook:generate-fo"
        prereqs="docbook:stylesheets-installed"
        description="Generate FO from docbook sources in ${maven.docbook.src.dir}">

    <!-- create target directory for generated FO -->
    <mkdir dir="${maven.docbook.target.fo}"/>

    <!-- get all *.xml and *.sgml -->  
    <fileScanner var="docbook_files">
      <fileset dir="${maven.docbook.src.dir}" includes="**/*.xml, **/*.sgml"/>
    </fileScanner>

    <j:forEach var="file" items="${docbook_files.iterator()}">

      <echo>Processing ${file.absolutePath}</echo>

      <!-- strip the extension from the file name -->      
      <j:set var="index" value="${file.name.lastIndexOf('.')}"/>
      <j:set var="basename" value="${file.name.substring(0,index)}"/>

      <!-- Use XSLT processor to transform DOCBOOK into HTML -->      
      <java classname="org.apache.xalan.xslt.Process" fork="yes">
        <classpath>
          <pathelement location="${pom.getDependencyPath('xalan')}"/>
          <pathelement location="${pom.getDependencyPath('xerces')}"/>
          <pathelement location="${pom.getDependencyPath('xml-apis')}"/>
        </classpath>
        <arg value="-in"/>
        <arg value="${file.absolutePath}"/>
        <arg value="-out"/>
        <arg value="${maven.docbook.target.fo}/${basename}.fo"/>
        <arg value="-xsl"/>
        <arg value="${docbook.stylesheet.dir}/fo/docbook.xsl"/>
      </java>
        
    </j:forEach>
  </goal>
  <goal name="docbook:generate-pdf"
        prereqs="docbook:generate-fo"
        description="Generate PDF from the generated FO in ${maven.docbook.target.fo}">

    <!-- create target directory for generated PDF -->
    <mkdir dir="${maven.docbook.target.pdf}"/>


    <!-- get all *.xml and *.sgml -->  
    <fileScanner var="docbook_files">
      <fileset dir="${maven.docbook.target.fo}" includes="**/*.fo"/>
    </fileScanner>

    <j:forEach var="file" items="${docbook_files.iterator()}">

      <echo>Processing ${file.absolutePath}</echo>

      <!-- strip the extension from the file name -->      
      <j:set var="index" value="${file.name.lastIndexOf('.')}"/>
      <j:set var="basename" value="${file.name.substring(0,index)}"/>

      <!-- Use XSLT processor to transform DOCBOOK into HTML -->      
      <java classname="org.apache.fop.apps.Fop" fork="yes">
        <classpath>
          <pathelement location="${pom.getDependencyPath('fop')}"/>
          <pathelement location="${pom.getDependencyPath('logkit')}"/>
          <pathelement location="${pom.getDependencyPath('xerces')}"/>
          <pathelement location="${pom.getDependencyPath('xml-apis')}"/>
          <pathelement location="${pom.getDependencyPath('avalon-framework')}"/>
          <pathelement location="${pom.getDependencyPath('batik')}"/>
        </classpath>
        <arg value="-fo"/>
        <arg value="${file.absolutePath}"/>
        <arg value="-pdf"/>
        <arg value="${maven.docbook.target.pdf}/${basename}.pdf"/>
      </java>
        
    </j:forEach>
  </goal>

  <goal
    name="docbook:copy"
    description="Copy the generated HTML and PDF into doc"
    prereqs="site:init">
    
    <!-- create target directory for generated PDF -->
    <mkdir dir="${maven.docbook.target.doc}"/>

    <copy todir="${maven.docbook.target.doc}" filtering="no">
      <fileset dir="${maven.docbook.target.html}">
        <include name="**/*.html"/>
      </fileset>
    </copy>

    <copy todir="${maven.docbook.target.doc}" filtering="no">
      <fileset dir="${maven.docbook.target.pdf}">
        <include name="**/*.pdf"/>
      </fileset>
    </copy>
    
  </goal>

</project>
