<?xml version="1.0"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<!-- WTK Aware build script -->
<!-- This requires Antenna -->
<!DOCTYPE project PUBLIC "-//Ant//Project 1.5//EN" "ant.dtd">

<project name="commons-me" default="compile" basedir=".">
	<description>Commons-ME - Apache Commons for Java Micro Edition</description>

	<!-- From here on down, you probably don't need to change anything -->
	<property environment="env"/>

	<!-- We really want Java 1.1 source level for the JavaME code -->
	<!-- However Java 6 doesn't support that, so we need 1.3 there -->
	<condition property="main.jdk.version.source" value="1.3" else="1.1">
		<equals arg1="${ant.java.version}" arg2="1.6" />
	</condition>
	<property name="main.jdk.version.class" value="1.1"
		description="JDK version of generated class files"/>

	<property name="support.jdk.version.source" value="1.5"
		description="JDK version of source code"/>
	<property name="support.jdk.version.class" value="1.5"
		description="JDK version of generated class files"/>

	<tstamp>
		<format property="tstamp.year" pattern="yyyy"/>
		<format property="DSTAMP" pattern="yyyy-MM-dd"/>
		<format property="shortDSTAMP" pattern="yyyyMMdd"/>
	</tstamp>
	<tstamp prefix="date">
		<format property="today" pattern="yyyyMMdd" />
	</tstamp>

	<property name="jar_file"     value="build/dist/Commons-ME-${date.today}.jar" />
	<property name="jad_file"     value="build/dist/Commons-ME-${date.today}.jad" />
	<property name="jme_jar_file" value="buid/dist/Commons-ME-${date.today}-preverified.jar" />

	<property name="wtk.cldc.version" value="1.1" />
	<property name="wtk.cldc.short_version" value="11" />
	<property name="wtk.midp.version" value="2.0" />
	<property name="wtk.midp.short_version" value="20" />

	<property name="wtk.bluetooth.enabled" value="true" />
	<property name="wtk.optionalpda.enabled" value="true" />
	<property name="wtk.locationservices.enabled" value="true" />

	<!-- The places we look for the WTK in, by default -->
	<!-- Specify -DWTK= if it isn't one of them -->
	<property name="wtk.home.a" value="../WTK2.5.1/" />
	<property name="wtk.home.b" value="../WTK2.5.2/" />
	<property name="wtk.home.c" value="../WTK/" />

	<condition property="wtk.home" value="${wtk.home.a}">
		<available file="${wtk.home.a}" />
	</condition>
	<condition property="wtk.home" value="${wtk.home.b}">
		<available file="${wtk.home.b}" />
	</condition>
	<condition property="wtk.home" value="${wtk.home.c}">
		<available file="${wtk.home.c}" />
	</condition>
	<condition property="wtk.home" value="${WTK}">
		<isset property="WTK" />
	</condition>
	<fail unless="wtk.home" message="No valid WTK path found. Please specify it with -DWTK=[path to your WTK] as an argument to ant" />

	<path id="wtk.jars">
		<!-- The ones we need a certain version off -->
		<fileset dir="${wtk.home}/lib/">
			<include name="cldcapi${wtk.cldc.short_version}.jar"/>
			<include name="midpapi${wtk.midp.short_version}.jar"/>
		</fileset>
		<!-- Now the rest -->
		<fileset dir="${wtk.home}/lib/">
			<include name="*.jar"/>
			<exclude name="clcd*.jar"/>
			<exclude name="midp*.jar"/>
		</fileset>
	</path>
	<path id="wtk.full_jars">
		<path refid="wtk.jars" />
		<fileset dir="${wtk.home}/wtklib/">
			<include name="*.jar" />
			<include name="*.zip" />
		</fileset>
	</path>

	<!-- Pull in some jars from /build-lib/ -->
	<path id="compile.classpath">
		<fileset dir="build-lib/">
			<include name="*.jar" />
		</fileset>
		<path refid="wtk.jars" />
	</path>
	<path id="run.classpath">
		<fileset dir="build-lib/">
			<include name="*.jar"/>
		</fileset>
		<pathelement path="build/ant-classes/" />

		<!-- Need normal WTK jars, and the wtklib -->
		<path refid="wtk.full_jars" />
	</path>


	<property name="repository" value="http://www.ibiblio.org/maven"/>
	<property name="junit.jar1.dir" location="build-lib/junit-3.8.1.jar"/>
	<property name="junit.jar1.url" value="${repository}/junit/jars/junit-3.8.1.jar"/>


	<!-- ******************************************** -->
	<!--  Core setup tasks                            -->
	<!-- ******************************************** -->

	<!-- Ensure that all the directories we need exist -->
	<target name="init" depends="check-jars,fetch-jars">
		<mkdir dir="build"/>
		<mkdir dir="build/ant-classes"/>
		<mkdir dir="build/mobile-classes"/>
		<mkdir dir="build/verified-mobile-classes"/>
		<mkdir dir="build/dist"/>
		<mkdir dir="build/tests"/>
		<mkdir dir="build/ide-classes"/>
	</target>

	<!-- Ensure all jars we need are found -->
	<target name="check-jars">
		<condition property="junit.present">
			<available file="${junit.jar1.dir}"/>
		</condition>
	</target>
	<target name="fetch-jars" depends="fetch-junit" />
	<target name="fetch-junit" unless="junit.present">
		<get src="${junit.jar1.url}" dest="${junit.jar1.dir}"/>
	</target>

	<!-- Delete all jars, and compiled classes -->
	<target name="clean">
		<delete dir="build"/>
		<delete>
			<fileset dir="." includes="**/*.class" />
		</delete>
	</target>


	<!-- ******************************************** -->
	<!--  Compile the program for on-phone / emulator -->
	<!-- ******************************************** -->

	<!-- Generate the normal-use jar -->
	<target name="jar" depends="clean,init,compile">
		<jar destfile="${jar_file}">
			<fileset dir="build/ant-classes/" />
			<fileset dir="legal/" />
		</jar>
		<echo message="Jar generated, produced ${jar_file}" />
	</target>

	<target name="javame-jar" depends="clean,init,compile">
		<!-- We require Antenna -->
		<!-- Download it from http://antenna.sourceforge.net/ -->
		<taskdef resource="antenna.properties" classpathref="compile.classpath" />

		<!-- We require svnant -->
		<!-- Download it from http://subclipse.tigris.org/svnant.html -->
		<taskdef resource="svntask.properties" classpathref="compile.classpath" />

		<!-- Compile -->
		<wtkbuild
			srcdir="src" 
			destdir="build/mobile-classes/"
		>
			<classpath refid="compile.classpath"/>
		</wtkbuild>

		<!-- Unpack the runlib -->
		<unjar dest="build/mobile-classes/">
			<patternset>
				<include name="**/*.class" />
			</patternset>
			<fileset dir="run-lib" />
		</unjar>

		<!-- Pre-verify -->
		<echo message="Running pre-verifier on the classes..." />
		<exec 
			failonerror="true"
			executable="${wtk.home}/bin/preverify1.1">
				<arg value="-d" />
				<arg value="build/verified-mobile-classes" />
				<arg value="-classpath" />
				<arg pathref="wtk.full_jars" />
				<arg value="build/mobile-classes" />
		</exec>
		<echo message="Pre-verifier completed" />

		<!-- Grab the current svn version -->
		<svn javahl="false">
			<status path="." revisionProperty="svn.rev" />
		</svn>
		<!-- Need it in the form d.dd.dd -->
		<exec 
			outputproperty="version" 
			executable="perl">
			<arg line='-e "$rev = ${svn.rev}; $reva = int($rev/10000); $revb = ($rev-$reva)/100; $revc = $rev % 100; printf(&#0039;%d.%02d.%02d&#0039;,$reva,$revb,$revc)"' />
		</exec>
		<echo message="Currently at SVN revision ${svn.rev}, version is ${version}" />

		<!-- Copy the stub jad file -->
		<copy 
			file="Stub.jad"
			tofile="${jad_file}" />

		<!-- Set the version on the Jad -->
		<wtkjad
			jadfile="${jad_file}"
			update="true"
			version="${version}" />

		<!-- Generate the Jar -->
		<wtkpackage
			jarfile="${jme_jar_file}"
			jadfile="${jad_file}"
			preverify="false">
			<fileset dir="build/verified-mobile-classes/" />
		</wtkpackage>
	</target>


	<!-- ******************************************* -->
	<!--  Compile the program for ant / testing use  -->
	<!-- ******************************************* -->

	<!-- Build the code, and the supporting libs -->
	<target name="compile" depends="init,compile-midp,compile-test" />
	<target name="compile-midp" depends="init">
		<javac
			srcdir="src" 
			destdir="build/ant-classes/"
			target="${main.jdk.version.class}"
			source="${main.jdk.version.source}"
			debug="yes"
		>
			<bootclasspath refid="compile.classpath"/>
		</javac>
	</target>

	<!-- Build the tests code -->
	<target name="compile-test" depends="init,compile-midp">
		<javac
			srcdir="test-src" 
			destdir="build/tests/"
			target="${support.jdk.version.class}"
			source="${support.jdk.version.source}"
			debug="yes"
		>
			<classpath refid="run.classpath"/>
		</javac>
	</target>

	<!-- ******************** -->
	<!--  Run the unit tests  -->
	<!-- ******************** -->
	<target name="test" depends="compile" description="Run all unit tests">
		<junit fork="true" forkmode="once" 
		errorProperty="test.failed" failureProperty="test.failed">
			<jvmarg value="-Djava.library.path=${wtk.home}/bin" />
			<jvmarg value="-Dkvem.home=${wtk.home}" />
			<classpath refid="run.classpath"/>
			<classpath path="build/tests/"/>
			<formatter type="brief" usefile="false" />
			<batchtest todir="build/tests">
				<fileset dir="test-src/">
					<include name="**/Test*.java" />
				</fileset>
			</batchtest>
		</junit>
	</target>
	<target name="test-stopping" depends="compile" 
			description="Run all unit tests, stopping on the first failing one">
		<junit fork="true" forkmode="once" 
		haltonerror="true" haltonfailure="true"
		errorProperty="test.failed" failureProperty="test.failed">
			<jvmarg value="-Djava.library.path=${wtk.home}/bin" />
			<jvmarg value="-Dkvem.home=${wtk.home}" />
			<classpath refid="run.classpath"/>
			<formatter type="brief" usefile="false" />
			<batchtest todir="build/tests">
				<fileset dir="test-src/">
					<include name="**/Test*.java" />
				</fileset>
			</batchtest>
		</junit>
	</target>
	<target name="single-test" depends="compile" description="Runs a single test case specified with -Dtestcase=classname">
		<junit fork="true" forkmode="once" 
		errorProperty="test.failed" failureProperty="test.failed">
			<jvmarg value="-Djava.library.path=${wtk.home}/bin" />
			<jvmarg value="-Dkvem.home=${wtk.home}" />
			<classpath refid="run.classpath"/>
			<formatter type="brief" usefile="false" />
			<test name="${testcase}" />
		</junit>
	</target>

</project>
