<project default="all" basedir=".">

    <property environment="env"/>

    <tstamp/>

    <property name="sourceDir" value="." />
    <property name="outputDir" value="." />

    <property name="jambiLib" value="${outputDir}/lib" />

    <target name="help"
            description="Provides detailed help.">
      <echo>To build Qt Jambi, run ant without parameters.</echo>
      <echo></echo>
      <echo>To get a description of all tasks run: ant -projecthelp</echo>
      <echo/>
      <echo>Available build properties:</echo>
      <echo>    -Dqtjambi.config=debug|release    Decide what native libraries</echo>
      <echo>                                      should be included.</echo>
      <echo>    -Dqtjambi.useJumpTable=true       Use jump tables for native functions (experimental)</echo>
      <echo/>
      <echo>You can also see the Qt Jambi intallation document at:</echo>
      <echo>http://doc.trolltech.com/qtjambi/com/trolltech/qt/qtjambi-installation.html</echo>
    </target>

    <uptodate property="ant-qtjambi.jar.uptodate"
              targetfile="${outputDir}/ant-qtjambi.jar" >
        <srcfiles dir= "${sourceDir}"
                  includes="com/trolltech/tools/ant/*.java,com/trolltech/qt/internal/Version.java,com/trolltech/qt/internal/OSInfo.java"/>
    </uptodate>

    <target name="init.ant"
            unless="ant-qtjambi.jar.uptodate"
            description="Creates a .jar file containing all the Qt Jambi ant tasks.">
        <mkdir dir="${outputDir}/ant-qtjambi"/>
        <javac srcdir="${sourceDir}" destdir="${outputDir}/ant-qtjambi" target="1.5">
            <include name="com/trolltech/tools/ant/*.java"/>
            <include name="com/trolltech/qt/internal/Version.java"/>
            <include name="com/trolltech/qt/internal/OSInfo.java"/>
        </javac>
        <jar destfile="${outputDir}/ant-qtjambi.jar">
            <fileset dir="${outputDir}/ant-qtjambi"/>
        </jar>
        <delete dir="${outputDir}/ant-qtjambi"/>
    </target>

    <target name="init.jumptable.do"
        description="Initialize the jump table..." if="qtjambi.useJumpTable">
      <echo>Using Native Jump Table. Please note that this feature is experimental...</echo>
      <property name="qtjambi.config.jumptable" value="jumptable"/>
      <property name="qtjambi.generator.jumptable" value="--native-jump-table"/>
    </target>
    <target name="init.jumptable.dont"
        description="Initialize the jump table..." unless="qtjambi.useJumpTable">
      <property name="qtjambi.config.jumptable" value=""/>
      <property name="qtjambi.generator.jumptable" value=""/>
    </target>

    <target name="init.taskdef"
            depends="init.ant, init.jumptable.do, init.jumptable.dont">
        <taskdef name="juic"      classpath="${outputDir}/ant-qtjambi.jar" classname="com.trolltech.tools.ant.JuicTask"/>
        <taskdef name="qmake"     classpath="${outputDir}/ant-qtjambi.jar" classname="com.trolltech.tools.ant.QMakeTask"/>
        <taskdef name="make"      classpath="${outputDir}/ant-qtjambi.jar" classname="com.trolltech.tools.ant.MakeTask"/>
        <taskdef name="generator" classpath="${outputDir}/ant-qtjambi.jar" classname="com.trolltech.tools.ant.GeneratorTask"/>
        <taskdef name="qtjambi-platform-jar" classpath="${outputDir}/ant-qtjambi.jar" classname="com.trolltech.tools.ant.PlatformJarTask"/>
        <taskdef name="qtjambi-initialize" classpath="${outputDir}/ant-qtjambi.jar" classname="com.trolltech.tools.ant.InitializeTask"/>

    </target>

    <target name="clean"
            description="Cleans everything.">
        <delete file="${outputDir}/ant-qtjambi.jar"/>
    <delete file="${outputDir}/qtjambi-${qtjambi.version}.jar"/>
    <delete file="${outputDir}/qtjambi-examples-${qtjambi.version}.jar"/>
    <delete file="${outputDir}/qtjambi-designer-${qtjambi.version}.jar"/>
        <delete dir="${outputDir}/bin"/>
    <delete dir="${outputDir}/lib"/>
        <delete dir="${outputDir}/cpp"/>
        <delete includeEmptyDirs="true">
          <fileset dir="${outputDir}" includes="**/*.class"/>
          <fileset dir="${outputDir}" includes="**/*.o"/>
          <fileset dir="${outputDir}" includes="**/moc_*.cpp"/>
          <fileset dir="${outputDir}" includes="**/*.obj"/>
          <fileset dir="${outputDir}" includes="**/*.pdb"/>
          <fileset dir="${outputDir}" includes="**/*.ilk"/>
          <fileset dir="${outputDir}" includes="**/release"/>
          <fileset dir="${outputDir}" includes="**/debug"/>
          <fileset dir="${outputDir}" includes="**/Makefile*"/>
        </delete>
    </target>

    <target name="library.native.qmake"
            depends="init"
            description="Runs qmake on the Qt Jambi project.">
        <delete dir="${jambiLib}" />
    <qmake recursive="true"
               dir="${outputDir}"
               pro="${sourceDir}/java.pro"
               config="${qtjambi.configuration} ${qtjambi.config.jumptable}"
               debugTools="${qtjambi.debug-tools}"/>
    </target>

    <target name="generator.qmake"
            depends="init, generator.xmlmerge"
            description="Runs qmake on the generator project.">
      <mkdir dir="${outputDir}/generator"/>
      <qmake config="release"
             dir="${outputDir}/generator"
             pro="${sourceDir}/generator/generator.pro"
             debugTools="${qtjambi.debug-tools}"/>
    </target>

    <target name="generator.compile"
        depends="init, generator.qmake"
            description="Compiles the Qt Jambi generator.">
        <make dir="${outputDir}/generator" />
    </target>

    <target name="generator.run"
            depends="init, generator.compile"
            description="Runs the Qt Jambi generator on the Qt sources, using the Qt version found at the QTDIR variable.">
        <generator dir="${outputDir}/generator"
                   outputDirectory="${outputDir}"
                   typesystem="${sourceDir}/generator/build_all.txt"
                   header="${sourceDir}/generator/qtjambi_masterinclude.h"
           options="${qtjambi.generator.jumptable}"/>
    </target>

    <target name="library.native.compile"
            depends="init"
            description="Compiles the C++ Qt Jambi bindings.">
    <make dir="${outputDir}"/>
    </target>

    <target name="examples.juic"
            depends="init"
            description="Generates Java source files from JUI files created by designer.">
        <juic message="Running Juic for all it is worth"
          outputDir="${outputDir}"
              trFunction=""
              classNamePrefix=""
              alwaysUpdate="true"
          classpath="${sourceDir}">
         <include name="com/**/**.jui"/>
        </juic>
    </target>

    <target name="library.java.compile" depends="init">
      <javac memoryMaximumSize="1024m"
             fork="true"
             destdir="${outputDir}"
         debug="true"
             target="1.5">
        <src path="${outputDir}"/>
        <src path="${sourceDir}"/>
        <include name="com/trolltech/qt/*.java"/>
        <include name="com/trolltech/qt/*/*.java"/>
    <include name="com/trolltech/extensions/signalhandler/*.java"/>
      </javac>
    </target>


    <target name="library.java.bundle" depends="library.java.compile">
        <jar destfile="${outputDir}/qtjambi-${qtjambi.version}.jar"
             basedir="${outputDir}"
             includes="com/trolltech/qt/*.class,com/trolltech/qt/*/*.class,com/trolltech/extensions/signalhandler/*.class"
             includesfile="${sourceDir}/com/trolltech/images/qt-logo.png">
            <manifest>
                <attribute name="Built-By" value="${user.name} - ${TODAY}"/>
            </manifest>
        </jar>
    </target>

    <target name="examples.phonon"
        depends="init"
        if="qtjambi.phonon">
        <javac destdir="${outputDir}" target="1.5">
            <src path="${outputDir}"/>
            <src path="${sourceDir}"/>
            <include name="com/trolltech/demos/phonon/*.java"/>
            <include name="com/trolltech/examples/phonon/*.java"/>
        </javac>
    </target>

    <target name="examples.webkit"
        depends="init"
        if="qtjambi.webkit">
        <javac destdir="${outputDir}" target="1.5">
            <src path="${outputDir}"/>
            <src path="${sourceDir}"/>
            <include name="com/trolltech/demos/webkit/*.java"/>
            <include name="com/trolltech/examples/webkit/*.java"/>
        </javac>
    </target>

    <target name="examples.xmlpatterns"
        depends="init"
        if="qtjambi.xmlpatterns">
        <javac destdir="${outputDir}" target="1.5">
            <src path="${outputDir}"/>
            <src path="${sourceDir}"/>
            <include name="com/trolltech/demos/xmlpatterns/*.java"/>
            <include name="com/trolltech/examples/xmlpatterns/*.java"/>
        </javac>
    </target>

    <target name="examples.compile"
            depends="init, examples.phonon, examples.webkit, examples.xmlpatterns">
        <javac destdir="${outputDir}"
           debug="true"
           fork="true"
           memoryMaximumSize="1024m"
           target="1.5">
            <src path="${outputDir}"/>
            <src path="${sourceDir}"/>
            <include name="com/trolltech/demos/**/*.java"/>
            <include name="com/trolltech/examples/**/*.java"/>
            <include name="com/trolltech/launcher/*.java"/>
            <exclude name="com/trolltech/demos/phonon/*.java"/>
            <exclude name="com/trolltech/demos/xmlpatterns/*.java"/>
            <exclude name="com/trolltech/demos/webkit/*.java"/>
            <exclude name="com/trolltech/examples/phonon/*.java"/>
            <exclude name="com/trolltech/examples/xmlpatterns/*.java"/>
            <exclude name="com/trolltech/examples/webkit/*.java"/>
            <exclude name="com/trolltech/examples/generator/*.java"/>
            <exclude name="com/trolltech/demos/HelloGL.java"/>
            <exclude name="com/trolltech/examples/GeneratorExample.java"/>
        </javac>

        <copy todir="${outputDir}">
            <fileset dir="${sourceDir}">
                <include name="com/trolltech/examples/**/*"/>
                <include name="com/trolltech/demos/**/*"/>
                <include name="com/trolltech/launcher/*"/>
                <include name="com/trolltech/images/**/*"/>
                <exclude name="com/trolltech/**/*.java"/>
                <exclude name="com/trolltech/**/*.jui"/>
            </fileset>
        </copy>
    </target>


    <target name="examples.bundle"
        depends="examples.compile">
      <jar destfile="${outputDir}/qtjambi-examples-${qtjambi.version}.jar"
             basedir="${outputDir}/">
            <include name="com/trolltech/examples/**/*"/>
            <include name="com/trolltech/demos/**/*"/>
            <include name="com/trolltech/launcher/*"/>
            <include name="com/trolltech/images/**/*"/>
            <exclude name="com/trolltech/**/*.jui"/>
            <manifest>
                <attribute name="Built-By" value="${user.name} - ${TODAY}"/>
            </manifest>
        </jar>
    </target>

    <target name="library.designer.compile" depends="init">
        <javac destdir="${outputDir}" target="1.5">
            <src path="${outputDir}"/>
            <src path="${sourceDir}"/>
            <include name="com/trolltech/tools/designer/*.java"/>
        </javac>
    </target>

    <target name="library.designer.bundle" depends="library.designer.compile">
        <jar destfile="${outputDir}/qtjambi-designer-${qtjambi.version}.jar"
             basedir="${outputDir}">
            <include name="com/trolltech/tools/designer/*"/>
            <exclude name="com/trolltech/tools/designer/*.java"/>
           <manifest>
                <attribute name="Built-By" value="${user.name} - ${TODAY}"/>
            </manifest>
        </jar>

    </target>

    <target name="library.native.bundle"
        depends="init"
        description="Creates a .jar file file containing native libraries.">

        <qtjambi-platform-jar cacheKey="${qtjambi.compiler}-${DSTAMP}-${TSTAMP}"
                              outdir="${outputDir}/platform-output">

            <!-- Qt Libraries... -->
            <library name="QtCore"    type="qt" rootPath="${qtjambi.qtdir}" />
            <library name="QtGui"     type="qt" rootPath="${qtjambi.qtdir}" />
            <library name="QtXml"     type="qt" rootPath="${qtjambi.qtdir}" />
            <library name="QtSql"     type="qt" rootPath="${qtjambi.qtdir}" />
            <library name="QtSvg"     type="qt" rootPath="${qtjambi.qtdir}" load="yes" />
            <library name="QtNetwork" type="qt" rootPath="${qtjambi.qtdir}" />
            <library name="QtOpenGL"  type="qt" rootPath="${qtjambi.qtdir}" if="${qtjambi.opengl}"/>
            <library name="QtWebKit"  type="qt" rootPath="${qtjambi.qtdir}" if="${qtjambi.webkit}"/>
            <library name="QtXmlPatterns" type="qt" rootPath="${qtjambi.qtdir}" if="${qtjambi.xmlpatterns}"/>
            <library name="phonon"    type="qt" rootPath="${qtjambi.qtdir}" if="${qtjambi.phonon}"/>
            <library name="QtDBus"    type="qt" rootPath="${qtjambi.qtdir}" if="${qtjambi.dbus}"/>
            <library name="phonon_ds9" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/phonon_backend" load="never" if="${qtjambi.phonon_ds9}" />
            <library name="phonon_gstreamer" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/phonon_backend" load="never" if="${qtjambi.phonon_gstreamer}" />
            <library name="phonon_qt7" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/phonon_backend" load="never" if="${qtjambi.phonon_qt7}" />

            <!-- Qt Plugins... -->
            <library name="qjpeg" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/imageformats" load="never"/>
            <library name="qgif" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/imageformats" load="never"/>
            <library name="qmng" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/imageformats" load="never"/>
            <library name="qtiff" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/imageformats" load="never"/>
            <library name="qsvg" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/imageformats" load="never"/>
            <library name="qsvgicon" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/iconengines" load="never"/>
            <library name="qcncodecs" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/codecs" load="never" />
            <library name="qjpcodecs" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/codecs" load="never" />
            <library name="qkrcodecs" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/codecs" load="never" />
            <library name="qtwcodecs" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/codecs" load="never" />
            <library name="qtaccessiblewidgets" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/accessible" load="never"/>
            <library name="qsqlite" type="plugin" rootPath="${qtjambi.qtdir}" subdir="plugins/sqldrivers" load="never" if="${qtjambi.sqlite}" />

            <!-- Qt Jambi Libraries -->
            <library name="qtjambi"                  type="qtjambi" rootPath="${outputDir}" />
            <library name="com_trolltech_qt_core"    type="qtjambi" rootPath="${outputDir}" />
            <library name="com_trolltech_qt_gui"     type="qtjambi" rootPath="${outputDir}" />
            <library name="com_trolltech_qt_xml"     type="qtjambi" rootPath="${outputDir}" />
            <library name="com_trolltech_qt_sql"     type="qtjambi" rootPath="${outputDir}" />
            <library name="com_trolltech_qt_svg"     type="qtjambi" rootPath="${outputDir}" />
            <library name="com_trolltech_qt_network" type="qtjambi" rootPath="${outputDir}" />
            <library name="com_trolltech_qt_opengl"  type="qtjambi" rootPath="${outputDir}" />
            <library name="com_trolltech_qt_phonon"  type="qtjambi" rootPath="${outputDir}" if="${qtjambi.phonon}"/>
            <library name="com_trolltech_qt_webkit"  type="qtjambi" rootPath="${outputDir}" if="${qtjambi.webkit}"/>
            <library name="com_trolltech_qt_xmlpatterns"  type="qtjambi" rootPath="${outputDir}" if="${qtjambi.xmlpatterns}"/>

        <plugin path="plugins" />

        </qtjambi-platform-jar>

        <jar destfile="${outputDir}/qtjambi-${qtjambi.osname}-${qtjambi.compiler}-${qtjambi.version}.jar">
            <fileset dir="${outputDir}/platform-output" />
            <manifest>
                <attribute name="Built-By" value="${user.name} - ${TODAY}"/>
            </manifest>
        </jar>

        <delete dir="${outputDir}/platform-output"/>

    </target>

    <target name="generator.xmlmerge"
        description="Merges the XML files used by the Qt Jambi generator.">
        <xslt in="${sourceDir}/generator/typesystem_core-common.xml"
              out="${outputDir}/generator/typesystem_core.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_core-java.xml"/>
        </xslt>
        <xslt in="${sourceDir}/generator/typesystem_gui-common.xml"
              out="${outputDir}/generator/typesystem_gui.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_gui-java.xml"/>
        </xslt>
        <xslt in="${sourceDir}/generator/typesystem_network-common.xml"
              out="${outputDir}/generator/typesystem_network.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_network-java.xml"/>
        </xslt>
        <xslt in="${sourceDir}/generator/typesystem_svg-common.xml"
              out="${outputDir}/generator/typesystem_svg.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_svg-java.xml"/>
        </xslt>
        <xslt in="${sourceDir}/generator/typesystem_xml-common.xml"
              out="${outputDir}/generator/typesystem_xml.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_xml-java.xml"/>
        </xslt>
        <xslt in="${sourceDir}/generator/typesystem_xmlpatterns-common.xml"
              out="${outputDir}/generator/typesystem_xmlpatterns.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_xmlpatterns-java.xml"/>
        </xslt>
        <xslt in="generator/typesystem_opengl-common.xml"
              out="${outputDir}/generator/typesystem_opengl.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_opengl-java.xml"/>
        </xslt>
        <xslt in="${sourceDir}/generator/typesystem_sql-common.xml"
              out="${outputDir}/generator/typesystem_sql.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_sql-java.xml"/>
        </xslt>
        <xslt in="${sourceDir}/generator/typesystem_designer-common.xml"
              out="${outputDir}/generator/typesystem_designer.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_designer-java.xml"/>
        </xslt>
         <xslt in="${sourceDir}/generator/typesystem_webkit-common.xml"
              out="${outputDir}/generator/typesystem_webkit.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_webkit-java.xml"/>
        </xslt>
        <xslt in="${sourceDir}/generator/typesystem_phonon-common.xml"
              out="${outputDir}/generator/typesystem_phonon.xml"
              style="${sourceDir}/generator/merge.xsl"
              force="yes">
            <param name="source"
                   expression="typesystem_phonon-java.xml"/>
        </xslt>
    </target>

    <target name="init"
        depends="init.ant, init.taskdef"
        description="Initializes the Qt Jambi build environment." >
      <qtjambi-initialize verbose="true" configuration="${qtjambi.config}" />
    </target>

    <target name="generator"
        depends="init, generator.xmlmerge, generator.qmake, generator.compile, generator.run"
        description="Compiles and runs the Qt Jambi Generator."/>

    <target name="library.native"
        depends="library.native.qmake, library.native.compile, library.native.bundle"
        description="Compiles and bundles the Qt Jambi native libraries."/>

    <target name="library.java"
        depends="library.java.compile, library.java.bundle"
        description="Compiles and bundles the Qt Jambi java libraries."/>

    <target name="library.designer"
        depends="library.designer.compile, library.designer.bundle"
        description="Compiles and bundles the Qt Jambi designer libraries."/>

    <target name="library"
        depends="init, library.native, library.java, library.designer"
        description="Compiles and bundles the Qt Jambi libraries."/>

    <target name="examples"
        depends="init, examples.juic, examples.bundle"
        description="Compiles and bundles the Qt Jambi examples."/>

    <target name="all"
            description="Generates, builds and bundles the Qt Jambi libraries."
            depends="init, generator, library, examples" />
</project>
