<?xml version="1.0" encoding="UTF-8"?>
<project name="Your Project" default="deploy" basedir=".">
	<description>Build file for Your Project</description>

	<!-- set your custom properties in build.properties -->
	<property file="${user.home}/build.properties" />
	<property file="build.properties" />

	<!-- set global properties for this build -->
	<property name="src" location="src" />
	<property name="tests" location="tests" />
	<property name="bin" location="bin" />
	<property name="dist" location="dist" />
	<property name="conf" location="conf" />
	<property name="res" location="res" />

	<!-- Defining Antenna's WTK tasks -->
	<taskdef name="wtkjad" classname="de.pleumann.antenna.WtkJad" />
	<taskdef name="wtkbuild" classname="de.pleumann.antenna.WtkBuild" />
	<taskdef name="wtkpackage" classname="de.pleumann.antenna.WtkPackage" />
	<taskdef name="wtkmakeprc" classname="de.pleumann.antenna.WtkMakePrc" />
	<taskdef name="wtkrun" classname="de.pleumann.antenna.WtkRun" />
	<taskdef name="wtkpreverify" classname="de.pleumann.antenna.WtkPreverify" />
	<taskdef name="wtkobfuscate" classname="de.pleumann.antenna.WtkObfuscate" />
	<taskdef name="wtksmartlink" classname="de.pleumann.antenna.WtkSmartLink" />
	<taskdef name="wtkpreprocess" classname="de.pleumann.antenna.WtkPreprocess" />

	<!-- The J2ME stuff should be declared in the build.properties -->
	<path id="j2me.classpath">
		<pathelement location="lib/midpapi20.jar" />
		<pathelement location="lib/wma.zip" />
		<pathelement location="lib/cldcapi11.zip" />
		<pathelement location="lib/pim.zip" />
		<pathelement location="lib/j2meunit.jar" />
		<pathelement location="lib/bluetooth.zip" />
	</path>

	<!-- Prints out configuration information -->
	<target name="info" description="Prints some basic build info">
		<echo>Build file for the ${ant.project.name} MIDlet</echo>
		<echo>Using WTK home as ${wtk.home}</echo>
	</target>

	<!-- Init tasks set ups the basic directory structure -->
	<target name="init" depends="info" description="Creates basic directories">
		<!-- Create the time stamp -->
		<tstamp />
		<!-- Create the build directory structure used by compile -->
		<mkdir dir="${bin}" />
		<mkdir dir="${dist}" />

		<!-- WTK-specific properties -->
		<property name="wtk.run.device" value="DefaultColorPhone" />
		<property name="wtk.run.wait" value="true" />

		<!-- General properties used in the project -->
		<property name="buildbase" value="." />

		<!-- MIDlet properties -->
		<property name="midlet.basename" value="${dist}/${midlet.name}" />
	</target>

	<!-- Cleans the output directories -->
	<target name="clean" depends="init">
		<delete dir="${bin}" failonerror="false" />
		<delete dir="preverified" failonerror="false" />
		<delete file="${midlet.basename}.jar" failonerror="false" />
		<delete file="${midlet.basename}.jad" failonerror="false" />
	</target>

	<!-- Cleans the output directories only if the property midlet.rebuild has 
	     been set externally -->
	<target name="conditional_clean" if="midlet.rebuild">
		<antcall target="clean">
		</antcall>
	</target>

	<!-- setup of build structures and information -->
	<target name="setup" depends="init">
		<!-- Check required properties -->
		<fail message="Property wtk.home must be set" unless="wtk.home" />
		<fail message="Property midlet.name must be set" unless="midlet.name" />
		<fail message="Property midlet.class must be set" unless="midlet.class" />

		<mkdir dir="${bin}" />

		<!-- Check if rebuilding of the JAR is necessary -->
		<condition property="midlet.uptodate">
			<and>
				<!-- check if midlet jar is uptodate -->
				<uptodate targetfile="${midlet.basename}.jar">
					<srcfiles dir="${src}" includes="**/*.java" />
					<srcfiles dir="${res}" includes="*" />
					<srcfiles dir="${buildbase}" includes="build*.xml" />
				</uptodate>

				<!-- check if rebuild requested externally -->
				<not>
					<istrue value="${midlet.rebuild}" />
				</not>
			</and>
		</condition>
	</target>

	<target name="compile" depends="conditional_clean,setup">

		<!-- set property defaults (only if not set before!) -->
		<property name="wtk.build.debug" value="off" />
		<property name="wtk.build.optimize" value="on" />

		<!-- Compile everything, but don't preverify (yet) -->
		<javac srcdir="${src}" destdir="${bin}" classpath="lib/midpapi20.jar:lib/cldcapi11.zip:lib/pim.zip:lib/wma.zip:lib/bluetooth.zip" source="1.3" 
