<?xml version="1.0" encoding="UTF-8"?>

<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you 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 name="docbook" default="all" basedir=".">

  <!-- Load our properties -->
  <property file="${dbf.basedir}/docbook.properties"/>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Set up the classpath for the XSLT conversion                       == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
  <path id="dbf.classpath">
    <fileset dir="${dbf.basedir}/lib">
      <include name="**/*.jar"/>
    </fileset>
    <fileset dir="${dbf.xsl.dir}/extensions">
      <include name="${xslt-db.jar}"/>
    </fileset>
    <pathelement location="${tmp.dir}"/>
  </path>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Prepare Path elements to be used when filtering the XSL files      == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
  <path id="dbf.xml.path" location="${dbf.xml.dir}"/>
  <path id="dbf.xsl.path" location="${dbf.xsl.dir}"/>
  <path id="src.dir.path" location="${src.dir}"/>
  <path id="tmp.dir.path" location="${tmp.dir}"/>
  <path id="target.dir.path" location="${target.dir}"/>
  <pathconvert dirsep="/" property="dbf.xml.path" refid="dbf.xml.path"/>
  <pathconvert dirsep="/" property="dbf.xsl.path" refid="dbf.xsl.path"/>
  <pathconvert dirsep="/" property="src.dir.path" refid="src.dir.path"/>
  <pathconvert dirsep="/" property="tmp.dir.path" refid="tmp.dir.path"/>
  <pathconvert dirsep="/" property="target.dir.path" refid="target.dir.path"/>

  <condition  property="file.prefix" value="file:///">
    <os family="windows"/>
  </condition>
  <condition  property="file.prefix" value="file://">
    <os family="unix"/>
  </condition>

  <!-- This filterset is used to convert the style sheets to reference
       the actual locations of the docbook XSLT files -->
  <filterset id="filterset.dbf.location">
    <filter token="paper.type" value="${paper.type}" />
    <filter token="file.prefix" value="${file.prefix}" />
    <filter token="dbf.xml" value="${dbf.xml.path}" />
    <filter token="dbf.xsl" value="${dbf.xsl.path}" />
    <filter token="src.dir" value="${src.dir.path}" />
    <filter token="tmp.dir" value="${tmp.dir.path}" />
    <filter token="target.dir" value="${target.dir.path}" />
  </filterset>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Xalan Converter macro that uses commons-resolver                   == -->
  <!-- ==                                                                    == -->
  <!-- == input: The file to transform                                       == -->
  <!-- == output: The transformation result                                  == -->
  <!-- == style: The Style Sheet used for the transformation                 == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
  <macrodef name="xalan">
    <attribute name="input"/>
    <attribute name="output"/>
    <attribute name="style"/>
    <sequential>
      <java classname="org.apache.xalan.xslt.Process" fork="true"
            dir="${basedir}" classpathref="dbf.classpath">
        <arg line="-URIRESOLVER org.apache.xml.resolver.tools.CatalogResolver"/>
        <arg line="-ENTITYRESOLVER org.apache.xml.resolver.tools.CatalogResolver"/>
        <arg value="-OUT"/>
        <arg value="@{output}"/>
        <arg value="-IN"/>
        <arg value="@{input}"/>
        <arg value="-XSL"/>
        <arg value="@{style}"/>
      </java>
    </sequential>
  </macrodef>



  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Transformation Macro that generates output from Docbook            == -->
  <!-- ==                                                                    == -->
  <!-- == type: Type of transformation (pdf, html, htmlsingle)               == -->
  <!-- == title: Titlepage to use (html and htmlsingle both use html!)       == -->
  <!-- == target: Target directory (pdf needs tmp for FOP transform)         == -->
  <!-- == dir: Subdir where the Docbook XML file is located. Also subdir     == -->
  <!-- ==      for the output                                                == -->
  <!-- == file: Name of the file (without ending!) to transform              == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
  <macrodef name="transform">
    <attribute name="type"/>
    <attribute name="title"/>
    <attribute name="target"/>
    <attribute name="dir"/>
    <attribute name="file"/>
    <attribute name="xsl"/>
    <sequential>

    <property name="@{type}.target.dir" value="${target.dir}/@{dir}/@{target}"/>
    <property name="@{type}.target.file" value="${@{type}.target.dir}/@{file}"/>
    <property name="@{type}.tmp.style" value="${tmp.dir}/@{type}.xsl"/>
    <property name="@{type}.tmp.custom" value="${tmp.dir}/@{type}-style.xsl"/>
    <property name="@{type}.tmp.titlepage" value="${tmp.dir}/@{type}-titlepage"/>
    <property name="@{type}.tmp.imageincludes" value="${tmp.dir}/@{type}_imageincludes.txt"/>

    <path id="@{type}.target.path" location="${@{type}.target.dir}"/>
    <pathconvert dirsep="/" property="@{type}.target.path" refid="@{type}.target.path"/>

    <copy overwrite="true" filtering="on" file="${dbf.style.src.dir}/@{type}.xsl" tofile="${@{type}.tmp.style}">
      <filterset refid="filterset.dbf.location"/>
      <filterset>
        <filter token="@{type}.target.dir" value="${@{type}.target.path}" />
      </filterset>
    </copy>

    <copy overwrite="true" failonerror="false" filtering="on" file="${styles.src.dir}/@{type}/custom.xsl" tofile="${@{type}.tmp.custom}">
      <filterset refid="filterset.dbf.location"/>
      <filterset>
        <filter token="@{type}.target.dir" value="${@{type}.target.path}" />
      </filterset>
    </copy>

    <!-- No custom style file around. Use the vanilla one. -->
    <copy overwrite="false" failonerror="true" file="${dbf.style.src.dir}/custom.xsl" tofile="${@{type}.tmp.custom}"/>

    <mkdir dir="${@{type}.target.dir}/" />

    <!-- copy custom title page. If we don't have one, use the custom one. -->
    <copy file="${styles.src.dir}/@{title}/titlepage.xml" tofile="${@{type}.tmp.titlepage}.xml" overwrite="true" failonerror="false" />
    <copy file="${dbf.xsl.dir}/@{xsl}/titlepage.templates.xml" tofile="${@{type}.tmp.titlepage}.xml" overwrite="false" failonerror="true" />

    <xalan input="${@{type}.tmp.titlepage}.xml"
           output="${@{type}.tmp.titlepage}.xsl"
           style="${dbf.xsl.dir}/template/titlepage.xsl"/>

    <!-- copy custom imageincludes.  If we don't have one, use the default one. -->
    <copy file="${docbook.src.dir}/@{dir}/@{file}.@{type}_imageincludes.txt"
      tofile="${@{type}.tmp.imageincludes}" overwrite="true" failonerror="false" />
    <copy file="${dbf.basedir}/default_imageincludes.txt" tofile="${@{type}.tmp.imageincludes}"
      overwrite="false" failonerror="true" />
    <copy todir="${@{type}.target.dir}/images">
      <fileset dir="${images.src.dir}" includesfile="${@{type}.tmp.imageincludes}" />
    </copy>

    <xalan input="${docbook.src.dir}/@{dir}/@{file}.xml"
           output="${@{type}.target.file}.xml"
           style="${@{type}.tmp.style}"/>

    </sequential>
  </macrodef>  
  
  <!-- =========================================================================== -->
  <!-- ==                                                                       == -->
  <!-- == prepare temporary directories and unzip the docbook DTD and XSL files == -->
  <!-- ==                                                                       == -->
  <!-- =========================================================================== -->
  <target name="prepare">

    <fail message="You must set docbook.file and docbook.dir!">
      <condition>
        <not>
          <and>
            <isset property="docbook.dir"/>
            <isset property="docbook.file"/>
          </and>
        </not>
      </condition>
    </fail>

    <fail message="You must configure the dbf.basedir from your project.properties. That is usually ${basedir}/docbook!">
      <condition>
        <not>
          <and>
            <isset property="dbf.basedir"/>
          </and>
        </not>
      </condition>
    </fail>

    <mkdir dir="${tmp.dir}" />

    <mkdir dir="${dbf.xml.dir}" />
    <unzip src="${dbf.zip.src.dir}/docbook-xml-${dbf.xml.version}.zip" dest="${dbf.xml.dir}" />

    <mkdir dir="${dbf.xsl.dir}" />
    <unzip src="${dbf.zip.src.dir}/docbook-xsl-${dbf.xsl.version}.zip" dest="${target.dir}" />

    <copy todir="${tmp.dir}" filtering="on" overwrite="true">
      <fileset dir="${dbf.resolver.src.dir}"/>
      <filterset refid="filterset.dbf.location"/>
    </copy>

  </target>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == This is the main target to generate all the docs                   == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
  <target name="all" depends="pdf,html,htmlsingle"
          description="--> Generate and copy reference documentation"/>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Clean up the target directory                                      == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
  <target name="clean"
          description="--> Delete temporary and distribution directories for docs">

    <fail message="You must configure the dbf.basedir from your project.properties. That is usually ${basedir}/docbook!">
      <condition>
        <not>
          <and>
            <isset property="dbf.basedir"/>
          </and>
        </not>
      </condition>
    </fail>

    <delete quiet="true" dir="${target.dir}"/>
  </target>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Create the PDF documentation                                       == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
  <target name="pdf"
          depends="prepare"
          description="--> Generate PDF docs">

    <transform type="pdf" target="tmp" title="pdf" xsl="fo" dir="${docbook.dir}" file="${docbook.file}"/>

    <mkdir dir="${target.dir}/${docbook.dir}/pdf"/>

    <java classname="org.apache.fop.cli.Main" fork="true" maxmemory="256m"
          dir="${basedir}" classpathref="dbf.classpath">
      <arg value="${pdf.target.file}.xml"/>
      <arg value="${target.dir}/${docbook.dir}/pdf/${docbook.file}.pdf"/>
    </java>
  </target>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Create the HTML documentation, many pages                          == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
  <target name="html"
          depends="prepare"
          description="--> Generate HTML docs in multiple files">

    <transform type="html" target="html" title="html" xsl="html" dir="${docbook.dir}" file="${docbook.file}"/>

    <!-- transform copied the custom or default imageincludes, so use it -->
    <copy todir="${html.target.dir}/images">
      <fileset dir="${dbf.xsl.dir}/images/" includesfile="${tmp.dir}/html_imageincludes.txt"/>
    </copy>

    <copy todir="${html.target.dir}/css">
      <fileset dir="${css.src.dir}/html"/>
    </copy>

    <delete file="${html.target.file}.xml"/>

    <delete quiet="true" file="${html.target.file}.zip"/>
    <zip basedir="${target.dir}/${docbook.dir}" destfile="${html.target.file}.zip">
      <include name="html/**"/>
    </zip>
  </target>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Create the HTML documentation, one big page                        == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
  <target name="htmlsingle" 
          depends="prepare"
          description="--> Generate HTML docs in a single, big file">

    <transform type="htmlsingle" target="htmlsingle" title="html" xsl="html" dir="${docbook.dir}" file="${docbook.file}"/>

    <!-- transform copied the custom or default imageincludes, so use it -->
    <copy todir="${htmlsingle.target.dir}/images">
      <fileset dir="${dbf.xsl.dir}/images/" includesfile="${tmp.dir}/htmlsingle_imageincludes.txt"/>
    </copy>

    <copy todir="${htmlsingle.target.dir}/css">
      <fileset dir="${css.src.dir}/html"/>
    </copy>

    <move file="${htmlsingle.target.file}.xml" tofile="${htmlsingle.target.file}.html"/>

    <delete quiet="true" file="${htmlsingle.target.file}.zip"/>
    <zip basedir="${target.dir}/${docbook.dir}" destfile="${htmlsingle.target.file}.zip">
      <include name="htmlsingle/**"/>
    </zip>

  </target>
</project>
