<!-- ==================================================================== -->
<!-- Ant build file for Freenet-ext.                                      -->
<!-- Please download and install ant from http://ant.apache.org/          -->
<!-- ==================================================================== -->
<project name="Freenet-ext" default="jar">
	<property name="build" location="build_dir"/>
	<property name="jar.location" location="freenet-ext.jar"/>
	<property name="javac.target.version" value="1.5"/>

	<property name="junit.location" value="/usr/share/java/junit.jar"/>
	<property name="strip-modules" value="strip-modules@5ca910ce"/>
	
	<!-- set version string based on git-describe -->
	<exec executable="git" failifexecutionfails="false"
		errorProperty="git.errror" outputproperty="git.describe" resultproperty="git.result">
		<arg value="describe" />
		<arg value="--always" />
		<arg value="--abbrev=4" />
	</exec>
	<condition property="git.revision" value="${git.describe}" else="@unknown@">
		<and>
			<equals arg1="${git.result}" arg2="0" />
			<isset property="git.describe" />
		</and>
	</condition>
	

	<target name="init" description="Create build directory">
		<mkdir dir="${build}"/>
		<mkdir dir="${build}/freenet/node/"/>

		<!-- Update the Version file -->
		<replace file="ExtVersion.java">
			<replacefilter token="@custom@" value="${git.revision}"/>
		</replace>
		<javac srcdir="." destdir="${build}" optimize="on" source="${javac.target.version}">
			<include name="ExtVersion.java"/>
		</javac>
	</target>

	<target name="fec-common" depends="init" description="build required classes for FEC">
		<ant antfile="build.xml" dir="../fec/common/"/>
	</target>

	<target name="fec" depends="fec-common" description="build the fecencoder/decoder plugins for fproxy.">
		<javac destdir="${build}" optimize="on" source="${javac.target.version}">
			<src path="../fec/src"/>
			<classpath path="../fec/common/lib/onion-common.jar"/>
			<exclude name="csrc/*.java"/>
		</javac>

		<copy file="../fec/onion_LICENSE" todir="${build}"/>
		<copy todir="${build}">
			<fileset dir="../fec/">
				<include name="lib/"/>
			</fileset>
		</copy>
	</target>

	<target name="jcpuid" depends="init">
		<copy todir="${build}">
			<fileset dir="../jcpuid/lib" includes="freenet/**" />
		</copy>
	</target>

	<target name="bigint" depends="init">
		<copy todir="${build}">
			<fileset dir="../NativeBigInteger/lib" includes="net/**" />
		</copy>
	</target>

	<target name="nativethread" depends="init">
		<copy todir="${build}">
			<fileset dir="../NativeThread/lib" includes="freenet/**" />
		</copy>
	</target>

	<target name="bdb" depends="init" description="Build the BDB provider">
		<ant antfile="build.xml" dir="../bdb/" target="jar"/>
	</target>

	<target name="wrapper" depends="init" description="Build the java wrapper">
		<ant antfile="build.xml" target="jar" dir="../wrapper/">
			<property name="bits" value="${sun.arch.data.model}"/>
			<property name="javac.target.version" value="${javac.target.version}"/>
		</ant>
	</target>

	<target name="db4o" depends="init" description="Build db4o">
		<ant antfile="build.xml" target="jar" dir="../db4o/">
			<property name="javac.target.version" value="${javac.target.version}"/>
		</ant>
	</target>

	<target name="build-java" description="build the 3rd party code we bundle">
		<tstamp/>
		<javac srcdir="../java" destdir="${build}" debug="on" optimize="on" source="1.5">
			<classpath>
				<pathelement location="${build}"/>
			</classpath>
			<compilerarg value="-Xlint"/>
			<include name="**/*.java"/>
		</javac>

		<delete dir="${build}-test"/>
		<mkdir dir="${build}-test"/>

		<javac srcdir="../java-test" destdir="${build}-test" debug="on" optimize="on" source="1.5">
			<classpath>
				<pathelement location="${junit.location}"/>
				<pathelement location="${build}"/>
			</classpath>
			<compilerarg value="-Xlint"/>
			<include name="**/*.java"/>
		</javac>

		<junit printsummary="yes" fork="yes" haltonfailure="yes">
			<classpath>
				<pathelement path="${build}"/>
				<pathelement path="${build}-test"/>
				<pathelement location="${junit.location}"/>
			</classpath>
			<formatter type="plain" usefile="false"/>
			<batchtest fork="yes">
				<fileset dir="${build}-test">
					<include name="**/*Test.class"/>
				</fileset>
			</batchtest>
			<sysproperty key="benchmark" value="${benchmark}" />
			<sysproperty key="extensiveTesting" value="${extensiveTesting}" />
		</junit>

		<delete dir="${build}-test"/>
	</target>

	<target name="build-modules" description="build extra modules using strip-modules scripts">
		<ant inheritAll="false" antfile="${strip-modules}/build.xml" target="package" />
	</target>

	<target name="clean-all" depends="clean" description="More thorough version of clean">
		<ant antfile="build.xml" target="clean-javadoc" dir="../bdb"/>
		<ant antfile="build.xml" target="clean-jca" dir="../bdb"/>
		<!--WORKAROUND bug in wrapper's build.xml-->
		<mkdir dir="../wrapper/bin"/>
		<mkdir dir="../wrapper/lib"/>
		<mkdir dir="../wrapper/test"/>
		<ant antfile="build.xml" target="total-clean" dir="../wrapper">
			<property name="bits" value="${sun.arch.data.model}"/>
		</ant>
		<ant inheritAll="false" antfile="${strip-modules}/build.xml" target="clean-libsrc" />
	</target>

	<target name="clean" description="Cleanup the build directories">
		<delete file="${jar.location}"/>
		<delete dir="${build}"/>
		<ant antfile="build.xml" target="clean" dir="../bdb"/>
		<ant antfile="build.xml" target="clean" dir="../db4o"/>
		<ant antfile="build.xml" target="clean" dir="../fec/common"/>
		<ant antfile="build.xml" target="clean" dir="../wrapper">
			<property name="bits" value="${sun.arch.data.model}"/>
		</ant>
		<ant inheritAll="false" antfile="${strip-modules}/build.xml" target="clean" />
	</target>

	<target name="jar" depends="clean,fec,jcpuid,nativethread,bigint,bdb,wrapper,db4o,build-java,build-modules" description="Create the jar file">
		<delete file="${jar.location}"/>
		<jar jarfile="${jar.location}" basedir="${build}" includes="**" update="yes" duplicate="fail" level="9">
			<zipfileset src="../fec/common/lib/onion-common.jar"/>
			<zipfileset src="../bdb/build/lib/je.jar"/>
			<zipfileset src="../wrapper/lib/wrapper.jar"/>
			<zipgroupfileset dir="${strip-modules}/dist"/>
		</jar>
	</target>
</project>
