<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="build" name="Java Information Dynamics Toolkit">
	<description>
		Build file for the Java Information Dynamics Toolkit
	</description>
	
	<!-- set global properties for this build -->
	<property name="version" value="1.3.1"/>
	<property name="mainfilename" value="infodynamics"/>
	<property name="jarplainname" value="${mainfilename}.jar" />
	<property name="jarversiondistnamezip" value="${mainfilename}-jar-${version}.zip" />
	<property name="distname" value="${mainfilename}-dist-${version}" />
	<property name="distnamezip" value="${distname}.zip" />
	<property name="src" location="java/source"/>
	<property name="bin" location="bin"/>
	<property name="unittestsouttoplevel" location="unittests"/>
	<property name="unittestssrc" location="java/unittests"/>
	<property name="unittestsbin" location="${unittestsouttoplevel}/bin"/>
	<property name="autoanalyserdemo" location="demos/java"/>
	<property name="reports.tests" location="${unittestsouttoplevel}/reports"/>
	<property name="javadocsdir" location="javadocs"/>
	<property name="versionfile" value="version-${version}.txt"/>
	
	<path id="project.classpath">
		<pathelement location="bin"/>
	</path>
	
	<!-- Make required directories -->
	<target name="init" description="Create the compiled code directories">
		<mkdir dir="${bin}"/>
		<mkdir dir="${unittestsbin}"/>
		<mkdir dir="${reports.tests}"/>
	</target>
	
	<!-- Compile the java toolkit -->
	<target name="compile" depends="init" description="compile the source and unittests">
		<!-- Compile to Java 6 to provide compatibility for users with older JREs.
		     Caveat: The flags here only check the language compatibility, but
		     may still use newer libraries which may cause issues for users with JDK 6.
		     Indeed, one gets the warning: "bootstrap class path not set in conjunction with -source 1.6"
		     To fix this, one would use the bootstrap classpath to point our JDK to an rt.jar
		     for Java 6. 
		     At this stage, I'm sure I'm not using new library calls from Java 7, so we can
		     ignore the warning, and I don't want to bother installing Java 6 just to compile
		     like this. I'll endeavour not to use JDK 7 libraries so as not to cause
		     any issues here ... -->
		<javac srcdir="${src}" destdir="${bin}" includeAntRuntime="false" target="1.6" source="1.6"/>
	</target>

	<!-- Jar the toolkit -->
	<target name="jar" depends="compile" description="Create the jar for distribution">
		<!-- Put everything in ${bin} into the infodynamics-${version}.jar file -->
		<jar jarfile="${jarplainname}" basedir="${bin}"/>
	</target>

	<!-- Compile and run the JUnit tests -->
	<target name="junit" depends="compile" description="Run the junit tests and make sure they compile">
		<!-- Compile the junit tests first -->
		<javac destdir="${unittestsbin}" includeAntRuntime="true">
			<!-- Need includeAntRuntime=true if you want to pick up junit.jar and ant-junit.jar in ANT_HOME/lib;
			     Otherwise you will need to set the classpath to include these here. -->
			<src path="${unittestssrc}"/>
			<classpath refid="project.classpath"/>
		</javac>
		<!-- Run the junit tests and make sure they complete ok -->
		<junit printsummary="yes" showoutput="yes" haltonfailure="yes" haltonerror="yes" includeantruntime="true">
			<classpath>
				<path refid="project.classpath"/>
				<pathelement path="${unittestsbin}"/>
			</classpath>
			<formatter type="plain"/>
			<batchtest todir="${reports.tests}"> <!-- Writes full reports with stdout and stderr to ${reports.tests} -->
				<fileset dir="${unittestsbin}">
					<include name="**/*.class"/>
					<exclude name="**/*AbstractTester.class"/>
					<exclude name="**/ActiveInfoStorageCalculatorCorrelationIntegrals.class"/>
					<exclude name="**/ActiveInfoStorageCalculatorKernelDirect.class"/>
				</fileset>
			</batchtest>
		</junit>
	</target>

	<!-- Compile the autoanalyser demo -->
	<target name="autoanalyser" depends="jar" description="compile the autoanalyser demo">
		<!-- Compile to Java 6 to provide compatibility for users with older JREs.
		     Caveat: The flags here only check the language compatibility, but
		     may still use newer libraries which may cause issues for users with JDK 6.
		     Indeed, one gets the warning: "bootstrap class path not set in conjunction with -source 1.6"
		     To fix this, one would use the bootstrap classpath to point our JDK to an rt.jar
		     for Java 6. 
		     At this stage, I'm sure I'm not using new library calls from Java 7, so we can
		     ignore the warning, and I don't want to bother installing Java 6 just to compile
		     like this. I'll endeavour not to use JDK 7 libraries so as not to cause
		     any issues here ... -->
		<javac srcdir="${autoanalyserdemo}" includes="infodynamics/demos/autoanalysis/*.java" excludes="infodynamics/demos/autoanalysis/GeneratedCalculator.java" includeAntRuntime="false" target="1.6" source="1.6">
			<classpath refid="project.classpath"/>
		</javac>
	</target>

	<!-- Make javadocs, excluding the classes we've derived from Apache Commons Math -->
	<target name="javadocs" depends="compile" description="Make the javadocs for the toolkit">
		<delete dir="${javadocsdir}"/>
		<javadoc destdir="${javadocsdir}">
			<fileset dir="${src}">
				<include name="**/*.java"/>
				<exclude name="**/commonsmath3/*.java"/>
				<exclude name="**/commonsmath3/**/*.java"/>
			</fileset>
		</javadoc>
		<!-- Change some of the style in the javadocs css for our lists: -->
		<concat destfile="${javadocsdir}/stylesheet.css" append="true">
		.block ul li {list-style:disc; margin-left: 20px;}
		.block ol li {list-style:decimal; margin-left: 40px;}
		.block ol li ol li {list-style:lower-alpha; margin-left: 40px;}
		.block ol li ul li {list-style:disc; margin-left: 40px;}
		.block ol li ul li ol li {list-style:lower-alpha; margin-left: 40px;}
		.block ol li ol li ol li {list-style:lower-roman; margin-left: 40px;}
		.block ul li ol li {list-style:decimal; margin-left: 20px;}
		</concat>
	</target>

	<!-- Clean up -->
	<target name="clean">
		<!-- Delete the compiled code directories -->
		<delete dir="${bin}"/>
		<delete dir="${unittestsbin}"/>
		<delete dir="${reports.tests}"/>
		<delete dir="${unittestsouttoplevel}"/>
		<delete dir="${javadocsdir}"/>
		<delete dir="demos/clojure/target"/>
		<delete file="demos/clojure/pom.xml"/>
		<delete file="demos/clojure/pom.xml.asc"/>
		<delete file="demos/clojure/project.clj"/>
		<delete file="${jarversiondistnamezip}"/>
		<delete file="${distnamezip}"/>
		<delete file="${jarplainname}"/>
		<delete>
			 <fileset dir="demos/AutoAnalyser" includes="GeneratedCalculator.*"/>
		</delete>
		<delete>
			 <fileset dir="demos/java/infodynamics/demos/autoanalysis" includes="GeneratedCalculator.*"/>
		</delete>
		<!-- Don't delete the readme and version files - the user may not have the template to recreate them from -->
	</target>
	
	<!-- Build the project jar for users -->
	<target name="build" depends="jar" description="user: build the project jar file">
		<echo message="${ant.project.name}: ${ant.file}"/>
	</target>

	<!--
	***********************************
	
	Developer builds below here
	
	***********************************
	-->

	<!-- Developer build - creates readme and version files -->
	<target name="readmefiles" description="developer: create the readme and version files from templates">
		<tstamp>
			<format property="TODAY_DATE" pattern="dd/MM/yyyy" locale="en,UK"/>
			<format property="TODAY_YEAR" pattern="yyyy" locale="en,UK"/>
		</tstamp>
		<copy file="readme-template.txt" toFile="readme.txt" failonerror="false" overwrite="true">
			<!-- The template file only exists in SVN - this won't crash ant 
			     if users try to run it though because failonerror="false" -->
			<filterset>
				<filter token="YEAR" value="${TODAY_YEAR}"/>
				<filter token="DATE" value="${TODAY_DATE}"/>
				<filter token="VERSION" value="${version}"/>
			</filterset>
		</copy>
		<copy file="version-template.txt" toFile="${versionfile}" failonerror="false">
			<!-- The template file only exists in SVN - this won't crash ant 
			     if users try to run it though because failonerror="false" -->
			<filterset>
				<filter token="YEAR" value="${TODAY_YEAR}"/>
				<filter token="DATE" value="${TODAY_DATE}"/>
				<filter token="VERSION" value="${version}"/>
			</filterset>
		</copy>
		<copy file="demos/clojure/deploy/project-template.clj" toFile="demos/clojure/deploy/project.clj" failonerror="false" overwrite="true">
			<!-- The template file only exists in SVN - this won't crash ant 
			     if users try to run it though because failonerror="false" -->
			<filterset>
				<filter token="VERSION" value="${version}"/>
			</filterset>
		</copy>
	</target>

	<!-- Developer build - builds everything and makes the jar only distribution file -->
	<target name="jardist" depends="build,readmefiles" description="developer: generate the jar distribution">
		<zip destfile="${jarversiondistnamezip}">
			<fileset file="${jarplainname}"/>
			<fileset file="license-gplv3.txt"/>
			<fileset file="readme.txt"/>
			<fileset file="${versionfile}"/>
			<zipfileset dir="notices" includes="**/*.*,**/*" prefix="notices"/>
		</zip>
	</target>
	
	<!-- Developer build - builds everything and makes the full distribution file -->
	<target name="dist" depends="jar,junit,autoanalyser,javadocs,readmefiles" description="developer: generate the full distribution">
		<echo message="${ant.project.name}: ${ant.file}"/>
		<zip destfile="${distnamezip}">
			<fileset file="build.xml"/>
			<fileset file="${jarplainname}"/>
			<fileset file="license-gplv3.txt"/>
			<fileset file="readme.txt"/>
			<fileset file="InfoDynamicsToolkit.pdf"/>
			<fileset file="JIDT-logo.png" erroronmissingdir="false"/> <!-- This file is missing in full repository versions -->
			<fileset file="${versionfile}"/>
			<zipfileset dir="java" includes="**/*.java" prefix="java"/>
			<zipfileset dir="demos" includes="**/*.*,**/*" excludes="clojure/deploy,clojure/deploy/*.*,python/*.pyc" prefix="demos"/>
			<zipfileset dir="javadocs" includes="**/*.*,**/*" prefix="javadocs"/>
			<zipfileset dir="notices" includes="**/*.*,**/*" prefix="notices"/>
			<zipfileset dir="tutorial" prefix="tutorial"/>
			<zipfileset dir="web" includes="JIDT-logo.png" prefix="" erroronmissingdir="false"/> <!-- This file is missing in zip dist versions -->
		</zip>
	</target>
</project>
