<?xml version="1.0" encoding="UTF-8"?>
<project name="simpledb" default="jar" basedir=".">
    <property name="src" location="src"/>
    <property name="test" location="src/unittest"/>
    <property name="bin" location="bin"/>
    <property name="lib" location="lib"/>
    <property name="doc" location="doc"/>
    <property name="dist" location="dist"/>
    <property name="jarfile" location="${dist}/${ant.project.name}.jar"/>
    <property name="compile.debug" value="true"/>
    <property name="test.reports" location="/tmp/${test.lab}/${test.user}"/>

    <fileset id="lib.jars" dir="${lib}">
        <include name="**/*.jar"/>
    </fileset>

    <path id="lib.path">
        <fileset refid="lib.jars"/>
    </path>

    <path id="classpath.base">
    </path>

    <path id="classpath.test">
        <fileset refid="lib.jars"/>
        <pathelement location="${jarfile}"/>
        <path refid="classpath.base"/>
    </path>

    <!-- Stub install target.  Install should depend on the 'jar' target,
         and copy the built objects to the 'dist' directory. -->
    <target name="install" description="Install jar" depends="jar">
    </target>

    <target name="compile" description="Compile code">
        <mkdir dir="${bin}"/>
        <mkdir dir="${lib}"/>
        <javac srcdir="${src}" destdir="${bin}" includeAntRuntime="no"
               classpathref="lib.path" debug="${compile.debug}">
            <!-- <compilerarg value="-Xlint:unchecked" /> -->
        </javac>
    </target>
   
    <target name ="doc" description="Compile Documentation"> 
        <javadoc destdir="${doc}" access="protected">
            <classpath refid="classpath.test" />
            <fileset dir="src" defaultexcludes="yes">
                <include name="simpledb/*"/>
                <include name="simpledb/test/*.java"/>
                <include name="simpledb/unittest/*.java"/>
            </fileset>
        </javadoc>
    </target>
 
    <target name="jar" depends="compile" description="Build jar">
        <mkdir dir="${dist}"/>
        <jar jarfile="${jarfile}" basedir="${bin}" manifest="Manifest">
            <!-- Merge library jars into final jar file -->
            <zipgroupfileset refid="lib.jars"/>
        </jar>
    </target>

    <target name="run" depends="jar" description="Run jar file">
        <java jar="${jarfile}" fork="yes" failonerror="true"/>
    </target>

    <target name="clean" description="Remove build and dist directories">
        <delete dir="${bin}"/>
        <delete dir="${dist}"/>
    </target>

    <target name="test" depends="jar" 
        description="Run all unit tests over package classes">
        <junit printsummary="off" haltonfailure="yes">
            <classpath refid="classpath.test" />
            <formatter type="plain" usefile="false"/>
            <batchtest fork="yes">
                <fileset dir="${src}">
                    <include name="**/unittest/*Test*.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="ensure-test-name" unless="test">
        <fail message="You must run this target with -Dtest=TestName"/>
    </target>

    <target name="runtest" description="Runs the test you specify on the command 
        line with -Dtest=" depends="jar, ensure-test-name">
        <junit printsummary="on" fork="yes"> 
            <classpath refid="classpath.test" />
            <formatter type="plain" usefile="false"/>
            <batchtest>
                <fileset dir="${src}">
                    <include name="**/unittest/${test}.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <!-- The following targets are used for automated grading. -->

    <target name="ensure-testuser-name" unless="test.user">
        <fail message="You must run this target with -Dtest.user=username"/>
    </target>

    <target name="ensure-testlab-name" unless="test.lab">
        <fail message="You must run this target with e.g. -Dtest.lab=lab1"/>
    </target>

    <target name="test-report" description="Generates HTML reports over all
        the tests in the directory ${test.reports}" 
        depends="jar, ensure-testuser-name, ensure-testlab-name">

        <junit printsummary="off" haltonfailure="no"> 
            <classpath refid="classpath.test" />
            <formatter type="xml"/>
            <formatter type="plain" usefile="true"/>

            <batchtest fork="yes" todir="${test.reports}" >
                <fileset dir="${src}">
                    <include name="**/unittest/*Test*.java"/>
                </fileset>
            </batchtest>
        </junit>

        <junitreport todir="${test.reports}">
            <fileset dir="${test.reports}">
                <include name="TEST-*.xml" />
            </fileset>
            <report todir="${test.reports}" />
        </junitreport>
    </target>

</project>
