<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project
	name="common"
	xmlns:ac="antlib:net.sf.antcontrib">
	<echo
		message="${ant.project.name}: ${ant.file}" />
	<echo
		message="${os.name} ${os.arch} ${os.version}" />

	<!-- ==================================================== -->
	<!-- Main entry points -->
	<!-- ==================================================== -->

	<!-- Default is everything but deployment -->
	<target
		name="default"
		depends="package"
		description="Default target" />

	<target
		name="package"
		depends="checksum"
		description="Create the distributables and calculate checksums" />

	<target
		name="test"
		depends="unit-tests"
		description="Compile and run the tests" />

	<target
		name="clean"
		description="Delete all files that are generated by the build">
		<delete
			dir="${build.dir}" />
		<delete
			dir="${dist.dir}" />
	</target>

	<!-- override point. -->
	<!-- if the file build.properties exists, it is loaded and can set any other properties before we get there-->
	<property
		name="project.name"
		value="${ant.project.name}" />
	<property
		file="${user.home}/${project.name}.properties" />
	<echo
		message="Loaded properties from ${user.home}/${project.name}.properties" />
	<property
		file="${user.home}/build.properties" />
	<echo
		message="Loaded properties from ${user.home}/build.properties" />
	<property
		name="config.filename"
		value="${user.name}.properties" />
	<property
		file="cfg/${config.filename}" />
	<echo
		message="Loaded properties from cfg/${config.filename}" />
	<property
		file="build.properties" />

	<property
		name="src.dir"
		value="src" />
	<property
		name="testsrc.dir"
		value="testsrc" />
	<property
		name="lib.dir"
		location="lib" />
	<property
		name="ant.lib.dir"
		location="${ant.dir}/lib" />
	<property
		name="ant.cfg.dir"
		location="${ant.dir}/cfg" />
	<property
		name="junit.lib.dir"
		location="${ant.lib.dir}/junit" />

	<!-- ant-contrib properties -->
	<property
		name="ant-contrib.lib.dir"
		location="${ant.lib.dir}/ant-contrib" />

	<property
		name="build.dir"
		location="build" />
	<property
		name="build.classes.dir"
		location="${build.dir}/classes" />
	<property
		name="test.dir"
		location="${build.dir}/test" />

	<!-- set up distribution targets -->
	<property
		name="dist.dir"
		location="dist" />
	<property
		name="doc.dir"
		location="${dist.dir}/doc" />
	<property
		name="javadoc.dir"
		location="${dist.dir}/api" />

	<property
		name="build.debug"
		value="true" />
	<property
		name="target"
		value="1.6" />
	<property
		name="source"
		value="1.6" />

	<!-- what is our fork policy for junit -->
	<property
		name="junit.fork"
		value="true" />
	<property
		name="junit.forkmode"
		value="once" />

	<!-- pattern of source files to copy -->
	<property
		name="source.files.tocopy"
		value="**/*.properties,**/*.dtd,**/*.xml,**/*.xsd,**/*.jpg" />

	<property
		name="untar.dir"
		location="${build.dir}/untar" />
	<property
		name="unjar.dir"
		location="${build.dir}/unjar" />

	<property
		name="configuration"
		location="cfg/com/motlin/base/${ant.project.name}.properties" />

	<!-- ==================================================== -->
	<!-- inner targets and lesser entry points -->
	<!-- ==================================================== -->

	<!-- This is the updated compile target -->
	<target
		name="compile"
		depends="classpaths">
		<condition
			property="build.debuglevel"
			value="lines,source"
			else="lines,vars,source">
			<isset
				property="release.build" />
		</condition>
		<echo
			level="verbose">debug level=${build.debuglevel}</echo>
		<mkdir
			dir="${build.classes.dir}" />
		<javac
			destdir="${build.classes.dir}"
			debug="true"
			debuglevel="${build.debuglevel}"
			includeAntRuntime="false"
			srcdir="${src.dir}"
			source="${source}"
			target="${target}">
			<classpath
				refid="compile.classpath" />
		</javac>
		<!-- pull in copied files -->
		<copy
			todir="${build.classes.dir}">
			<fileset
				dir="src"
				includes="${source.files.tocopy}" />
		</copy>
	</target>

	<!-- create the JAR file -->
	<target
		name="jar"
		depends="compile,classpaths-extended">

		<property
			name="unsigned.dir"
			location="${build.dir}/unsigned" />
		<property
			name="unsigned.target.jar"
			value="${unsigned.dir}/${target.name}" />

		<property
			name="manifest.mf"
			location="${build.dir}/manifest.mf" />
		<manifest
			file="${manifest.mf}">
			<attribute
				name="Built-By"
				value="${user.name}" />
			<attribute
				name="Sealed"
				value="true" />
			<attribute
				name="Main-Class"
				value="${main.class}" />
		</manifest>
		<mkdir
			dir="${unsigned.dir}" />
		<jar
			destfile="${unsigned.target.jar}"
			duplicate="fail"
			manifest="${manifest.mf}">
			<fileset
				dir="${build.classes.dir}" />
			<fileset
				dir="cfg" />
		</jar>
	</target>

	<!-- for debugging -->
	<target
		name="unjar"
		depends="jar">
		<unjar
			src="${unsigned.target.jar}"
			dest="${unjar.dir}" />

	</target>

	<target
		name="init-security">
		<property
			name="keystore.dir"
			location="${user.home}/.secret" />
		<mkdir
			dir="${keystore.dir}" />
		<chmod
			file="${keystore.dir}"
			perm="700" />
		<property
			name="keystore"
			location="${keystore.dir}/local.keystore" />
		<property
			file="${keystore.dir}/keystore.properties" />
		<property
			name="keystore.alias"
			value="code.signer" />
	</target>

	<target
		name="get-password"
		depends="init-security">
		<input
			addproperty="keystore.password">password for keystore:</input>
		<echo
			level="verbose">keystore.password = ${keystore.password}</echo>
	</target>

	<target
		name="create-signing-key"
		depends="get-password">
		<genkey
			alias="${keystore.alias}"
			keystore="${keystore}"
			storepass="${keystore.password}"
			validity="366">
			<dname>
				<param
					name="CN"
					value="autosigner" />
				<param
					name="OU"
					value="Craig P. Motlin" />
				<param
					name="C"
					value="US" />
			</dname>
		</genkey>
	</target>

	<target
		name="delete-keystore"
		depends="init-security">
		<delete
			file="${keystore}" />
	</target>

	<!--
		setting no.sign.jar turns off JAR signing. This is for gump and other automated builds where we dont want signing to get involved.
	-->
	<target
		name="sign-jar"
		depends="jar,get-password"
		unless="no.sign.jar"
		description="Create the signed jar archive">
		<fail
			message="no keystore ${keystore}, run create-signing-key ">
			<condition>
				<not>
					<available
						file="${keystore}" />
				</not>
			</condition>
		</fail>

		<mkdir
			dir="${dist.dir}" />

		<signjar
			jar="${unsigned.target.jar}"
			signedjar="${target.jar}"
			alias="${keystore.alias}"
			keystore="${keystore}"
			storepass="${keystore.password}" />
	</target>

	<target
		name="sign-lib"
		depends="get-password"
		unless="no.sign.jar">
		<fail
			message="no keystore ${keystore}, run create-signing-key ">
			<condition>
				<not>
					<available
						file="${keystore}" />
				</not>
			</condition>
		</fail>

		<property
			name="dist.lib.dir"
			location="${dist.dir}/lib" />

		<mkdir
			dir="${dist.lib.dir}" />

		<copy
			todir="${dist.lib.dir}">
			<fileset
				dir="${lib.dir}">
				<include
					name="**/" />
				<exclude
					name="**/*.jar" />
			</fileset>
		</copy>

		<signjar
			destDir="${dist.lib.dir}"
			alias="${keystore.alias}"
			keystore="${keystore}"
			storepass="${keystore.password}">
			<fileset
				dir="${lib.dir}"
				includes="**/*.jar" />
		</signjar>
	</target>

	<target
		name="test-compile"
		depends="sign-jar">
		<mkdir
			dir="${test.classes.dir}" />

		<javac
			destdir="${test.classes.dir}"
			srcdir="${testsrc.dir}"
			debug="true"
			debuglevel="${debuglevel}"
			source="${source}"
			target="${target}">
			<classpath
				refid="test.compile.classpath" />
		</javac>
		<copy
			todir="${test.classes.dir}">
			<fileset
				dir="${testsrc.dir}"
				includes="${source.files.tocopy}" />
		</copy>
	</target>

	<patternset
		id="failing.tests">
		<exclude
			unless="run.failing.tests"
			name="" />
	</patternset>

	<target
		name="test-log4j">
		<property
			name="test.log4j.properties"
			location="${test.dir}/log4j.properties" />

		<copy
			file="cfg/log4j.properties"
			tofile="${test.log4j.properties}" />
		<propertyfile
			file="${test.log4j.properties}">
			<entry
				key="log4j.rootCategory"
				value="WARN, StdOutErr, Relative" />
		</propertyfile>
	</target>

	<target
		name="unit-tests"
		depends="test-compile,test-log4j">

		<property
			name="test.data.dir"
			location="${test.dir}/data" />
		<delete
			dir="${test.data.dir}" />
		<mkdir
			dir="${test.data.dir}" />

		<junit
			printsummary="false"
			errorproperty="test.failed"
			failureproperty="test.failed"
			fork="${junit.fork}"
			forkmode="${junit.forkmode}"
			dir="${basedir}">
			<classpath
				refid="test.classpath" />
			<sysproperty
				key="log4j.configuration"
				value="file:${test.log4j.properties}" />

			<assertions
				enablesystemassertions="true">
				<enable />
			</assertions>

			<formatter
				type="brief"
				usefile="false" />
			<formatter
				type="xml" />

			<test
				name="${testcase}"
				todir="${test.data.dir}"
				if="testcase" />
			<batchtest
				todir="${test.data.dir}"
				unless="testcase">
				<fileset
					dir="${test.classes.dir}">
					<include
						name="**/test/*Test.class" />
					<patternset
						refid="failing.tests" />
				</fileset>
			</batchtest>
		</junit>

		<property
			name="test.reports.dir"
			location="${test.dir}/reports" />
		<delete
			dir="${test.reports.dir}" />
		<mkdir
			dir="${test.reports.dir}" />

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

		<!--conditional failure -->
		<fail
			if="test.failed">
			Tests failed. Check ${test.reports.dir}
		</fail>

		<echo>Test report at file:///${test.reports.dir}/index.html</echo>
	</target>

	<target
		name="ant-contrib-init">
		<path
			id="ant-contrib.classpath">
			<fileset
				dir="${ant-contrib.lib.dir}">
				<include
					name="*.jar" />
			</fileset>
		</path>

		<taskdef
			uri="antlib:net.sf.antcontrib"
			classpathref="ant-contrib.classpath"
			onerror="failall" />
	</target>

	<target
		name="javadocs"
		depends="ant-contrib-init,classpaths">
		<ac:outofdate>
			<sourcefiles>
				<fileset
					dir="${src.dir}"
					includes="**/*.java" />
			</sourcefiles>
			<mapper
				type="glob"
				dir="src"
				from="*.java"
				to="${javadoc.dir}/*.html" />
			<sequential>
				<javadoc
					access="public"
					destdir="${javadoc.dir}"
					sourcepath="${src.dir}"
					use="true"
					version="true"
					windowtitle="${project.name}"
					failonerror="true"
					author="true"
					nodeprecated="false"
					nodeprecatedlist="false"
					noindex="false"
					nonavbar="false"
					notree="false"
					source="${source}"
					splitindex="true">
					<classpath
						refid="compile.classpath" />
					<link
						href="http://junit.sourceforge.net/javadoc/" />
					<link
						href="http://joda-time.sourceforge.net/api-release/" />
					<link
						href="http://commons.apache.org/lang/api-release/" />
					<link
						href="http://java.sun.com/javase/6/docs/api/" />
				</javadoc>

				<fixcrlf
					srcdir="${javadoc.dir}"
					includes="**/*.html"
					eol="unix" />
			</sequential>
		</ac:outofdate>
	</target>

	<target
		name="docs"
		depends="javadocs"
		description="Make the documentation">
		<copy
			todir="${doc.dir}"
			file="gpl.txt" />
	</target>

	<!-- for debugging -->
	<target
		name="unzip-bin-zipfile"
		depends="create-bin-zipfile">
		<unzip
			src="${target.zip}"
			dest="${dist.dir}" />
	</target>

	<target
		name="create-src-zipfile"
		depends="classpaths-extended">

		<mkdir
			dir="${dist.dir}" />

		<property
			name="src.zip"
			location="${dist.dir}/${project.name-ver}-src.zip" />
		<zip
			destfile="${src.zip}"
			duplicate="fail">

			<zipfileset
				dir="${src.dir}" />
		</zip>
	</target>

	<target
		name="create-src-tar"
		depends="classpaths-extended">

		<mkdir
			dir="${dist.dir}" />

		<property
			name="src.tar"
			location="${dist.dir}/${project.name-ver}-src.tar" />
		<property
			name="src.tar.gz"
			location="${src.tar}.gz" />
		<property
			name="src.tar.bz2"
			location="${src.tar}.bz2" />

		<tar
			destfile="${src.tar}"
			longfile="gnu">
			<zipfileset
				dir="${src.dir}" />
		</tar>
		<gzip
			src="${src.tar}"
			destfile="${src.tar.gz}" />
		<bzip2
			src="${src.tar}"
			destfile="${src.tar.bz2}" />
	</target>

	<!-- for debugging -->
	<target
		name="unzip-src-zipfile"
		depends="create-src-zipfile">
		<unzip
			src="${src.zip}"
			dest="${dist.dir}" />
	</target>

	<!-- for debugging -->
	<target
		name="untar-bin.tar.gz"
		depends="create-bin-tar">
		<mkdir
			dir="${untar.dir}" />
		<gunzip
			src="${target.tar.gz}"
			dest="${untar.dir}" />
		<untar
			src="${untar.dir}/${project.name-ver}.tar"
			dest="${untar.dir}" />
	</target>

	<!-- for debugging -->
	<target
		name="untar-with-copy"
		depends="create-bin-tar">
		<mkdir
			dir="${untar.dir}" />
		<copy
			todir="${untar.dir}">
			<gzipresource>
				<file
					file="${target.tar.gz}" />
			</gzipresource>
			<mapper
				type="glob"
				from="*.gz"
				to="*" />
		</copy>
		<copy
			todir="${untar.dir}"
			preservelastmodified="true">
			<tarfileset
				src="${untar.dir}/${project.name-ver}.tar"
				includes="**/*" />
		</copy>
	</target>



	<target
		name="package-everything"
		description="Package everything"
		depends="create-src-zipfile,create-bin-zipfile,create-src-tar,create-bin-tar" />

	<!-- Distribution targets -->
	<target
		name="checksum"
		depends="package-everything">
		<checksum
			file="${target.zip}"
			algorithm="sha1"
			format="MD5SUM" />
		<checksum
			file="${target.tar.gz}"
			algorithm="sha1"
			format="MD5SUM" />
		<checksum
			file="${target.tar.bz2}"
			algorithm="sha1"
			format="MD5SUM" />
		<checksum
			file="${src.zip}"
			algorithm="sha1"
			format="MD5SUM" />
		<checksum
			file="${src.tar.gz}"
			algorithm="sha1"
			format="MD5SUM" />
		<checksum
			file="${src.tar.bz2}"
			algorithm="sha1"
			format="MD5SUM" />
	</target>

	<target
		name="checksum-verify"
		depends="checksum">
		<checksum
			file="${target.zip}"
			algorithm="sha1"
			format="MD5SUM"
			property="target.zip.sha1" />
		<checksum
			file="${target.tar.gz}"
			algorithm="sha1"
			format="MD5SUM"
			property="target.tar.gz.sha1" />
		<checksum
			file="${target.tar.bz2}"
			algorithm="sha1"
			format="MD5SUM"
			property="target.tar.bz2.sha1" />
		<checksum
			file="${src.zip}"
			algorithm="sha1"
			format="MD5SUM"
			property="src.zip.sha1" />
		<checksum
			file="${src.tar.gz}"
			algorithm="sha1"
			format="MD5SUM"
			property="src.tar.gz.sha1" />
		<checksum
			file="${src.tar.bz2}"
			algorithm="sha1"
			format="MD5SUM"
			property="src.tar.bz2.sha1" />
	</target>

	<fileset
		id="ftp.upload.fileset"
		dir="${dist.dir}">
		<include
			name="*.zip" />
		<include
			name="*.zip.sha1" />
		<include
			name="*.tar.gz" />
		<include
			name="*.tar.gz.sha1" />
	</fileset>

	<target
		name="ftp-init">
		<fail
			unless="server">Set the "server" property!</fail>
		<property
			name="ftp.propfile"
			location="secure/${server}.properties" />
		<loadproperties
			srcFile="${ftp.propfile}" />
	</target>

	<target
		name="ftp-upload"
		depends="ftp-init">
		<echo>FTP target is ${ftp.server}</echo>
		<ftp
			server="${ftp.server}"
			userid="${ftp.user}"
			password="${ftp.password}"
			action="mkdir"
			remotedir="${ftp.dir}" />
		<ftp
			server="${ftp.server}"
			userid="${ftp.user}"
			password="${ftp.password}"
			action="put"
			remotedir="${ftp.dir}">
			<fileset
				refid="ftp.upload.fileset" />
		</ftp>
	</target>

	<target
		name="email-announcement"
		depends="checksum">
		<mail
			tolist="${email.to}"
			from="${email.from}"
			subject="New release of ${target.name}"
			mailhost="${email.server}"
			mailport="${email.port}"
			ssl="${email.ssl}"
			user="${email.user}"
			password="${email.password}">
			<message>
				Here is a new build of ${target.name}
				The SHA1 checksum of the file is
				${target.zip.sha1}

				-The development team
		    </message>
			<!-- <fileset file="${target.zip}" /> -->
			<fileset
				file="${target.zip}.sha1" />
		</mail>
	</target>

</project>


