<project name="util" default="compile" basedir=".">
  <property name="package" value="com.kstroke.util" />
  <property name="src.dir" value="src" />
  <property name="build.dir" value="build" />
  <property name="build.classes.dir" value="${build.dir}/classes" />
  <property name="test.dir" value="${build.dir}/test" />
  <property name="test.data.dir" value="${test.dir}/data" />
  <property name="test.reports.dir" value="${test.dir}/reports" />
  <property name="test.last.failed.file" location="${build.dir}/.lasttestsfailed" />
  <property name="lib.dir"  value="lib" />
  <property name="docs.dir" value="docs" />
  <property name="jarname"  value="kstrokeUtil.jar" />

  <property name="runClassName"  value="com.kstroke.util.Calendar" />
  <target name="run" depends="compile">
    <java classname="${runClassName}" fork="true" classpath="${build.classes.dir}" />
  </target>

  <path id="compile.classpath">
    <pathelement location="" />
  </path>

  <path id="test.classpath">
    <path refid="compile.classpath" />
    <pathelement location="${junit.jar}" />
    <pathelement location="${build.classes.dir}" />
    <pathelement location="${test.dir}" />
  </path>

  <target name="init">
    <!-- Create the directory to dump the class files from compiling src -->
    <mkdir dir="${build.dir}" />
    <mkdir dir="${build.classes.dir}" />
    <!-- Create the directory for the jar file -->
    <mkdir dir="${lib.dir}" />
    <!-- Create the directory for the java docs -->
    <mkdir dir="${docs.dir}" />
  </target>

  <target name="compile" depends="init"> 
    <!-- Compile the java code -->
    <javac srcdir="${src.dir}" destdir="${build.classes.dir}">
      <classpath refid="compile.classpath"/>
    </javac>
  </target>



  <target name="test-init">
    <mkdir dir="${test.dir}" />
    <delete dir="${test.data.dir}" />
    <delete dir="${test.reports.dir}" />
    <mkdir dir="${test.data.dir}" />
    <mkdir dir="${test.reports.dir}" />
  </target>

  <target name="test-compile" depends="compile, test-init">
    <javac destdir="${test.dir}"
	   includeAntRuntime="true"
	   srcdir="test">
      <classpath refid="test.classpath"/>
    </javac>
    <copy todir="${test.dir}">
      <fileset dir="test" excludes="**/*.java"/>
    </copy>

    <condition property="tests.uptodate">
      <and>
        <uptodate>
          <srcfiles dir="${src.dir}" includes="**/*.java"/>
	  <mapper type="glob"
	          from="*.java"
		  to="${build.classes.dir}/*.class" />
        </uptodate>

        <uptodate>
          <srcfiles dir="${test.src.dir}" includes="**/*.java"/>
	    <mapper type="glob"
	            from="*.java"
	            to="${test.classes.dir}/*.class" />
        </uptodate>

        <uptodate>
          <srcfiles dir="${test.src.dir}" excludes="**/*.java"/>
	    <mapper type="glob"
	            from="*"
		    to="${test.classes.dir}/*" />
        </uptodate>

        <not>
          <available file="${test.last.failed.file}"/>
        </not>

        <not>
          <isset property="testcase"/>
        </not>

        <uptodate>
          <srcfiles dir="${test.src.dir}" includes="**/*.java"/>
	  <mapper type="package"
	          from="*Test.java"
		  to="${test.data.dir}/TEST-*Test.xml"/>
        </uptodate>

      </and>
    </condition>

  </target>

  <target name="test" depends="test-compile" unless="tests.uptodate">
    <!-- run the class -->
    <junit printsummary="false"
           errorProperty="test.failed"
	   failureProperty="test.failed"
	   haltonfailure="false">
      <classpath refid="test.classpath"/>
      <formatter type="brief" usefile="false" />
      <formatter type="xml" />
      <!-- single testcase usage: ant test -Dtestcase=<fully qualified classname> -->
      <test name="${testcase}" todir="${test.data.dir}" if="testcase"/>
      <batchtest toDir="${test.data.dir}" unless="testcase">
        <fileset dir="${test.dir}" includes="**/*Test.class" />
      </batchtest>
    </junit>

    <junitreport todir="${test.data.dir}">
      <fileset dir="${test.data.dir}">
        <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="${test.reports.dir}"/>
    </junitreport>

    <echo message="last build failed tests"
          file="${test.last.failed.file}"/>
    <fail if="test.failed">
      Unit tests failed. Check log or reports for details
    </fail>
    <!-- Remove test failed file, as these tests succeeded -->
    <delete file="${test.last.failed.file}"/>
  </target>
  
  <target name="docs" depends="compile">
    <!-- Create javadocs -->
    <javadoc packagenames="${package}"
	     sourcepath="${src}"
	     defaultexcludes="yes"
	     destdir="${docs}"
	     author="true"
	     version="true"
	     use="true"
	     windowtitle="Kstroke Util API Documentation">
    </javadoc>
  </target>

  <target name="jar" depends="compile">
    <!-- make a jar file -->
    <jar jarfile="${lib}/${jarname}" basedir="${build}/"/>
  </target>

  <target name="clean">
    <!-- Delete the created directory trees -->
    <delete dir="${build.dir}"/>
    <delete dir="${lib.dir}"/>
    <delete dir="${docs.dir}"/>
  </target>

</project> 


