<project name="junit" default="dist" basedir=".">
	<tstamp />

	<property file="${user.home}/.junit.properties" />
	<property name="src" value="src/main/java" />
	<property name="target" location="target" />
	<property name="bin" location="${target}/main" />
	<property name="version" value="4.5" />
	<property name="dist" value="junit${version}" />
	<property name="versionfile" value="${src}/junit/runner/Version.java" />
	<property name="zipfile"  value="${dist}/${dist}.zip" />
	<property name="testsrc" location="src/test/java" />
	<property name="testbin" location="${target}/test/java" />
	<property name="unjarred" value="**/*.jar, ${testfiles}, doc/**, README.html, .classpath, .project, cpl-v10.html" />
	<property name="binjar" value="junit-${version}.jar" />
	<property name="depjar" value="junit-dep-${version}.jar" />
	<property name="srcjar" value="junit-${version}-src.jar" />
	<property name="javadocdir" location="${dist}/javadoc" />
	<property name="javadoczip" location="${dist}-javadoc.zip" />
	<property name="javadocpackages" value="org.junit, org.junit.runner, org.junit.runner.description, org.junit.runner.manipulation, org.junit.runner.notification, org.junit.runners, org.hamcrest.core, org.junit.matchers" />
	<property name="hamcrestlib" location="lib/hamcrest-core-1.1.jar" />
	<property name="hamcrestsrc" location="${dist}/temp.hamcrest.source" />
	
	<target name="init">
		<tstamp/>
	</target>

	<target name="versiontag" depends="init">
		<filter token="version" value="${version}" />
		<copy 
			file="${versionfile}.template" 
			tofile="${versionfile}" 
			filtering="on"
                        overwrite="true"
		/>
	</target>

	<target name="clean">
		<!-- If two builds are made within a minute -->
		<delete dir="${dist}" quiet="true" />
		<!-- Delete all previous temporary build artifacts -->
		<delete dir="${target}" quiet="true" />
		
		<delete file="${zipfile}" quiet="true"/>
		<delete file="${javadoczip}" />
		 
		<!-- What is this good for? It will clear out earlier dist builds and the eclipse bin folder. Remove?
		<delete>
			<fileset dir="${basedir}" includes="**/*.class" />
		</delete>
		-->
	</target>

	<macrodef name="junit_compilation">
		<attribute name="srcdir"/>
		<attribute name="destdir"/>
		<attribute name="classpath"/>
		<sequential>
			<mkdir dir="@{destdir}"/>
			<javac 
				srcdir="@{srcdir}"
				destdir="@{destdir}"
				debug="on"
				classpath="@{classpath}"
				includeantruntime="false"
				target="1.5"
	  		>
				<compilerarg value="-Xlint:unchecked" />
			</javac>
		</sequential>
	</macrodef>
	
	<target name="build" depends="versiontag">
		<junit_compilation srcdir="${src}" destdir="${bin}" classpath="${hamcrestlib}"/>
		<unjar src="${hamcrestlib}" dest="${bin}" />
		<junit_compilation srcdir="${testsrc}" destdir="${testbin}" classpath="${hamcrestlib};${bin}"/>
	</target>

	<target name="jars">
		<mkdir dir="${dist}" />
		<jar 
			jarfile="${dist}/${srcjar}"
			basedir="${src}"
			excludes="${unjarred}, **/*.class"
		/>
		<jar 
			jarfile="${dist}/${binjar}"
			basedir="${bin}"
			excludes="${unjarred}, **/*.java, build.xml"
		/>
		<jar 
			jarfile="${dist}/${depjar}" 
			basedir="${bin}" 
			excludes="${unjarred}, org/hamcrest/**, **/*.java, build.xml" 
		/>
	</target>

	<target name="samples-and-tests">
		<copy todir="${dist}">
			<fileset dir="${testbin}" />
			<fileset dir="${testsrc}" />
		</copy>
	</target>

	<target name="unjar.hamcrest">
		<unjar src="${hamcrestlib}" dest="${hamcrestsrc}" />
	</target>
	
	<target name="javadoc" depends="unjar.hamcrest">
		<javadoc packagenames="${javadocpackages}"
		         destdir="${javadocdir}"
		         author="false"
		         version="false"
		         use="false"
		         windowtitle="JUnit API"
		         stylesheetfile="stylesheet.css"
		>
			<sourcepath location="${src}" />
			<sourcepath location="${hamcrestsrc}" />
		</javadoc>
	</target>

	<target name="javadoczip">
		<delete file="${javadoczip}" />
		<antcall target="javadoc" />
		<zip basedir="${javadocdir}" file="${javadoczip}" />
	</target>

	<target name="populate-dist" 
		    depends="clean, build, jars, samples-and-tests, javadoc"
	>
		<copy todir="${dist}/doc">
			<fileset dir="doc"/>
		</copy>
		<copy file="README.html" tofile="${dist}/README.html" />
		<copy file="cpl-v10.html" tofile="${dist}/cpl-v10.html" />
		<copy file="build.xml" tofile="${dist}/build.xml" />
	</target>

	<target name="dist" depends="populate-dist">
		<java classname="org.junit.runner.JUnitCore" fork="yes" failonerror="true">
			<arg value="org.junit.tests.AllTests"/>
			<classpath>
				<pathelement location="${dist}" />
				<pathelement location="${dist}/${binjar}" />
			</classpath>
		</java>
	</target>

	<!-- TODO: DUP above -->

	<target name="profile" depends="populate-dist">
		<java classname="org.junit.runner.JUnitCore" fork="yes" failonerror="true">
			<jvmarg value="-agentlib:hprof=cpu=samples"/>
			<arg value="org.junit.tests.AllTests"/>
			<classpath>
				<pathelement location="${dist}" />
				<pathelement location="${dist}/${binjar}" />
			</classpath>
		</java>
	</target>	

	<target name="zip" depends="dist">
		<zip zipfile="${zipfile}" basedir="." includes="${dist}/**" />
	</target>

	<target name="upload.to.sourceforge" depends="zip">
		<ftp server="upload.sourceforge.net"
		     userid="anonymous"
		     password="saff@mit.edu"
		     remotedir="incoming"
		>
			<fileset dir="${dist}" includes="*.jar" />
			<fileset file="${zipfile}" />
		</ftp>
		<echo message="To upload docs, use build/upload_docs.sh" />
	</target>
</project>
