<project default="main" name="Mutant">

  <property name="src.dir" value="src"/>
  <property name="lib.dir" value="lib"/>
  <property name="java.dir" value="${src.dir}/java"/>

  <property name="bin.dir" value="bin"/>
  <property name="dist.dir" value="dist"/>
  <property name="javadocs.dir" value="${dist.dir}/javadocs"/>
  <property name="distlib.dir" value="${dist.dir}/lib"/>
  
  <property name="debug" value="true"/>

  <path id="classpath.parser">
    <fileset dir="${lib.dir}/parser" includes="*.jar"/>
  </path>
  
  <path id="classpath.common">
    <pathelement location="${distlib.dir}/init.jar"/>
  </path>

  <path id="classpath.antcore">
    <path refid="classpath.common"/>
    <pathelement location="${distlib.dir}/common/common.jar"/>
    <path refid="classpath.parser"/>
  </path>

  <path id="classpath.cli">
    <path refid="classpath.antcore"/>
    <pathelement location="${distlib.dir}/antcore/antcore.jar"/>
  </path>
  
  <path id="classpath.start">
    <pathelement location="${distlib.dir}/init.jar"/>
  </path>

  <target name="buildsetup">
    <available classname="com.puppycrawl.tools.checkstyle.CheckStyleTask" 
               property="checkstyle.available"/>
    <mkdir dir="${bin.dir}"/>
    <mkdir dir="${distlib.dir}"/>
    <copy todir="${distlib.dir}/parser">
      <fileset dir="${lib.dir}/parser"/>
    </copy>
  </target>

  <target name="init" depends="buildsetup">
    <mkdir dir="${bin.dir}/init"/>
    <depend destdir="${bin.dir}/init" srcdir="${java.dir}/init"/>
    <javac destdir="${bin.dir}/init" srcdir="${java.dir}/init" debug="${debug}"/>
    <jar basedir="${bin.dir}/init" jarfile="${distlib.dir}/init.jar"/>
  </target>

  <target name="common" depends="init">
    <mkdir dir="${bin.dir}/common"/>
    <mkdir dir="${distlib.dir}/common"/>
    <depend destdir="${bin.dir}/common" srcdir="${java.dir}/common">
      <classpath refid="classpath.common"/>
    </depend>
    <javac destdir="${bin.dir}/common" srcdir="${java.dir}/common" debug="${debug}">
      <classpath refid="classpath.common"/>
    </javac>
    <jar basedir="${bin.dir}/common" jarfile="${distlib.dir}/common/common.jar"/>
  </target>

  <target name="antcore" depends="common">
    <mkdir dir="${bin.dir}/antcore"/>
    <mkdir dir="${distlib.dir}/antcore"/>
    <depend destdir="${bin.dir}/antcore" srcdir="${java.dir}/antcore">
      <classpath refid="classpath.antcore"/>
    </depend>
    <javac destdir="${bin.dir}/antcore" srcdir="${java.dir}/antcore" debug="${debug}">
      <classpath refid="classpath.antcore"/>
    </javac>
    <jar basedir="${bin.dir}/antcore" jarfile="${distlib.dir}/antcore/antcore.jar"/>
  </target>

  <target name="cli" depends="antcore, start">
    <mkdir dir="${bin.dir}/cli"/>
    <mkdir dir="${distlib.dir}/cli"/>
    <depend destdir="${bin.dir}/cli" srcdir="${java.dir}/cli">
      <classpath refid="classpath.cli"/>
    </depend>
    <javac destdir="${bin.dir}/cli" srcdir="${java.dir}/cli" debug="${debug}">
      <classpath refid="classpath.cli"/>
    </javac>
    <jar basedir="${bin.dir}/cli" jarfile="${distlib.dir}/cli/cli.jar"/>
  </target>

  <target name="start" depends="init">
    <mkdir dir="${bin.dir}/start"/>
    <depend destdir="${bin.dir}/start" srcdir="${java.dir}/start">
      <classpath refid="classpath.start"/>
    </depend>
    <javac destdir="${bin.dir}/start" srcdir="${java.dir}/start" debug="${debug}">
      <classpath refid="classpath.start"/>
    </javac>
    <jar basedir="${bin.dir}/start" jarfile="${distlib.dir}/start.jar">
      <manifest>
        <attribute name="Main-Class" value="org.apache.ant.start.Main"/>
        <attribute name="Class-Path" value="init.jar"/>
      </manifest>
      <include name="org/apache/ant/**/*"/>
    </jar>
    <jar basedir="${bin.dir}/start" jarfile="${distlib.dir}/ant.jar">
      <manifest>
        <attribute name="Main-Class" value="org.apache.tools.ant.Main"/>
        <attribute name="Class-Path" value="start.jar"/>
      </manifest>
      <include name="org/apache/tools/ant/**/*"/>
    </jar>
  </target>

  <target name="ant1compat" depends="common">
    <ant antfile="build/ant1compat.xml" />
  </target>
  
  <target name="remote" depends="init">
    <mkdir dir="${bin.dir}/remote"/>
    <depend destdir="${bin.dir}/remote" srcdir="${java.dir}/remote">
      <classpath refid="classpath.start"/>
    </depend>
    <javac destdir="${bin.dir}/remote" srcdir="${java.dir}/remote" debug="${debug}">
      <classpath refid="classpath.start"/>
    </javac>
    <jar basedir="${bin.dir}/remote" jarfile="${distlib.dir}/remote.jar">
      <manifest>
        <attribute name="Main-Class" value="org.apache.ant.remote.RemoteMain"/>
      </manifest>
    </jar>
  </target>
  
  <target name="clean">
    <delete dir="${bin.dir}"/>
    <delete dir="${dist.dir}"/>
  </target>

  <target name="antlibs" depends="common">
    <antcall target="build-lib" inheritall="false">
      <param name="libset" value="system"/>
    </antcall>
    <ant antfile="build/script.xml" inheritAll="false"/> 
  </target>
  
  <target name="build-lib">
    <mkdir dir="${bin.dir}/antlibs/${libset}"/>
    <mkdir dir="${distlib.dir}/antlibs"/>
    <path id="classpath.antlibs">
      <path refid="classpath.common"/>
      <pathelement location="${distlib.dir}/common/common.jar"/>
    </path>
    <depend destdir="${bin.dir}/antlibs/${libset}" srcdir="${java.dir}/antlibs/${libset}">
      <classpath refid="classpath.antlibs"/>
    </depend>
    <javac destdir="${bin.dir}/antlibs/${libset}" srcdir="${java.dir}/antlibs/${libset}" debug="${debug}">
      <classpath refid="classpath.antlibs"/>
    </javac>
    <jar basedir="${bin.dir}/antlibs/${libset}" jarfile="${distlib.dir}/antlibs/${libset}.jar">
      <metainf dir="${java.dir}/antlibs/${libset}" 
                includes="antlib.xml"/>
    </jar>
  </target>

  <target name="main" depends="cli, antlibs, ant1compat, remote, checkstyle"/> 
  
  <target name="checkstyle" if="checkstyle.available">
    <taskdef name="checkstyle"
             classname="com.puppycrawl.tools.checkstyle.CheckStyleTask"/>
    <mkdir dir="${bin.dir}/check"/>
    <checkstyle maxlinelen="80"
                memberpattern="[a-z].*"
                parampattern="[a-z].*"
                staticpattern="[a-z].*"
                ignoreCastWhitespace="true">
      <fileset dir="${java.dir}">
        <include name="**/*.java"/>
        <exclude name="**/org/apache/tools/ant/Task.java"/>
        <exclude name="**/org/apache/tools/ant/ProjectComponent.java"/>
        <exclude name="**/org/apache/tools/ant/types/DataType.java"/>
        <exclude name="**/builder/MutantBuilder.java"/>
        <exclude name="**/builder/Ant1CompatBuilder.java"/>
      </fileset>
    </checkstyle>
    <checkstyle maxlinelen="80"
                memberpattern="[a-z].*"
                parampattern="[a-z].*"
                staticpattern="[a-z].*"
                allowProtected="true"
                ignoreCastWhitespace="true">
      <fileset dir="${java.dir}">
        <include name="**/org/apache/tools/ant/Task.java"/>
        <include name="**/org/apache/tools/ant/ProjectComponent.java"/>
        <include name="**/org/apache/tools/ant/types/DataType.java"/>
      </fileset>
    </checkstyle>
  </target>
  
  <target name="javadocs">
    <mkdir dir="${javadocs.dir}"/>
    <javadoc packagenames="org.apache.*"
             useexternalfile="yes"
             sourcepath="${java.dir}/antcore:${java.dir}/init:${java.dir}/common:${java.dir}/cli:${java.dir}/start"
             destdir="${javadocs.dir}"
             author="true" private ="true"
             version="true"
             windowtitle="Mutant API"
             doctitle="Mutant">
      <group title="Apache Ant Core" packages="org.apache.ant.antcore*" />

    </javadoc>
  </target>
</project>
