<?xml version="1.0" encoding="UTF-8"?>
<project name="Freenet dependencies" default="all" basedir=".">

	<!-- =================================================================== -->
	<!-- Global properties                                                   -->
	<!-- =================================================================== -->

	<!-- user overrides (properties are immutable, so set overrides first) -->
	<property file="override.properties" />
	<property file="build.properties" />
	<property file="source.properties" />
	<condition property="java.class.dirs" value="${java.class.dirs.user}" else="${java.home}/lib">
		<available file="${java.class.dirs.user}" type="dir"/>
	</condition>

	<property name="pkg.base" value=".."/>
	<property name="pkg.contrib" value="strip-modules@5ca910ce"/>
	<property name="main.dst" value="dist"/>

	<property name="tmp" value="tmp"/>
	<property name="lib" value="lib"/>

	<!-- =================================================================== -->
	<!-- Miscellaneous                                                       -->
	<!-- =================================================================== -->

	<target name="dist" depends="clean-all, all" description="clean-build everything"/>

	<target name="all" depends="package" description="build everything, incl. docs"/>

	<target name="clean-all" depends="clean, clean-libsrc" description="clean all build products and remote source code"/>

	<target name="debug">
		<echoproperties/>
	</target>

	<!-- =================================================================== -->
	<!-- Standard build                                                      -->
	<!-- =================================================================== -->

	<target name="init">
		<mkdir dir="${main.dst}"/>
	</target>

	<target name="package" depends="package-lib" description="build standard binary packages">
		<lib-copy lib="commons-compress"/>
		<lib-copy lib="bitcollider-core"/>
	</target>

	<target name="clean" description="clean standard build products">
		<delete dir="${main.dst}"/>
	</target>

	<!-- =================================================================== -->
	<!-- Build remote packages                                               -->
	<!-- =================================================================== -->

	<target name="package-lib" depends="prepare-libsrc" description="build external packages">
		<lib-sequential lib="commons-compress">
			<maven basedir="${pkg.base}/commons-compress" goal="package"/>
		</lib-sequential>
		<lib-sequential lib="bitcollider-core">
			<ant inheritAll="false" antfile="${pkg.base}/${pkg.contrib}/generic-build.xml"
			  dir="${pkg.base}/bitcollider-core" target="jar">
				<property name="jar" value="bitcollider-core.jar"/>
			</ant>
		</lib-sequential>
	</target>

	<target name="clean-lib" depends="prepare-libsrc" description="clean build products of external packages">
		<lib-sequential lib="commons-compress">
			<maven basedir="${pkg.base}/commons-compress" goal="clean"/>
		</lib-sequential>
		<lib-sequential lib="bitcollider-core">
			<ant inheritAll="false" antfile="${pkg.base}/${pkg.contrib}/generic-build.xml"
			  dir="${pkg.base}/bitcollider-core" target="clean">
				<property name="jar" value="bitcollider-core.jar"/>
			</ant>
		</lib-sequential>
	</target>

	<!-- =================================================================== -->
	<!-- Get remote packages                                                 -->
	<!-- =================================================================== -->

	<target name="get-libsrc">
		<lib-getsrc lib="commons-compress"/>
		<lib-getsrc lib="bitcollider-core"/>
	</target>

	<target name="prepare-libsrc" depends="get-libsrc" description="ensure that all remote source code is available">
		<lib-unpacksrc lib="commons-compress"/>
		<lib-unpacksrc lib="bitcollider-core"/>
	</target>

	<target name="clean-libsrc" description="remove all remote source code">
		<lib-delsrc lib="commons-compress"/>
		<lib-delsrc lib="bitcollider-core"/>
	</target>

	<!-- =================================================================== -->
	<!-- ANT dependencies                                                    -->
	<!-- =================================================================== -->

	<!-- required to work with commons-compress -->
	<include file="ant-task-maven.xml"/>

	<!-- required for "lib-getsrc" macro -->
	<taskdef resource="net/sf/antcontrib/antlib.xml">
		<classpath><pathelement location="${java.class.dirs}/ant-contrib.jar"/></classpath>
	</taskdef>

	<macrodef name="lib-report">
		<attribute name="lib" />
		<sequential>
			<if>
				<istrue value="${extlib.@{lib}.suppress}"/>
				<then><echo message="suppress @{lib} is ON"/></then>
			</if>
		</sequential>
	</macrodef>

	<macrodef name="lib-sequential">
		<attribute name="lib" />
		<element name="tasks" implicit="true" optional="true" />
		<sequential>
			<if>
				<istrue value="${extlib.@{lib}.suppress}"/>
				<else><tasks/></else>
			</if>
		</sequential>
	</macrodef>

	<macrodef name="lib-delsrc">
		<attribute name="lib" />
		<attribute name="dest" default="@{lib}"/>
		<sequential>
			<lib-sequential lib="@{lib}">
				<delete dir="${pkg.base}/@{dest}"/>
			</lib-sequential>
		</sequential>
	</macrodef>

	<macrodef name="lib-copy">
		<attribute name="lib" />
		<sequential>
			<lib-sequential lib="@{lib}">
				<copy tofile="${main.dst}/@{lib}.jar" file="${pkg.base}/@{lib}/${extlib.@{lib}.jar-dst}"/>
			</lib-sequential>
		</sequential>
	</macrodef>

	<macrodef name="lib-unpacksrc">
		<attribute name="lib" />
		<attribute name="dest" default="@{lib}"/>
		<sequential>
			<lib-report lib="@{lib}"/>
			<lib-sequential lib="@{lib}">
				<if>
					<available file="${pkg.base}/@{dest}" type="dir"/>
					<else>
						<delete dir="${tmp}"/>
						<mkdir dir="${tmp}"/>
						<basename property="extlib.@{lib}.filename" file="${extlib.@{lib}.location}"/>
						<untar dest="${tmp}" compression="gzip">
							<fileset dir="${lib}" includes="${extlib.@{lib}.filename}"><filename name="*.tar.gz"/></fileset>
						</untar>
						<untar dest="${tmp}" compression="bzip2">
							<fileset dir="${lib}" includes="${extlib.@{lib}.filename}"><filename name="*.tar.bz2"/></fileset>
						</untar>
						<untar dest="${tmp}">
							<fileset dir="${lib}" includes="${extlib.@{lib}.filename}"><filename name="*.tar"/></fileset>
						</untar>
						<unzip dest="${tmp}">
							<fileset dir="${lib}" includes="${extlib.@{lib}.filename}"><filename name="*.zip"/></fileset>
						</unzip>
						<move file="${tmp}/${extlib.@{lib}.top-dir}" tofile="${pkg.base}/@{dest}"/>
						<delete dir="${tmp}"/>
					</else>
				</if>
			</lib-sequential>
		</sequential>
	</macrodef>

	<macrodef name="lib-getsrc">
		<attribute name="lib" />
		<sequential>
			<lib-sequential lib="@{lib}">
				<basename property="extlib.@{lib}.filename" file="${extlib.@{lib}.location}"/>
				<condition property="extlib.@{lib}.available" else="false">
					<and>
						<available file="${lib}/${extlib.@{lib}.filename}"/>
						<checksum file="${lib}/${extlib.@{lib}.filename}" algorithm="SHA-256"
						  property="${extlib.@{lib}.sum-sha256}" />
					</and>
				</condition>
				<if>
					<istrue value="${extlib.@{lib}.available}"/>
					<else>
						<delete dir="${tmp}"/>
						<mkdir dir="${tmp}"/>
						<get src="${extlib.@{lib}.location}" dest="${tmp}/${extlib.@{lib}.filename}"
						  verbose="true" usetimestamp="true"/>
						<checksum file="${tmp}/${extlib.@{lib}.filename}" algorithm="SHA-256"
						  property="${extlib.@{lib}.sum-sha256}" verifyProperty="extlib.@{lib}.sum-sha256.remote" />
						<fail unless="${extlib.@{lib}.sum-sha256.remote}"
						  message="lib-getsrc: ${extlib.@{lib}.filename} checksum mismatch"/>
						<move file="${tmp}/${extlib.@{lib}.filename}" todir="${lib}"/>
						<delete dir="${tmp}"/>
					</else>
				</if>
			</lib-sequential>
		</sequential>
	</macrodef>

</project>
