<project name="ant1-compatibility" default="main">

    <property name="ant1compat.source" value="src/ant1compat"/>

    <property name="manifest.dir" value="src/manifest"/>
    <property name="java.dir" value="src/java"/>

    <property name="build.dir" value="build/ant1compat"/>
    <property name="build.lib" value="${build.dir}/lib"/>
    <property name="build.classes" value="${build.dir}/classes"/>

    <property name="custom-tasks-dir" value="build/tasks"/>

    <path id="project.class.path">
        <pathelement location="build/classes"/>
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
    </path>

    <taskdef name="antlib-jar"
             classname="org.apache.myrmidon.build.AntlibJarTask">
        <classpath location="${custom-tasks-dir}"/>
    </taskdef>

    <target name="main" depends="antlib"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <!-- Builds the Ant1 compatibility layer -->
    <target name="antlib"
            description="Builds the Ant1 compatibility layer.">

        <property name="ant1compat.dir" value="src/ant1compat"/>
        <property name="ant1.jar" value="${ant1compat.dir}/jar/ant.jar"/>
        <property name="ant1optional.jar" value="${ant1compat.dir}/jar/optional.jar"/>

        <property name="ant1.package" value="org/apache/tools"/>

        <property name="build.ant1classes" value="${build.dir}/ant1classes"/>

        <mkdir dir="${build.ant1classes}"/>
        <mkdir dir="${build.classes}"/>
        <mkdir dir="${build.lib}"/>

        <unjar src="${ant1.jar}" dest="${build.ant1classes}" overwrite="no"/>
        <unjar src="${ant1optional.jar}" dest="${build.ant1classes}" overwrite="no"/>

        <javac destdir="${build.classes}"
               debug="on"
               includeAntRuntime="false">
            <classpath>
                <path refid="project.class.path"/>
                <pathelement location="${build.ant1classes}"/>
            </classpath>
            <src location="${ant1compat.dir}"/>
            <include name="${ant1.package}/**"/>
        </javac>

        <patternset id="ant1.omit">
          <exclude name="${ant1.package}/ant/Main.class"/>
          <exclude name="${ant1.package}/ant/Task.class"/>
          <exclude name="${ant1.package}/ant/BuildException.class"/>
          <exclude name="${ant1.package}/ant/types/Path.class"/>
        </patternset>

        <property name="antlib.file" value="${build.lib}/ant1compat.atl"/>

        <!-- Create the ant1compat antlib -->
        <antlib-jar jarfile="${antlib.file}"
                    descriptor="${ant1compat.dir}/ant-descriptor.xml"
                    rolesDescriptor="${manifest.dir}/empty-roles.xml"
                    manifest="${ant1compat.dir}/ant1compat.mf">
            <fileset dir="${build.ant1classes}">
                <include name="${ant1.package}/**"/>
                <patternset refid="ant1.omit"/>
            </fileset>
            <fileset dir="${build.classes}">
                <include name="${ant1.package}/**"/>
            </fileset>
        </antlib-jar>

        <copy todir="dist/lib" file="${antlib.file}"/>
    </target>

    <!-- Runs the supplied build file through the XSL converter -->
    <target name="ant1convert"
            description="Converts an Ant1 build file into a Myrmidon build file.">
        <property name="ant1file" value="build.xml"/>

        <style
            style="${java.dir}/org/apache/myrmidon/components/builder/ant1convert.xsl"
            in="${ant1file}"
            out="${ant1file}.ant"/>
    </target>

</project>