<project name="B649 Assignment Five" default="compile" basedir=".">

  <!-- Load in the property file for this build,
       Must define:
         project.src.home
         project.build.home
         project.lib.home
         test.src.home
         test.build.home
         javadoc.home
   -->
  <property file="build.properties" />

  <!-- Set up the CLASSPATH, includes all jars in the lib
       directory and all built files for both the main project
       and the tests -->
  <path id="project.classpath">
    <pathelement location="." />
    <fileset dir="${project.lib.home}">
       <include name="**/*.jar" />
    </fileset>
    <pathelement location="${project.build.home}" />
    <pathelement location="${test.build.home}" />
  </path>

  <!-- Special CLASSPATH that does not include the jalopy jar file.  This
       prevents runtime loader errors due to collision of differing InputSource
       classes from SAX. -->
  <path id='nojalopy.classpath'>
    <pathelement location="." />
    <fileset dir="${project.lib.home}/junit3.8.1">
       <include name="**/*.jar" />
    </fileset>
    <pathelement location="${project.build.home}" />
    <pathelement location="${test.build.home}" />
  </path>


  <!-- This is required for using the jalopy task. -->
  <target name="define-jalopy">
    <taskdef name="jalopy"
       classname="de.hunsicker.jalopy.plugin.ant.AntPlugin">
       <classpath refid="project.classpath"/>
    </taskdef>
  </target>

  <target name="help">
    <echo>
    Available targets:
      compile - Compile all the source code and tests.
      clean - Remove all compiled source and test classes.
      docs - Create JavaDoc documentation for all source code.
      test - Run all JUnit in the java/test/ hierarchy.
      checkout - This will get all the new code from CVS and merge
                 it with your code.
      checkin - This will check all of the files you have changed
                into CVS. NOTE: This will also convert the layout of
                                all you code to the (sun-style)
                                 layout used in CVS!
      handin-style - Change the style of all the code to the
                     KnownSpace style.
      codebase-style - Change the style of all the code to the
                       style used in CVS.
    </echo>
  </target>

  <!-- Prepare for a build by creating appropriate directories -->
  <target name="prepare">
    <mkdir dir="${project.build.home}"/>
    <mkdir dir="${test.build.home}"/>
  </target>

  <!-- Clean up build by deleting directories -->
  <target name="clean">
    <delete dir="${project.build.home}"/>
    <delete dir="${test.build.home}"/>
  </target>

  <!-- Compile the project source files -->
  <target name="compile" depends="prepare">
    <javac srcdir="${project.src.home}"
           destdir="${project.build.home}"
           debug="on"
           optimize="off"
           deprecation="on"
           source="1.4">
       <classpath refid="project.classpath" />
     </javac>
  </target>

  <!-- Run it! -->
  <target name='run' depends='compile, testClasses'>
    <java classname='${knownspace.rootclass}'
          fork='yes'>
      <classpath refid='project.classpath' />
    </java>
  </target>

  <!-- Build project documentation -->
  <target name="docs">
    <javadoc sourcepath="${project.src.home}"
             destdir="${javadoc.home}"
             packagenames="*"
             source="1.4"
             link="http://java.sun.com/j2se/1.4/docs/api/"/>
  </target>

  <!-- Build private-level documentation -->
  <target name="private-docs">
    <javadoc sourcepath="${project.src.home}"
             destdir="${javadoc.private.home}"
             packagenames="*"
             link="http://java.sun.com/j2se/1.4/docs/api/"
             source="1.4"
             access="private"/>
  </target>

  <!-- Compile Tests -->
  <target name="testClasses" depends="prepare, compile">
    <javac srcdir="${test.src.home}"
           destdir="${test.build.home}"
           source="1.4">
      <classpath refid="project.classpath" />
    </javac>
  </target>

  <!-- Run Tests -->
  <target name="test" depends="testClasses">
    <junit haltonfailure="no" printsummary="yes">
      <classpath refid="nojalopy.classpath" />
      <batchtest>
        <fileset dir="${test.build.home}">
          <include name="**/*Tester.class" />
        </fileset>
        <formatter usefile="no" type="plain" />
      </batchtest>
    </junit>
  </target>


  <!-- Update the source that has been checked out. -->
  <target name="checkout">
     <cvs command="update -d"
        failonerror="yes"
        cvsRoot="${cvs.root}"/>
  </target>


  <!-- Checkin the source that has been modified.
       The code is transformed using the codebase-format task and
       then checked into CVS.  -->
  <target name="checkin" depends="codebase-style, compile, test">
     <cvs command="commit ${project.src.home} ${test.src.home}"
        failonerror="yes"
        cvsRoot="${cvs.root}"/>
  </target>


  <!-- This should be run only once to initially checkout the code. -->
  <target name="setup">
     <echo>
     Please make sure that you have run: cvs -d ${cvs.root} login
     </echo>

     <cvs command="checkout"
        package="${cvs.module}"
        failonerror="yes"
        cvsRoot="${cvs.root}"/>

     <copy file="build.properties" toDir="${cvs.module}"/>

     <echo>
     The code has been successfully retrieved.
     You can now move into the ${cvs.module} directory and start working!
     </echo>
  </target>


  <!-- Use Jalopy to reformat all of the source code.
       This is dependant on "compile" because only working code
       should be passed to Jalopy. -->
  <target name="handin-style" depends="define-jalopy, prepare, compile">
     <jalopy style="b649style.xml"
        classpathref="project.classpath">
        <fileset dir="${project.src.home}">
           <include name="**/*.java"/>
        </fileset>
        <fileset dir="${test.src.home}">
           <include name="**/*.java"/>
        </fileset>
     </jalopy>
  </target>

  <!-- Use Jalopy to reformat all of the source code.
       This is dependant on "compile" because only working code
       should be passed to Jalopy. -->
  <target name="codebase-style" depends="define-jalopy, prepare, compile">
     <jalopy style="codebase-style.xml"
        classpathref="project.classpath">
        <fileset dir="${project.src.home}">
           <include name="**/*.java"/>
        </fileset>
        <fileset dir="${test.src.home}">
           <include name="**/*.java"/>
        </fileset>
     </jalopy>
  </target>
</project>
