<?xml version="1.0"?>

<project name="minime-common" default="build" basedir=".">

    <!-- Build properties -->
    <property name="build-common" value="${path.minime}/build" />
    <property name="tools" value="${path.minime}/tools" />
    
    <property file="${build-common}/minime.common.properties"/>
    
    <!-- load mobile properties file -->
    <property file="${build-common}/devices/device-${device.type}.properties"/>
    <property file="${build-common}/devices/device-default.properties"/>
    
    <!-- ant extension -->
    <property name="wtk.proguard.home" value="${tools}/ant"/>
    <taskdef resource="antenna.properties" classpath="${tools}/ant/lib/antenna-bin-0.9.13.jar"/>
    <taskdef name="parseText" classname="TxtToBinary" classpath="${tools}/minime-ant-task/class"/>
    <taskdef name="parseImage" classname="ImgToBinary" classpath="${tools}/minime-ant-task/class"/>
    <taskdef name="doxygen" classname="org.doxygen.tools.DoxygenTask" classpath="${tools}/ant/lib/ant_doxygen.jar"/>
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
      <classpath>
        <pathelement location="${tools}/ant/lib/ant-contrib-1.0b1.jar"/>
      </classpath>
    </taskdef>
    
    <!-- ================================================================== -->
    <!--                        define context build property               -->
    <!-- ================================================================== -->
    <condition property="task.debug" value="true">
        <istrue value="${debug}"/>
    </condition>
    <condition property="task.cldc10" value="true" else="false">
        <equals arg1="${wtk.cldc.version}" arg2="1.0"/>
    </condition>
    
    <property name="classpath.extra" value="${device.lib};${path.minime}/lib/j2meunit.jar" />  <!-- ${midp.lib};${device.lib} -->
    
    <echo />
    <echo>---------- MINIME ------------</echo>
    <echo>path.minime: ${path.minime}</echo>
    <echo>wtk.home: ${wtk.home}</echo>
    <echo>path.minime.src: ${path.minime.src}</echo>
    <echo>wtk.proguard.home: ${wtk.proguard.home}</echo>
	<echo>classpath.extra: ${classpath.extra}</echo>
    
    <echo>---------- APPLICATION ------------</echo>
    <echo>basedir: ${basedir}</echo>
    <echo>app.build.src: ${app.build.src}</echo>
    <echo>app.build.bin: ${app.build.bin}</echo>
    <echo>app.build.dist: ${app.build.dist}</echo>

    <echo>device.type: ${device.type}</echo>
    <echo>classpath.extra: ${classpath.extra}</echo>
    
    <!-- user task -->
    <target name="build" depends="compile, package, preverify"
            description="Build the midlet for device: ${device.type}" />
            
    <!-- task definition -->
    <target name="clean">
    	 <delete dir="${app.build}"/>
    </target>
    
    <target name="init" depends="clean">
    	<mkdir dir="${app.build.bin}"/>
        <mkdir dir="${app.build.src}"/>
        <mkdir dir="${app.build.dist}"/>
    	<mkdir dir="${app.build.test}"/>
    </target>
    
    <!-- Compile -->
    <target name="compile" depends="init, packrsc, preprocsrc">
        <echo>debug support: ${task.debug}</echo>
    	<mkdir dir="${app.build.bin}"/>
    	<!-- Compile everything, but don't preverify (yet). -->
    	<wtkbuild srcdir="${app.build.src}"
    	          destdir="${app.build.bin}"
    		      preverify="false"
    	          debug="${task.debug}"
    	          classpath="${classpath.extra}"
    	          source="1.3"/>		
    </target>
    
    <!-- pack/copy all resources -->
    <!-- preprocess resources including creating .bin resources files and resources id .java files -->
    <target name="packrsc" depends="init" description="Process and pack resources">
        <parseText fileName="${path.minime.rsc}/text.${txt.rsc.file}" todir="${path.minime.src}" javaFileName="minime/gen/TextRscId.java"/>
        <parseImage fileName="${path.minime.rsc}/image.${img.rsc.file}" todir="${path.minime.src}"  javaFileName="minime/gen/ImageRscId.java"/>
        <mkdir dir="${app.build.bin}/rsc"/>
    	<copy todir="${app.build.bin}/rsc">
    		<fileset dir="${path.minime.rsc}" includes="*.bin"/>
    	    <fileset file="${basedir}/${midlet.icon}"/>         <!-- copy Midlet icon -->
    	    <fileset file="${basedir}/${midlet.log4mm}"/>
    	</copy>
    </target>
    
    <target name="preprocsrc" depends="init" description="Preprocess the source code with antenna">
        <echo>preprocess from: ${path.minime.src} to: ${app.build.src}</echo>	
        <echo>preprocess from: ${app.src} to: ${app.build.src}</echo>	
        <echo/>
        <!-- preprocess application source -->
    	<wtkpreprocess  srcdir="${app.src}"
    	                destdir="${app.build.src}"
    	                symbols="true"
    	                />

        <!-- preprocess minime source -->
    	<wtkpreprocess  srcdir="${path.minime.src}"
    	                destdir="${app.build.src}"
    	                symbols="true"
    	                />
    </target>
    
    <!-- Create a jad and package file. -->
    <target name="package">
    
        <!-- create the JAD -->
        <mkdir dir="${app.build.dist}"/>
    	<mkdir dir="${app.build.test}"/>
        	
        <wtkjad name="${midlet.name}" 
            jadfile="${app.build.dist}/${midlet.jad}.jad" 
            jarfile="${app.build.dist}/${midlet.jad}.jar" 
            vendor="${midlet.vendor}" 
            version="${midlet.version}"
            update="false">
        		
            <midlet name="${midlet.name}" icon="${midlet.icon}" class="${midlet.class}">  
            </midlet>            
        </wtkjad>
    	
    	
        <!-- create the Test JAD -->
 	   <wtkjad name="${midlet.name}Test" 
             jadfile="${app.build.test}/${midlet.jad}Test.jad" 
             jarfile="${app.build.test}/${midlet.jad}Test.jar" 
             vendor="${midlet.vendor}" 
             version="${midlet.version}"
             update="false">
         		
             <midlet name="${midlet.name}Test" icon="${midlet.icon}" class="${unittest.midlet.class}">  
 			</midlet>       

         </wtkjad>
    	
        
        <!-- package and create the jar -->
        <wtkpackage 
            jarfile="${app.build.dist}/${midlet.jad}.jar" jadfile="${app.build.dist}/${midlet.jad}.jad">
            <fileset dir="${app.build.bin}" />
        </wtkpackage>
        
           <!-- package and create the application unit test jar -->
           <wtkpackage 
               jarfile="${app.build.test}/${midlet.jad}Test.jar" jadfile="${app.build.test}/${midlet.jad}Test.jad">
               <fileset dir="${app.build.bin}"/>
               <zipfileset  src="${path.minime}/lib/j2meunit.jar"/>   
           </wtkpackage>
    	
    	
    </target>
    
    <target name="preverify">
        <wtkpreverify
            jarfile="${app.build.dist}/${midlet.jad}.jar" 
            jadfile="${app.build.dist}/${midlet.jad}.jad"
            classpath="${classpath.extra}"
            cldc="${task.cldc10}">
        </wtkpreverify>
    <!--      bootclasspathref="classpath.extra"  -->
    </target>
	
    <target name="preverifyTest">
        <wtkpreverify
            jarfile="${app.build.test}/${midlet.jad}Test.jar" 
            jadfile="${app.build.test}/${midlet.jad}Test.jad"
            classpath="${classpath.extra}"
            cldc="${task.cldc10}">
        </wtkpreverify>
    <!--      bootclasspathref="classpath.extra"  -->
    </target>
    
    <target name="obfuscate" unless="task.debug">
        <!-- Doing the obfuscation/optimization separately and being able to specify
            the '-defaultpackage' option decreases TitleManager's jar-file size about 4kB
            when compared to doing obfuscation in the wtkpackage task. -->
        <wtkobfuscate
            jarfile="${app.build.dist}/${midlet.jad}.jar" jadfile="${app.build.dist}/${midlet.jad}.jad">
            <!-- classpath="${device.lib}" -->
            <argument value="-defaultpackage "/>
            <argument value="-printmapping ${app.build.dist}/pro_map.txt"/>
        </wtkobfuscate>
    </target>
    
    <!-- doxygen and doc generation -->
    <target name="doc" depends="init, preprocsrc">
    	<mkdir dir="${app.build}/doc"/>
    	<doxygen doxygenPath="${tools}/doxygen/bin/doxygen.exe">
    		<property name="INPUT" value="${app.build.src}" />
    		<property name="PROJECT_NAME" value="${midlet.name}" />
    		<property name="OUTPUT_DIRECTORY" value="${app.build}/doc" />
    		<property name="RECURSIVE" value="yes" />
    		<property name="GENERATE_LATEX" value="false" />
    		<property name="JAVADOC_AUTOBRIEF" value="yes" />
    		<property name="OPTIMIZE_OUTPUT_JAVA" value="yes" />
    		
    		<property name="EXTRACT_PRIVATE" value="yes" /> 
    		<property name="HIDE_UNDOC_MEMBERS" value="no" /> 
    		<property name="REFERENCES_RELATION" value="no" />
    		<property name="REFERENCES_LINK_SOURCE" value="no" />
    	</doxygen>
    	<!-- create the shortcut -->
        <exec executable="${tools}/ant/lib/XXMKLINK.EXE">
            <arg value="${midlet.name}-on-${device.type}-doc"/>
            <arg value="${app.build}/doc/html/index.html"/>
        </exec>
    </target>	
        
    <target name="run" description="Run app in the appropriate emulator">
    	<copy file="${path.minime}/tools/wtk/bin/emulator-batch-template" 
    	    tofile="${path.minime}/tools/wtk/bin/emulator.bat"
    	    overwrite="true" />
    	<replace file="${path.minime}/tools/wtk/bin/emulator.bat" token="@minime-path@" value="${path.minime}"/>

        <if>
            <equals arg1="${debug}" arg2="true"/>
            <then>
                <echo>debug on port: ${debug.emulator.port}</echo>
                <wtkrun jadfile="${app.build.dist}/${midlet.jad}.jad"
                        debug="${debug.emulator.port}"
                        wait="true"/>
            </then>
            <else>
                <wtkrun jadfile="${app.build.dist}/${midlet.jad}.jad"
                        classpath="${app.build.dist}/${midlet.jad}.jar;${classpath.extra}"
                        wait="true"/>
            </else>
        </if>
    </target>
    
    <target name="run-nok">
        <echo message="executable=${device.wtk.home}/${device.emulator.exe}"/>
        <echo message="jad: ${app.build.dist}\${midlet.jad}.jad"/>
        <if>
            <equals arg1="${debug}" arg2="true"/>
            <then>
                <exec dir="${device.wtk.home}" executable="${device.wtk.home}/${device.emulator.exe}" failonerror="true">
                    <arg line="-Xdebug -Xrunjdwp:transport=dt_socket,address=${debug.emulator.port}, server=y -Xdescriptor:${app.build.dist}\${midlet.jad}.jad"/>
            </exec>
            </then>
            <else>
                <exec dir="${device.wtk.home}" executable="${device.wtk.home}/${device.emulator.exe}" failonerror="true">
                <arg line="-Xdescriptor:${app.build.dist}\${midlet.jad}.jad"/>
            </exec>
            </else>
        </if>
    </target>
	

	<!--Run the testsuite in the file unitTest.testAllMidlet-->
    <target name="test" description="test app in unitTest.testAllMidlet">
    	<copy file="${path.minime}/tools/wtk/bin/emulator-batch-template" 
			tofile="${path.minime}/tools/wtk/bin/emulator.bat"
    	    overwrite="true" />
	<replace file="${path.minime}/tools/wtk/bin/emulator.bat" token="@minime-path@" value="${path.minime}"/>
  	<wtkrun jadfile="${app.build.test}/${midlet.jad}Test.jad"
                        classpath="${app.build.test}/${midlet.jad}Test.jar;${classpath.extra}"
                        wait="true"/>
	</target>

    <target name="init-minime">
        <exec dir="${path.minime}/tools/wtk/bin" executable="echo">
            <arg line="hello >test.txt"/>
<!--            <arg line="java.exe -Dkvem.home=tools/wtk -Djava.library.path=tools/wtk/bin -Dsun.java2d.ddlock=true -Dsun.java2d.gdiblit=false -cp tools/wtk/wtklib/kenv.zip;tools/wtk/wtklib/ktools.zip;tools/wtk/wtklib/customjmf.jar com.sun.kvem.environment.EmulatorWrapper"/>
            <arg line="> emulator_test.bat"/>
            -->
        </exec>            
    </target>       
</project>