target="1.1" debug="on" bootclasspath="lib/midpapi20.jar:lib/cldcapi11.zip:lib/pim.zip:lib/wma.zip:lib/bluetooth.zip" />
	</target>

	<!-- Invokes the WTK specific tasks to create the midlet JAR -->
	<target name="create.jad.unsigned">
		<wtkjad jadfile="conf/${midlet.name}.jad" jarfile="${midlet.basename}.jar" name="${midlet.name}" manifest="${conf}/MANIFEST.MF" 
vendor="${wtk.jad.vendor}" target="${deploy.url.unsigned}" update="true">
			<midlet name="${midlet.name}" class="${midlet.class}" icon="${wtk.jad.icon}" />
			<attribute name="MicroEdition-Profile" value="${wtk.jad.j2me-profile}" />
			<attribute name="MicroEdition-Configuration" value="${wtk.jad.j2me-config}" />
			<attribute name="MIDlet-Icon" value="${wtk.jad.icon}" />
			<attribute name="MIDlet-Push-1" value="${midlet.push.1}" />
			<attribute name="MIDlet-Description" value="${midlet.description}" />
			<attribute name="MIDlet-Permissions" value="${midlet.permissions}" />
		</wtkjad>
		<!-- Copy the JAD over so we don't build up a huge corrupt file -->
		<copy file="conf/${midlet.name}.jad" todir="${dist}" />
	</target>

	<target name="package" depends="compile, create.jad.unsigned">
		<wtkpackage jarfile="${midlet.basename}.jar" 
			jadfile="${midlet.basename}.jad" 
			basedir="bin" 
			manifest="${conf}/MANIFEST.MF" 
			preverify="false" 
			classpath="Put your classpath in here" 
			bootclasspath="Put your bootclasspath in here" 
			autoversion="true" 
			verbose="true" 
			obfuscate="true">
			<fileset dir="res" />
			<Preserve class="Put Your Preserves in here..." />
		</wtkpackage>

		<exec executable="${wtk.home}/bin/preverify">
			<arg line="-classpath lib/midpapi20.jar:lib/cldcapi11.zip:lib/pim.zip:lib/wma.zip" />
			<arg line="-d ${dist}" />
			<arg line="${midlet.basename}.jar" />
		</exec>

		<!-- Copy the JAD back to conf directory so we get auto versioning -->
		<copy file="${midlet.basename}.jad" todir="conf" />
	</target>

	<target name="sign" depends="package">
 		<exec executable="${jarsigner.exe}">
			<arg line="-keystore ${sign.keystore}" />
			<arg line="-storepass ${sign.password}" />
			<arg line="${midlet.basename}.jar" />
			<arg line="${sign.alias}" />
		    <arg line="-verbose" />
		</exec>
	</target>

	<target name="create.jad.signed">
		<!-- Add the certificate from the JAR to the JAD -->
		<exec executable="${java.exe}">
			<arg line="-jar ${wtk.home}/bin/JadTool.jar" />
			<arg line="-addcert" />
			<arg line="-alias ${sign.alias}" />
			<arg line="-storepass ${sign.password}" />
			<arg line="-keystore ${sign.keystore}" />
			<arg line="-inputjad ${midlet.basename}.jad" />
			<arg line="-outputjad ${midlet.basename}.jad" />
		</exec>

		<!-- Add the signature from the JAR to the JAD -->
		<exec executable="${java.exe}">
			<arg line="-jar ${wtk.home}/bin/JadTool.jar" />
			<arg line="-addjarsig" />
			<arg line="-alias ${sign.alias}" />
			<arg line="-jarfile ${midlet.basename}.jar" />
			<arg line="-storepass ${sign.password}" />
			<arg line="-keypass ${sign.password}" />
			<arg line="-keystore ${sign.keystore}" />
			<arg line="-inputjad ${midlet.basename}.jad" />
			<arg line="-outputjad ${midlet.basename}.jad" />
		</exec>

		<!-- Update JAD file size and target after signing -->
		<wtkjad jadfile="${midlet.basename}.jad" jarfile="${midlet.basename}.jar" target="${deploy.url.signed}" update="true">
		</wtkjad>
	</target>

	<!-- Internal target to perform a full build -->
	<target name="rebuild" depends="clean,package">
	</target>

	<!-- Builds the midlet and deploys it if {deploydir} is set -->
	<target name="deploy.unsigned" depends="rebuild">
		<fail message="Property deploydir must be set" unless="deploy.dir" />
		<echo message="Deploying ${midlet.basename}.jar/jad to '${deploy.dir}'" />
		<copy file="${midlet.basename}.jad" todir="${deploy.dir}" />
		<copy file="${midlet.basename}.jar" todir="${deploy.dir}" />
	</target>

	<target name="deploy.signed" depends="sign, create.jad.signed">
		<fail message="Property deploydir must be set" unless="deploy.dir" />
		<echo message="Deploying ${midlet.basename}.jar/jad to '${deploy.dir}/signed'" />
		<copy file="${midlet.basename}.jad" todir="${deploy.dir}/signed" />
		<copy file="${midlet.basename}.jar" todir="${deploy.dir}/signed" />
	</target>

	<target name="deploy" depends="deploy.unsigned, deploy.signed">
	</target>
</project>

