<?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.
-->

<!--
NOTE: This build file depends on the Velocity Docbook Framework and won't
work unless that framework is installed. Set the property 'dbf.basedir' in
project.properties to refer to the installation directory before using this
script.
-->
<project name="build-docbook" default="all" basedir=".">

    <!-- Load the properties -->
    <property file="project.properties"/>

    <!-- Ensure Velocity Docbook Framework is available -->
    <available file="${dbf.path}" property="dbf.downloaded"/>
    <fail message="The Velocity Docbook Framework is not available. To resolve this download the dependencies (~9 MB) with 'ant get-deps'.">
        <condition>
            <or><isfalse value="${dbf.downloaded}"/></or>
        </condition>
    </fail>

    <import file="${dbf.basedir}/build-docbook.xml"/>

    <property name="docbook.dir" value="click"/>
    <property name="docbook.file" value="click-book"/>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Build all the Velocity Docbook Documentation                       == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
    <target name="all" description="Build all documentation">

        <!-- Build the Users Guide -->
        <antcall target="html"/>
        <antcall target="htmlsingle"/>
        <antcall target="pdf"/>

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

    <!--<fixcrlf srcdir="target/click" includes="**/*.html" eol="crlf" /> -->
    </target>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Create the PDF documentation                                       == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->

    <target name="pdf" description="Generates a single PDF file" depends="prepare">

        <taskdef name="fop"
             classname="org.apache.fop.tools.anttasks.Fop">
            <classpath>
                <fileset dir="${dbf.basedir}/lib">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
        </taskdef>

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

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

        <fop format="application/pdf"
            basedir="${src.dir}"
            fofile="${pdf.target.file}.xml"
            outfile="target/${docbook.dir}/pdf/${docbook.file}.pdf" />

        <!-- Copy artifacts to Click user-guide folder -->
        <delete dir="${click.basedir}/documentation/docs/user-guide/pdf/"/>
        <copy todir="${click.basedir}/documentation/docs/user-guide/pdf/">
            <fileset dir="target/${docbook.dir}/pdf/"/>
        </copy>

    </target>

    <target name="html" description="Generate HTML docs in multiple files">

        <ant antfile="${dbf.basedir}/build-docbook.xml" target="html">
            <property name="docbook.dir" value="${docbook.dir}"/>
            <property name="docbook.file" value="${docbook.file}"/>
        </ant>

        <!-- Copy artifacts to Click user-guide folder -->
        <delete dir="${click.basedir}/documentation/docs/user-guide/html/"/>
        <copy todir="${click.basedir}/documentation/docs/user-guide/html/">
            <fileset dir="target/${docbook.dir}/html/">
                <exclude name="**/*.zip"/>
            </fileset>
        </copy>

    </target>

    <target name="htmlsingle" description="Generate HTML docs in a single, big file">

        <ant antfile="${dbf.basedir}/build-docbook.xml" target="htmlsingle">
            <property name="docbook.dir" value="${docbook.dir}"/>
            <property name="docbook.file" value="${docbook.file}"/>
        </ant>

        <!-- Copy artifacts to Click user-guide folder -->
        <delete dir="${click.basedir}/documentation/docs/user-guide/htmlsingle/"/>
        <copy todir="${click.basedir}/documentation/docs/user-guide/htmlsingle/">
            <fileset dir="target/${docbook.dir}/htmlsingle/">
                <exclude name="**/*.zip"/>
            </fileset>
        </copy>
    </target>

  <!-- ======================================================================== -->
  <!-- ==                                                                    == -->
  <!-- == Clean up the target directory                                      == -->
  <!-- ==                                                                    == -->
  <!-- ======================================================================== -->
    <target name="clean" description="Delete temporary and distribution directories for docs">
        <ant antfile="${dbf.basedir}/build-docbook.xml" target="clean"/>
    </target>
</project>
