<?xml version="1.0" encoding="UTF-8"?>
<!--
Usage:
    Building 'The Definitive Guide' - html
        $ ant html

    Building 'The Definitive Guide' - pdf
        $ ant pdf

Dependency:
    *) jdk 1.6
    *) ant 1.7
    *) bsf 2.4
    *) beanshell 2.0b4
    *) saxon 6.5.5
    *) RELAX NG Support(jing)
    *) DocBook v5 XSL
    *) python 2.5
    *) python-libxml2 2.6.32 (Line number in the pot file is inaccurate if XML
            file larger than 65535 lines, See:
            http://bugzilla.gnome.org/show_bug.cgi?id=325533)
    *) gettext 0.17
    *) xep 4.1.4 or fop trunk 2008-12-19(r728025), pdf only
    *) chinese font files, pdf only
-->
<project name="defguide" default="html">

    <property file="build.properties"/>

    <path id="lib.classpath">
        <fileset dir="${ant.home}/lib">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${fop.home}">
            <include name="**/*.jar"/>
        </fileset>
        <fileset dir="${usr.share.java}">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement location="${usr.share.java}"/>
    </path>

    <path id="xep.classpath">
        <fileset dir="${xep.home}/lib">
            <include name="*.jar"/>
        </fileset>
    </path>

    <taskdef name="jing" classname="com.thaiopensource.relaxng.util.JingTask"/>

    <target name="usage">
        <echo message="Use the -projecthelp option instead"/>
    </target>

    <target name="clean" description="Clean the build directory">
        <delete dir="build"/>
    </target>

    <target name="init" description="Check runtime configuration">
        <echo message="java.version: ${java.version}"/>

        <!--Java SE 1.4 & 1.5: Unfixed java.lang.StackOverflowError !-->
        <!--
        <condition property="jdk_ok">
            <scriptcondition language="beanshell" value="true">
            <![CDATA[
                jvm = System.getProperty("java.specification.version");
                if(jvm.compareTo("1.6") < 0)
                    self.setValue(false);
            ]]>
            </scriptcondition>
        </condition>
        <fail unless="jdk_ok">
            Must use Java SE 6+ !
        </fail>
        -->

        <fail>
            <condition>
                <not><available file="build.properties"/></not>
            </condition>
            Please create your build.properties from build.properties.tmpl !
        </fail>

        <fail>
            <condition>
                <not><available classname="com.thaiopensource.relaxng.util.Driver"/></not>
            </condition>
            RELAX NG Support(jing) NOT Present !
        </fail>

        <fail>
            <condition>
                <not><available classname="com.icl.saxon.StyleSheet" classpathref="lib.classpath"/></not>
            </condition>
            Saxon 6.5 Support NOT Present !
        </fail>
    </target>

    <target name="validate" depends="init" description="Validate document">
        <jing rngfile="${docbook5.home}/rng/docbook.rng" file="source/defguide.xml"/>
        <!--Don't uncomment this unless xml2po support namespace !-->
        <!--jing rngfile="${docbook5.home}/rng/docbook.rng"
                file="build/defguide-zh.xml"/-->
    </target>

    <target name="all" description="Generate document - pdf,html">
        <antcall target="defguide"/>
        <antcall target="pdf"/>
        <antcall target="html"/>
    </target>

    <target name="po.init">
        <uptodate property="po.isUpToDate" targetfile="po/zh.po">
            <srcfiles dir="source" includes="defguide.xml"/>
        </uptodate>
    </target>

    <!-- Total time: 29 minutes 12 seconds -->
    <target name="updatepo" depends="po.init" unless="po.isUpToDate" description="Update PO file">
        <echo message="Making defguide.pot ..."/>
        <exec dir="${basedir}/po" executable="python" failonerror="true">
            <arg value="../xml2po/xml2po.py"/>
            <arg value="-o"/>
            <arg value="defguide.pot"/>
            <arg value="../source/defguide.xml"/>
        </exec>

        <echo message="Merging zh.po ..."/>
        <exec dir="${basedir}" executable="msgmerge">
            <arg value="--sort-by-file"/>
            <arg value="--width=80"/>
            <arg value="-o"/>
            <arg value="po/zh.po.new"/>
            <arg value="po/zh.po"/>
            <arg value="po/defguide.pot"/>
        </exec>
        <move file="po/zh.po.new" tofile="po/zh.po"/>
    </target>

    <target name="translate.init" depends="updatepo">
        <uptodate property="translate.isUpToDate" targetfile="build/defguide-zh.xml">
            <srcfiles dir="source" includes="defguide.xml"/>
            <srcfiles dir="po" includes="zh.po"/>
        </uptodate>
    </target>

    <!-- Total time: 29 minutes 14 seconds -->
    <target name="translate" depends="translate.init" unless="translate.isUpToDate"
            description="Translate document">
        <mkdir dir="build"/>
        <echo message="Translating defguide-zh.xml ..."/>
        <exec dir="${basedir}/xml2po" executable="python" failonerror="true">
            <arg value="xml2po.py"/>
            <arg value="-l"/>
            <arg value="zh-cn"/>
            <arg value="-p"/>
            <arg value="../po/zh.po"/>
            <arg value="-o"/>
            <arg value="../build/defguide-zh.xml"/>
            <arg value="../source/defguide.xml"/>
        </exec>
        <delete file="xml2po/.xml2po.mo"/>
    </target>

    <target name="html.init">
        <uptodate property="html.isUpToDate" targetfile="build/html/.done">
            <srcfiles dir="source" includes="defguide.xml"/>
            <srcfiles dir="po" includes="zh.po"/>
            <srcfiles dir="stylesheets" includes="html.xsl"/>
            <srcfiles dir="stylesheets/zh" includes="html.xsl"/>
        </uptodate>
    </target>

    <!-- Total time: 14 minutes 10 seconds -->
    <target name="html" depends="init,translate,html.init" unless="html.isUpToDate"
            description="Generate document - html">
        <copy todir="build/schema">
            <fileset dir="source/schema"/>
        </copy>
        <copy todir="build/html/images">
            <fileset dir="${docbook.xsl}/images" includes="**/*.png"/>
        </copy>
        <copy todir="build/html/figures">
            <fileset dir="source/figures"/>
        </copy>
        <copy todir="build/examples">
            <fileset dir="../en/examples"/>
        </copy>

        <echo message="Making html ..."/>
        <java classname="com.icl.saxon.StyleSheet" fork="true" maxmemory="1024m"
                failonerror="true" dir="${basedir}/build/html">
            <jvmarg value="-Xss512k"/>
            <classpath refid="lib.classpath"/>
            <arg value="-x"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-y"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-r"/>
            <arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
            <arg value="../defguide-zh.xml"/>
            <arg value="../../stylesheets/zh/html.xsl"/>
        </java>
        <!--exec dir="${basedir}/build/html" executable="xsltproc" failonerror="true">
            <arg value="- -nonet"/>
            <arg value="../../stylesheets/zh/html.xsl"/>
            <arg value="../defguide-zh.xml"/>
        </exec-->
        <touch file="build/html/.done"/>
    </target>

    <target name="pdf.init">
        <uptodate property="pdf.isUpToDate" targetfile="build/defguide-zh.pdf">
            <srcfiles dir="source" includes="defguide.xml"/>
            <srcfiles dir="po" includes="zh.po"/>
            <srcfiles dir="stylesheets" includes="fo.xsl"/>
            <srcfiles dir="stylesheets/zh" includes="fo.xsl"/>
        </uptodate>
    </target>

    <!-- Total time: 10 minutes 44 seconds -->
    <target name="pdf" depends="init,translate,pdf.init" unless="pdf.isUpToDate"
            description="Generate document - pdf">
        <copy todir="build/schema">
            <fileset dir="source/schema"/>
        </copy>
        <copy todir="build/images">
            <fileset dir="${docbook.xsl}/images" includes="**/*.png"/>
        </copy>
        <copy todir="build/figures">
            <fileset dir="source/figures" />
        </copy>
        <copy todir="build/examples">
            <fileset dir="../en/examples"/>
        </copy>

        <echo message="Making fo ..."/>
        <java classname="com.icl.saxon.StyleSheet" fork="true" maxmemory="1024m"
                failonerror="true" dir="${basedir}/build">
            <jvmarg value="-Xss512k"/>
            <classpath refid="lib.classpath"/>
            <arg value="-x"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-y"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-r"/>
            <arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
            <arg value="-o"/>
            <arg value="defguide-zh.fo"/>
            <arg value="defguide-zh.xml"/>
            <arg value="../stylesheets/zh/fo.xsl"/>
            <arg value="fop1.extensions=1"/>
            <arg value="title.font.family=${fop.title.font.family}"/>
            <arg value="body.font.family=${fop.body.font.family}"/>
            <arg value="sans.font.family=${fop.sans.font.family}"/>
            <arg value="ingbat.font.family=${fop.ingbat.font.family}"/>
            <arg value="monospace.font.family=${fop.monospace.font.family}"/>
        </java>

        <echo message="Making pdf ..."/>
        <java classname="${fop.class}" fork="true" maxmemory="1024m"
                failonerror="true" dir="${basedir}/build">
            <classpath refid="lib.classpath"/>
            <arg value="-c"/>
            <arg value="${fop.home}/conf/userconfig.xml"/>
            <arg value="defguide-zh.fo"/>
            <arg value="defguide-zh.pdf"/>
        </java>
    </target>

    <!-- Total time: 12 minutes 49 seconds -->
    <target name="pdf-xep" depends="init,translate,pdf.init" unless="pdf.isUpToDate"
            description="Generate document - pdf - xep">
        <copy todir="build/schema">
            <fileset dir="source/schema"/>
        </copy>
        <copy todir="build/images">
            <fileset dir="${docbook.xsl}/images" includes="**/*.png"/>
        </copy>
        <copy todir="build/figures">
            <fileset dir="source/figures" />
        </copy>
        <copy todir="build/examples">
            <fileset dir="../en/examples"/>
        </copy>

        <echo message="Making fo ..."/>
        <java classname="com.icl.saxon.StyleSheet" fork="true" maxmemory="1024m"
                failonerror="true" dir="${basedir}/build">
            <jvmarg value="-Xss1m"/>
            <classpath refid="lib.classpath"/>
            <arg value="-x"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-y"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-r"/>
            <arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
            <arg value="-o"/>
            <arg value="defguide-zh.fo"/>
            <arg value="defguide-zh.xml"/>
            <arg value="../stylesheets/zh/fo.xsl"/>
            <arg value="xep.extensions=1"/>
        </java>

        <echo message="Making pdf ..."/>
        <java classname="com.renderx.xep.XSLDriver" fork="true" maxmemory="1536m"
                failonerror="true" dir="${basedir}/build">
            <jvmarg value="-Xss512k"/>
            <classpath refid="xep.classpath"/>
            <arg value="-DCONFIG=${xep.home}/xep.xml"/>
            <arg value="-quiet"/>
            <arg value="defguide-zh.fo"/>
            <arg value="defguide-zh.pdf"/>
        </java>
    </target>

    <target name="defguide.init">
        <uptodate property="defguide.isUpToDate" targetfile="build/defguide.pdf">
            <srcfiles dir="source" includes="defguide.xml"/>
        </uptodate>
    </target>

    <!-- Total time: 12 minutes 14 seconds -->
    <target name="defguide" depends="init,defguide.init" unless="defguide.isUpToDate"
            description="Generate document - defguide(en)">
        <copy todir="build/schema">
            <fileset dir="source/schema"/>
        </copy>
        <copy todir="build/images">
            <fileset dir="${docbook.xsl}/images" includes="**/*.png"/>
        </copy>
        <copy todir="build/figures">
            <fileset dir="source/figures" />
        </copy>
        <copy todir="build/examples">
            <fileset dir="../en/examples"/>
        </copy>
        <copy file="source/defguide.xml" todir="build"/>

        <echo message="Making fo ..."/>
        <java classname="com.icl.saxon.StyleSheet" fork="true" maxmemory="1024m"
                failonerror="true" dir="${basedir}/build">
            <jvmarg value="-Xss512k"/>
            <classpath refid="lib.classpath"/>
            <arg value="-x"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-y"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-r"/>
            <arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
            <arg value="-o"/>
            <arg value="defguide.fo"/>
            <arg value="defguide.xml"/>
            <arg value="../stylesheets/fo.xsl"/>
            <arg value="fop1.extensions=1"/>
            <arg value="title.font.family=${fop.title.font.family}"/>
            <arg value="body.font.family=${fop.body.font.family}"/>
            <arg value="sans.font.family=${fop.sans.font.family}"/>
            <arg value="ingbat.font.family=${fop.ingbat.font.family}"/>
            <arg value="monospace.font.family=${fop.monospace.font.family}"/>
        </java>

        <echo message="Making pdf ..."/>
        <java classname="${fop.class}" fork="true" maxmemory="1024m"
                failonerror="true" dir="${basedir}/build">
            <classpath refid="lib.classpath"/>
            <arg value="-c"/>
            <arg value="${fop.home}/conf/userconfig.xml"/>
            <arg value="defguide.fo"/>
            <arg value="defguide.pdf"/>
        </java>
    </target>

    <!-- Total time: 14 minutes 57 seconds -->
    <target name="defguide-xep" depends="init,defguide.init" unless="defguide.isUpToDate"
            description="Generate document - defguide(en) - xep">
        <copy todir="build/schema">
            <fileset dir="source/schema"/>
        </copy>
        <copy todir="build/images">
            <fileset dir="${docbook.xsl}/images" includes="**/*.png"/>
        </copy>
        <copy todir="build/figures">
            <fileset dir="source/figures" />
        </copy>
        <copy todir="build/examples">
            <fileset dir="../en/examples"/>
        </copy>
        <copy file="source/defguide.xml" todir="build"/>

        <echo message="Making fo ..."/>
        <java classname="com.icl.saxon.StyleSheet" fork="true" maxmemory="1024m"
                failonerror="true" dir="${basedir}/build">
            <jvmarg value="-Xss1m"/>
            <classpath refid="lib.classpath"/>
            <arg value="-x"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-y"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-r"/>
            <arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
            <arg value="-o"/>
            <arg value="defguide.fo"/>
            <arg value="defguide.xml"/>
            <arg value="../stylesheets/fo.xsl"/>
            <arg value="xep.extensions=1"/>
        </java>

        <echo message="Making pdf ..."/>
        <java classname="com.renderx.xep.XSLDriver" fork="true" maxmemory="1536m"
                failonerror="true" dir="${basedir}/build">
            <jvmarg value="-Xss512k"/>
            <classpath refid="xep.classpath"/>
            <arg value="-DCONFIG=${xep.home}/xep.xml"/>
            <arg value="-quiet"/>
            <arg value="defguide.fo"/>
            <arg value="defguide.pdf"/>
        </java>
    </target>

    <target name="defguide-html.init">
        <uptodate property="defguide-html.isUpToDate" targetfile="build/defguide/.done">
            <srcfiles dir="source" includes="defguide.xml"/>
            <srcfiles dir="stylesheets" includes="html.xsl"/>
        </uptodate>
    </target>

    <!-- Total time: 15 minutes 57 seconds -->
    <target name="defguide-html" depends="init,defguide-html.init" unless="defguide-html.isUpToDate"
            description="Generate document - defguide(en) - html">
        <copy todir="build/schema">
            <fileset dir="source/schema"/>
        </copy>
        <copy todir="build/defguide/images">
            <fileset dir="${docbook.xsl}/images" includes="**/*.png"/>
        </copy>
        <copy todir="build/defguide/figures">
            <fileset dir="source/figures" />
        </copy>
        <copy todir="build/examples">
            <fileset dir="../en/examples"/>
        </copy>
        <copy file="source/defguide.xml" todir="build"/>

        <echo message="Making html ..."/>
        <java classname="com.icl.saxon.StyleSheet" fork="true" maxmemory="1024m"
                failonerror="true" dir="${basedir}/build/defguide">
            <jvmarg value="-Xss512k"/>
            <classpath refid="lib.classpath"/>
            <arg value="-x"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-y"/>
            <arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
            <arg value="-r"/>
            <arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
            <arg value="../defguide.xml"/>
            <arg value="../../stylesheets/html.xsl"/>
        </java>
        <touch file="build/defguide/.done"/>
    </target>
</project>
