<?xml version="1.0"?>

<project name="common">
    <property name="SDK_VERSION" value="$Change: 195038 $" />

    <target name="common">
        <property environment="env" />

        <!-- store this for passing into sub-calls -->
        <property name="PROJECT_ROOT" value="${basedir}" />
    
        <condition property="DANGER_USER" value="${env.DANGER_USER}">
            <isset property="env.DANGER_USER" />
        </condition>

        <!-- default this to the examples directory -->
        <condition property="DANGER_PROJECTS_HOME" value="${env.DANGER_PROJECTS_HOME}">
            <isset property="env.DANGER_PROJECTS_HOME" />
        </condition>
        <property name="DANGER_PROJECTS_HOME" value="${PROJECT_ROOT}/.." />

        <!-- set jar flag if need be -->
        <condition property="APP_FROM_JAR" value="true">
            <or>
                <isset property="APP_DANGER_FROM_JAR" />
                <isset property="APP_MIDLET_FROM_JAR" />
            </or>
        </condition>

        <!-- set midlet flag if need be -->
        <condition property="APP_IS_MIDLET" value="true">
            <or>
                <isset property="APP_MIDLET_FROM_SRC" />
                <isset property="APP_MIDLET_FROM_JAR" />
                <isset property="APP_MIDLET_FROM_WTK" />
            </or>
        </condition>

        <!-- some sanity checks -->
        <fail unless="DANGER_HOME" message="DANGER_HOME must be set by your local build file (build.xml)" />
        <condition property="MISSING_APP_CLASS_NAME" value="true">
            <and>
                <not> <isset property="APP_CLASS_NAME" /> </not>
                <not> <isset property="APP_IS_MIDLET" /> </not>
            </and>
        </condition>
        <fail if="MISSING_APP_CLASS_NAME" message="APP_CLASS_NAME must be set by your local build file (build.xml) for non-MIDlet projects" />

        <!-- MIDlet default -->
        <condition property="APP_SRC_PATH" value="src">
            <isset property="APP_IS_MIDLET" />
        </condition>

        <!-- other defaults -->
        <property name="APP_BIN_PATH" value="bin" />
        <property name="APP_RES_PATH" value="res" />

        <!-- where common code goes -->
        <property name="APP_COMMON_PATH" value="common" />

        <!-- the project name is used for defaulting JAR/JAD/RSRC filenames
             as well as for the WTK apps directory name. -->
        <condition property="APP_PROJECT_NAME" value="${APP_CLASS_NAME}">
            <isset property="APP_CLASS_NAME" />
        </condition>
        <basename property="APP_PROJECT_NAME" file="${PROJECT_ROOT}" />

        <!-- put together the full app class -->
        <condition property="APP_FULL_CLASS_NAME" value="${APP_PACKAGE_NAME}.${APP_CLASS_NAME}">
            <isset property="APP_PACKAGE_NAME" />
        </condition>
        <condition property="APP_FULL_CLASS_NAME" value="${APP_CLASS_NAME}">
            <isset property="APP_CLASS_NAME" />
        </condition>
        <condition property="APP_FULL_CLASS_NAME" value="danger.midlet.Launcher">
            <isset property="APP_IS_MIDLET" />
        </condition>
        <property name="APP_FULL_CLASS_NAME" value="" />

        <!-- come up with a suitable bundle name -->
        <condition property="APP_BUNDLE_NAME" value="${APP_PACKAGE_NAME}">
            <isset property="APP_PACKAGE_NAME" />
        </condition>
        <condition property="APP_BUNDLE_NAME" value="${APP_CLASS_NAME}">
            <isset property="APP_CLASS_NAME" />
        </condition>
        <property name="APP_BUNDLE_NAME" value="${APP_PROJECT_NAME}" />

        <!-- default filenames -->
        <property name="APP_JAR_FILE_NAME" value="${APP_PROJECT_NAME}.jar" />
        <property name="APP_JAD_FILE_NAME" value="${APP_PROJECT_NAME}.jad" />
        <property name="APP_RSRC_FILE_NAME" value="${APP_PROJECT_NAME}.rsrc" />

        <!-- allow specifying the shorter VERSION variable -->
        <condition property="TARGET_LIB_VERSION" value="${VERSION}">
            <isset property="VERSION" />
        </condition>

        <!-- default library version is 3.4 -->
        <!-- TODO: can we change this so it doesn't have to be hardcoded?? -->
        <property name="TARGET_LIB_VERSION" value="3.4" />

        <!-- if this is a MIDlet, we can't build these versions -->
        <condition property="EXCLUDE_LIBS" value="1.*,2.0,2.1">
            <isset property="APP_IS_MIDLET" />
        </condition>

        <!-- contained areas for assembling builds -->
        <property name="WORK_PATH" value="work" />
        <property name="WORK_ROOT" value="${PROJECT_ROOT}/${WORK_PATH}" />
        <property name="WORKING_ROOT" value="${WORK_ROOT}/${TARGET_LIB_VERSION}" />

        <property name="WORKING_SOURCE_ROOT" value="${WORKING_ROOT}/source" />
        <property name="WORKING_CLASSES_ROOT" value="${WORKING_ROOT}/classes" />
        <property name="WORKING_LOCALE_ROOT" value="${WORKING_ROOT}/locale" />
        <property name="WORKING_RESOURCES_ROOT" value="${WORKING_ROOT}/resources" />

        <!-- defaults app version -->
        <property name="APP_VERSION_MAJOR" value="1" />
        <property name="APP_VERSION_MINOR" value="0" />
        <property name="APP_VERSION_BUILD" value="0" />

        <!-- For MIDlet developers building WTK examples, we need to know the
        location of the WTK. -->
        <condition property="WTK_APPS_ROOT" value="${env.WTK_HOME}/apps">
            <isset property="env.WTK_HOME" />
        </condition>

        <condition property="APP_ROOT" value="${WTK_APPS_ROOT}/${APP_PROJECT_NAME}">
            <isset property="APP_MIDLET_FROM_WTK" />
        </condition>
        <property name="APP_ROOT" value="${PROJECT_ROOT}" />

        <!-- assert that any referenced WTK midlet can be found -->
        <condition property="WTK_PROJECT_NOT_FOUND" value="true">
            <and>
                <isset property="APP_MIDLET_FROM_WTK" />
                <not>
                    <available file="${APP_ROOT}" />
                </not>
            </and>
        </condition>
        <fail if="WTK_PROJECT_NOT_FOUND" message="could not find the referenced WTK project: ${APP_PROJECT_NAME}" />


        <condition property="TMP_SRC_ROOT" value="${APP_ROOT}/${APP_SRC_PATH}">
            <isset property="APP_SRC_PATH" />
        </condition>
        <property name="TMP_SRC_ROOT" value="${APP_ROOT}" />

        <!-- custom task to determine SRC version from LIB version -->
        <taskdef name="srcversion" classname="SrcVersion" classpath="${DANGER_HOME}/tools/tasks/SrcVersion.jar" />
        <srcversion property="TARGET_SRC_VERSION" libversion="${TARGET_LIB_VERSION}" srcpath="${TMP_SRC_ROOT}" />

        <!-- try to set the APP_SRC_ROOT -->
        <condition property="APP_SRC_ROOT" value="${TMP_SRC_ROOT}/${TARGET_SRC_VERSION}">
            <isset property="TARGET_SRC_VERSION" />
        </condition>
        <condition property="APP_SRC_ROOT" value="${TMP_SRC_ROOT}/${APP_COMMON_PATH}">
            <available file="${TMP_SRC_ROOT}/${APP_COMMON_PATH}" type="dir" />
        </condition>
        <property name="APP_SRC_ROOT" value="${TMP_SRC_ROOT}" />

        <!-- optional directory to allow for class file overriding -->
        <property name="APP_COMMON_SRC_ROOT" value="${TMP_SRC_ROOT}/${APP_COMMON_PATH}" />

        <property name="APP_BIN_ROOT" value="${APP_ROOT}/${APP_BIN_PATH}" />
        <property name="APP_RES_ROOT" value="${APP_ROOT}/${APP_RES_PATH}" />

        <condition property="APP_LOCALE_ROOT" value="${APP_SRC_ROOT}/locale">
            <available file="${APP_SRC_ROOT}/locale" type="dir" />
        </condition>
        <property name="APP_LOCALE_ROOT" value="${PROJECT_ROOT}/locale" />

        <!-- default to "en_US" -->
        <property name="LOCALE" value="en_US" />

        <condition property="SCREEN" value="qvga">
          <and>
			<or>
            	<equals arg1="${TARGET_LIB_VERSION}" arg2="4.0" />
			</or>
            <not>
                <isset property="SCREEN"/>
            </not>
          </and>
        </condition>

        <condition property="SCREEN" value="wqvga">
          <and>
			<or>
            	<equals arg1="${TARGET_LIB_VERSION}" arg2="4.4" />
			</or>
            <not>
                <isset property="SCREEN"/>
            </not>
          </and>
        </condition>

        <condition property="SCREEN" value="color">
            <not>
                <isset property="SCREEN"/>
            </not>
        </condition>

		<property name="PARTNER" value="default"/>

        <!-- expansions for macros in .rsrc files -->
        <!-- first set up the proper search path for assets (wqvga/qvga/color) -->
        <condition property="DRC_BASE_MACRO_PREFIX" value="./assets/${PARTNER}/wqvga/,../default/assets/${PARTNER}/wqvga/,./assets/default/wqvga/,../default/assets/default/wqvga/,./assets/${PARTNER}/qvga/,../default/assets/${PARTNER}/qvga/,./assets/default/qvga/,../default/assets/default/qvga/,">
            <equals arg1="${SCREEN}" arg2="wqvga" />
        </condition>
        <condition property="DRC_BASE_MACRO_PREFIX" value="./assets/${PARTNER}/qvga/,../default/assets/${PARTNER}/qvga/,./assets/default/qvga/,../default/assets/default/qvga/,">
            <equals arg1="${SCREEN}" arg2="qvga" />
        </condition>
		<property name="DRC_BASE_MACRO_PREFIX" value=""/>
        <property name="DRC_BASE_MACRO" value="${DRC_BASE_MACRO_PREFIX}./assets/${PARTNER}/color/,../default/assets/${PARTNER}/color/,./assets/default/color/,../default/assets/default/color/" />

        <property name="DRC_LOCALE_MACRO" value="./,../default/" />
        <property name="DRC_SOURCE_MACRO" value="${WORKING_SOURCE_ROOT}/" />
        <property name="DRC_OS_VERSION_MACRO" value="${TARGET_LIB_VERSION}" />
        <property name="DRC_RESOURCES_MACRO" value="${WORKING_RESOURCES_ROOT}" />

        <!-- application type for the Manifest file -->
        <condition property="APP_TYPE" value="J2ME">
            <isset property="APP_IS_MIDLET" />
        </condition>
        <property name="APP_TYPE" value="App" />

        <property name="ORIGIN" value="Danger SDK/${APP_TYPE}" />

        <!-- allow specifying the shorter BUILD variable -->
        <condition property="TARGET_LIB_BUILD" value="${BUILD}">
            <isset property="BUILD" />
        </condition>
        <!-- default build numbers for particular lib versions -->
        <!-- TODO: how to automate this discovery -->
        <condition property="TARGET_LIB_BUILD" value="57324">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="1.1" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="69244"> 
            <equals arg1="${TARGET_LIB_VERSION}" arg2="1.3" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="82508">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="2.0" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="79609"> 
            <equals arg1="${TARGET_LIB_VERSION}" arg2="2.1" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="96350">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="2.3" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="138112">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="2.4" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="122587">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="3.0" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="133296">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="3.1" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="149695">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="3.3" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="155053">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="3.4" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="184074">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="4.0" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="179450">
            <equals arg1="${TARGET_LIB_VERSION}" arg2="4.4" />
        </condition>
        <condition property="TARGET_LIB_BUILD" value="latest">
            <not>
                <isset property="TARGET_LIB_BUILD" />
            </not>
        </condition>
        <fail unless="TARGET_LIB_BUILD" message="Don't know what BUILD to use for version ${TARGET_LIB_VERSION}" />

        <property name="SDK_LIBS_ROOT" value="${DANGER_HOME}/libs" />

        <property name="TARGET_LIB_ROOT" value="${SDK_LIBS_ROOT}/${TARGET_LIB_VERSION}/${TARGET_LIB_BUILD}" />

        <!-- halt processing if this target doesn't exist -->
        <condition property="TARGET_LIB_ROOT_EXISTS" value="true">
            <available file="${TARGET_LIB_ROOT}" type="dir" /> 
        </condition>
        <fail unless="TARGET_LIB_ROOT_EXISTS" message="The release target '${TARGET_LIB_ROOT}' does not exist..." />

        <!-- Build the correct export file list for mkbundle.  We only want to 
        include libmidp export for MIDlets.  -->
        <condition property="EXPORT_LIST" value="">
            <or>
                <equals arg1="${TARGET_LIB_VERSION}" arg2="1.1" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="1.3" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="2.0" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="2.1" />
            </or>
        </condition>    
        <condition property="EXPORT_LIST" value="-e ${TARGET_LIB_ROOT}/libdanger/export">
            <not>
                <equals arg1="${APP_FULL_CLASS_NAME}" arg2="danger.midlet.Launcher" />
            </not>
        </condition>
        <property name="EXPORT_LIST" 
                  value="-e ${TARGET_LIB_ROOT}/libdanger/export,${TARGET_LIB_ROOT}/libmidp/export -m ${TARGET_LIB_ROOT}/libmidp/forName_export" />

        <!-- set properties for tools -->
        <condition property="TOOLS_EXT" value=".exe">
            <os family="windows" />
        </condition>
        <property name="TOOLS_EXT" value="" />

        <condition property="TOOLS_ROOT" value="${DANGER_HOME}/tools/win32">
            <os family="windows" />
        </condition>
        <condition property="TOOLS_ROOT" value="${DANGER_HOME}/tools/linux">
            <and>
                <os family="unix" />
                <not>
                    <os family="mac" />
                </not>
            </and>
        </condition>
        <condition property="TOOLS_ROOT" value="${DANGER_HOME}/tools/macosx">
            <os family="mac" />
        </condition>

        <!-- DRC_EXTRA_DEFINES allows a developer to pass additional defines to
             be used in DRC condtionals (@IF {foo}={bar}...)
             By default we just pass a simple flag that does nothing, which can
             be overridden for whatever reason. -->
         <condition property="DRC_EXTRA_DEFINES" value="-Dfoo=bar">
            <not>
                <isset property="DRC_EXTRA_DEFINES"/>
            </not>
        </condition>

        <!-- allow a selection of Java compilers, javac or jikes (or whatever ant supports, really) -->
        <!-- with the exception that jikes doesn't seem to cope with java 1.5 bytecode. oops. -->
		<!-- we sort of hack this up here by using a screen definition, because coincidentally you
			 can use 1.5 bytecode only on the OSes that support multiple screen sizes, so yay. -->

		<condition property="JAVAC_COMPILER" value="${COMPILER}">
		  <and>
            <isset property="COMPILER"/>
            <equals arg1="${SCREEN}" arg2="color"/>
          </and>
        </condition>

        <property name="JAVAC_COMPILER" value="modern"/>

        <!-- should call javac with deprecation flag on? Yes, please! -->
        <condition property="WITH_DEPRECATION" value="true">
            <not>
                <isset property="WITH_DEPRECATION" />
            </not>
        </condition>
        <condition property="COMPILER_DEPRECATION" value="on">
            <istrue value="${WITH_DEPRECATION}" />
        </condition>
        <property name="COMPILER_DEPRECATION" value="off"/>

        <!-- should call javac with debug flag on? Yes by default -->
        <condition property="WITH_DEBUG" value="true">
            <not>
                <isset property="WITH_DEBUG" />
            </not>
        </condition>
        <condition property="COMPILER_DEBUG" value="on">
            <istrue value="${WITH_DEBUG}" />
        </condition>
        <property name="COMPILER_DEBUG" value="off"/>

        <!-- should call javac with optimize flag on? No by default -->
        <condition property="COMPILER_OPTIMIZE" value="on">
                <istrue value="${WITH_OPTIMIZE}" />
        </condition>
        <property name="COMPILER_OPTIMIZE" value="off"/>

        <!-- should call javac with verbose flag on? No by default -->
        <condition property="COMPILER_VERBOSE" value="on">
                <istrue value="${WITH_VERBOSE}" />
        </condition>
        <property name="COMPILER_VERBOSE" value="off"/>

        <!-- setting "DRC_COMPRESSION" true will pass -z to drc to compress strings -->
        <condition property="DRC_COMPRESS_STRINGS" value="-z">
            <istrue value="${DRC_COMPRESSION}" />
        </condition>
        <property name="DRC_COMPRESS_STRINGS" value=""/>

		<!-- set the output (device) bytecode version based on what the device OS will support -->
		<condition property="JVM_BYTECODE_VERSION" value="1.3">
            <or>
                <equals arg1="${TARGET_LIB_VERSION}" arg2="1.1" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="1.1e" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="1.3" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="2.0" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="2.1" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="2.3" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="2.4" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="3.0" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="3.1" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="3.2" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="3.3" />
                <equals arg1="${TARGET_LIB_VERSION}" arg2="3.4" />
            </or>
		</condition>

		<condition property="JVM_BYTECODE_VERSION" value="1.5">
			<not>
				<isset property="JVM_BYTECODE_VERSION"/>
			</not>
		</condition>


    </target>

    <target name="summary"
            depends="common,fix-permissions">
        <echo>Build summary</echo>
        <echo>-------------</echo>
        <echo>project:     ${APP_PROJECT_NAME}</echo>
        <echo>class:       ${APP_FULL_CLASS_NAME}</echo>
        <echo>type:        ${APP_TYPE}</echo>
        <echo>main locale: ${LOCALE}</echo>
        <echo>library:     ${TARGET_LIB_VERSION}/${TARGET_LIB_BUILD}</echo>
        <echo>source:      ${APP_SRC_ROOT}</echo>
		<echo>screen:      ${SCREEN}</echo>
        <echo>compiler:    ${JAVAC_COMPILER} (deprecation=${COMPILER_DEPRECATION} debug=${COMPILER_DEBUG} optimize=${COMPILER_OPTIMIZE} verbose=${COMPILER_VERBOSE} bytecode=${JVM_BYTECODE_VERSION})</echo>
    </target>

    <target name="fix-permissions"
            depends="common"
            description="change permissions on the tools directory to make them executable (for Mac/Linux)">
        <chmod dir="${TOOLS_ROOT}" perm="ugo+rx" includes="**/*" />
    </target>

    <target name="clone"
            depends="common"
            description="Carbon-copy the current project to a new directory specified by the NAME parameter">
        <fail unless="NAME" message="clone target requires a NAME parameter" />

        <property name="TARGET_PROJECT_ROOT" value="${DANGER_PROJECTS_HOME}/${NAME}" />
        <echo>cloning this project to '${TARGET_PROJECT_ROOT}'... </echo>

        <condition property="TARGET_PROJECT_EXISTS" value="true">
            <available file="${TARGET_PROJECT_ROOT}" />
        </condition>
        <fail if="TARGET_PROJECT_EXISTS" message="file or directory '${TARGET_PROJECT_ROOT}' already exists" />

        <mkdir dir="${TARGET_PROJECT_ROOT}" />
        <copy todir="${TARGET_PROJECT_ROOT}">
            <fileset dir="${PROJECT_ROOT}" />
        </copy>
    </target>

    <target name="dre"
            depends="common"
            description="edit your application's resources in dre">
        <java jar="${DANGER_HOME}/tools/dre.jar" fork="true">
            <!-- make sure no spaces between flag and argument -->
            <arg line="-i${APP_LOCALE_ROOT}/${LOCALE}/${APP_RSRC_FILE_NAME} -Dbase={${DRC_BASE_MACRO}} -DlocaleDir={${DRC_LOCALE_MACRO}}" />
        </java>
    </target>

    <target name="check-locale-exists"
            depends="common">
        <!-- set CURRENT_LOCALE if not set already -->
        <basename property="CURRENT_LOCALE" file="${basedir}" />
        <condition property="${CURRENT_LOCALE}_EXISTS" value="true">
            <available file="${WORKING_LOCALE_ROOT}/${CURRENT_LOCALE}/${APP_RSRC_FILE_NAME}" />
        </condition>
    </target>

    <target name="copy-locale-clean"
            depends="common">
        <delete dir="${WORKING_LOCALE_ROOT}" failonerror="false" quiet="true" />
    </target>

    <target name="copy-locale"
            depends="copy-locale-clean,check-locale-root-exists"
            if="LOCALE_ROOT_EXISTS">
        <mkdir dir="${WORKING_LOCALE_ROOT}" />
        <!-- exclude RSRC files -->
        <copy todir="${WORKING_LOCALE_ROOT}" failonerror="false" overwrite="true">
            <fileset dir="${APP_LOCALE_ROOT}" excludes="**/*.rsrc" />
        </copy>
        <!-- copy RSRC files using token filters -->
        <copy todir="${WORKING_LOCALE_ROOT}" failonerror="false" overwrite="true" filtering="true">
            <!-- ignore any old crufty rdb files (we should probably delete them or at least warn) -->
            <fileset dir="${APP_LOCALE_ROOT}" includes="**/*.rsrc,**/*.rdb,**/*.rdb.dup" />
            <!-- enables dynamic strings in your RSRC file -->
            <!-- TODO: add more tokens here, or make more extensible -->
            <filterset>
                <filter token="APP_VERSION_MAJOR" value="${APP_VERSION_MAJOR}" />
                <filter token="APP_VERSION_MINOR" value="${APP_VERSION_MINOR}" />
                <filter token="APP_VERSION_BUILD" value="${APP_VERSION_BUILD}" />
            </filterset>
        </copy>
    </target>

    <target name="resources-for-locale" 
            depends="check-locale-exists"
            if="${CURRENT_LOCALE}_EXISTS">
        <echo>current locale: ${CURRENT_LOCALE} </echo>
        <!-- -se -sr flags prevent the interface files from being generated -->
        <condition property="OUTPUT_INTERFACE_FILES" value="">
            <equals arg1="${CURRENT_LOCALE}" arg2="${LOCALE}" />
        </condition>
        <condition property="OUTPUT_INTERFACE_FILES" value="-se -sr">
            <not>
                <equals arg1="${CURRENT_LOCALE}" arg2="${LOCALE}" />
            </not>
        </condition>
        <exec executable="${TOOLS_ROOT}/drc${TOOLS_EXT}" failonerror="true">    
            <arg line="${OUTPUT_INTERFACE_FILES} ${DRC_COMPRESS_STRINGS} -Dscreen=${SCREEN} -Dresources=${DRC_RESOURCES_MACRO} -Dsource=${DRC_SOURCE_MACRO} -Dos_version=${DRC_OS_VERSION_MACRO} -Dbase=${DRC_BASE_MACRO} -DlocaleDir=${DRC_LOCALE_MACRO} -DPARTNER=${PARTNER} -Dsdk=true ${DRC_EXTRA_DEFINES} -C -i ${WORKING_LOCALE_ROOT}/${CURRENT_LOCALE}/${APP_RSRC_FILE_NAME} -o ${WORKING_RESOURCES_ROOT}/${PARTNER}_${CURRENT_LOCALE}_${SCREEN}.rdb" />
        </exec>
    </target>

    <target name="resources" 
            depends="prepare,copy-source,copy-locale,resources-all-locales" />

    <target name="resources-clean"
            depends="common">
        <delete failonerror="false" quiet="true">
            <fileset dir="${WORKING_RESOURCES_ROOT}" />
        </delete>
    </target>

    <target name="resources-all-locales"
            depends="check-locale-root-exists"
            if="LOCALE_ROOT_EXISTS">
        <mkdir dir="${WORKING_RESOURCES_ROOT}" />
        <iterate-over-locales target="resources-for-locale" />
    </target>

    <target name="check-locale-root-exists"
            depends="common">
        <condition property="LOCALE_ROOT_EXISTS" value="true">
            <available file="${APP_LOCALE_ROOT}" />
        </condition>
        <!-- TODO: should fail here if LOCALE_ROOT_EXISTS isn't set, and we're not building from JAR?? -->
    </target>

    <!-- loop over all available locales -->
    <macrodef name="iterate-over-locales">
        <attribute name="target" />
        <sequential>
            <!-- do the main locale first, so that interface files are present for the others -->
            <subant target="@{target}" inheritall="false" genericantfile="${ant.file}">
                <property name="PROJECT_ROOT" value="${PROJECT_ROOT}" />
                <!-- TODO: can't guarantee this order? -->
                <dirset dir="${WORKING_LOCALE_ROOT}" includes="${LOCALE}" />
                <dirset dir="${WORKING_LOCALE_ROOT}" includes="*_*" excludes="${LOCALE},${EXCLUDE_LOCALES}" />
            </subant>
        </sequential>
    </macrodef>

    <target name="prepare" 
            depends="version-clean,unjar,collect-nonjar-resources,copy-jad-file" />

    <target name="version-clean"
            depends="copy-source-clean,compile-clean,copy-locale-clean,resources-clean,jar-clean,bundle-clean" />

    <target name="unjar"
            depends="compile-clean,resources-clean"
            if="APP_FROM_JAR">
        <mkdir dir="${WORKING_CLASSES_ROOT}"/>
        <unjar src="${APP_BIN_ROOT}/${APP_JAR_FILE_NAME}" dest="${WORKING_CLASSES_ROOT}">
            <patternset includes="**/*.class" />
        </unjar>

        <mkdir dir="${WORKING_RESOURCES_ROOT}"/>
        <unjar src="${APP_BIN_ROOT}/${APP_JAR_FILE_NAME}" dest="${WORKING_RESOURCES_ROOT}">
            <patternset excludes="**/*.class" />
        </unjar>

        <!-- pdm jars will contain .rdb.dup files, but we need them as .rdb files -->
        <copy todir="${WORKING_RESOURCES_ROOT}">
            <mapper type="glob" from="*.rdb.dup" to="*.rdb" />
            <fileset dir="${WORKING_RESOURCES_ROOT}" includes="**/*.rdb.dup" />
        </copy>
    </target>

    <!-- collect non-jar resources -->
    <target name="collect-nonjar-resources"
            depends="copy-res-dir"
            unless="APP_FROM_JAR" />

    <target name="copy-res-dir"
            depends="res-dir-exists"
            if="HAS_RES_DIR">
        <mkdir dir="${WORKING_RESOURCES_ROOT}"/>
        <copy todir="${WORKING_RESOURCES_ROOT}">
            <fileset dir="${APP_RES_ROOT}"/>
        </copy>
    </target>

    <target name="res-dir-exists"
            depends="common"
            unless="APP_FROM_JAR">
        <condition property="HAS_RES_DIR" value="true">
            <available file="${APP_RES_ROOT}" type="dir" />
        </condition>
    </target>
    
    <!-- copy jad file -->
    <target name="copy-jad-file"
            depends="jad-file-exists"
            if="HAS_JAD_FILE">
        <mkdir dir="${WORKING_RESOURCES_ROOT}/META-INF/com/danger" />
        <copy file="${APP_BIN_ROOT}/${APP_JAD_FILE_NAME}" tofile="${WORKING_RESOURCES_ROOT}/META-INF/MANIFEST.MF" />
        <copy file="${APP_BIN_ROOT}/${APP_JAD_FILE_NAME}" tofile="${WORKING_RESOURCES_ROOT}/META-INF/com/danger/MIDlet.jad" />
    </target>

    <target name="jad-file-exists"
            depends="common"
            if="APP_IS_MIDLET">
        <condition property="HAS_JAD_FILE" value="true">
            <available file="${APP_BIN_ROOT}/${APP_JAD_FILE_NAME}" type="file" />
        </condition>
    </target>

    <target name="copy-source-clean"
            depends="common">
        <delete dir="${WORKING_SOURCE_ROOT}" failonerror="false" quiet="true" />
    </target>

    <target name="copy-source"
            depends="copy-source-clean"
            unless="APP_FROM_JAR">
        <mkdir dir="${WORKING_SOURCE_ROOT}" />
        <copy todir="${WORKING_SOURCE_ROOT}" failonerror="false" overwrite="true">
            <fileset dir="${APP_COMMON_SRC_ROOT}" includes="**/*.java" />
        </copy>
        <copy todir="${WORKING_SOURCE_ROOT}" failonerror="false" overwrite="true">
            <fileset dir="${APP_SRC_ROOT}" includes="**/*.java" excludes="${WORK_PATH}/**" />
        </copy>
    </target>

    <target name="precompile"
            description="This empty target can be overriden by an import file specifying any specific tasks to be performed before the compilation step"/>

    <target name="postcompile"
            description="This empty target can be overriden by an import file specifying specific post-compilation tasks to be performed after compiling - like obfuscators/optimizers, etc"/>


    <target name="build" 
            depends="compile,postcompile,bundle"
            description="Compile your application and create bundle"/>

    <target name="compile"
            depends="summary,prepare,copy-source,precompile,resources"
            unless="APP_FROM_JAR">
        <mkdir dir="${WORKING_CLASSES_ROOT}"/>
        <javac source="${JVM_BYTECODE_VERSION}" 
               target="${JVM_BYTECODE_VERSION}" 
               compiler="${JAVAC_COMPILER}"
               srcdir="${WORKING_SOURCE_ROOT}" 
               destdir="${WORKING_CLASSES_ROOT}" 
               classpath="${TARGET_LIB_ROOT}/library.jar" 
               deprecation="${COMPILER_DEPRECATION}"
               optimize="${COMPILER_OPTIMIZE}"
               verbose="${COMPILER_VERBOSE}"
               debug="${COMPILER_DEBUG}"/>
<!-- uncomment this if your IDE needs it (like intelliJ) -->
<!--
        <copy todir="${APP_SRC_ROOT}" failonerror="false" overwrite="true">
            <fileset dir="${WORKING_SOURCE_ROOT}" includes="**/Commands.java" />
            <fileset dir="${WORKING_SOURCE_ROOT}" includes="**/Resources.java" />
        </copy>
-->
<!-- FIXME should parse rsrc events and interface file names for this and not assume-->

    </target>

    <target name="compile-clean"
            depends="common">
        <delete dir="${WORKING_CLASSES_ROOT}" failonerror="false" quiet="true" />
    </target>

    <target name="bundle-resources"
			depends="build">
        <mkbundle output="-o ${WORKING_CLASSES_ROOT}/application.dat" />
    </target>


    <target name="jar" 
            depends="build,bundle-resources">
        <jar destfile="${WORKING_ROOT}/${APP_PROJECT_NAME}.jar">
            <fileset dir="${WORKING_CLASSES_ROOT}" />
            <fileset dir="${WORKING_RESOURCES_ROOT}" excludes="**/*.rdb" />
        </jar>
    </target>

    <target name="jar-clean"
            depends="common">
        <delete file="${WORKING_ROOT}/${APP_PROJECT_NAME}.jar" failonerror="false" quiet="true" />
    </target>

    <!-- run in simulator -->
    <target name="run" 
            depends="jar" 
            description="Run the application in the simulator">
        <java classname="danger.Boot" fork="true" dir="${DANGER_HOME}">
            <jvmarg value="-Dcom.danger.username=${DANGER_USER}"/>
            <jvmarg value="-Dcom.danger.screen.color_space=color16"/>
            <jvmarg value="-Djava.net.preferIPv4Stack=true"/>
            <jvmarg value="-Dcom.danger.os_version=${TARGET_LIB_VERSION}"/>
			<jvmarg value="-Dfile.encoding=UTF8"/>
            <classpath>
                <pathelement location="${TARGET_LIB_ROOT}/library.jar"/>
                <pathelement location="${SDK_LIBS_ROOT}/${TARGET_LIB_VERSION}/simulator.jar"/>
                <pathelement location="${WORKING_ROOT}/${APP_PROJECT_NAME}.jar"/>
            </classpath>
        </java>
    </target>

    <target name="bundle" 
            depends="compile,postcompile">
        <mkbundle input="${EXPORT_LIST} ${TARGET_LIB_ROOT}/library.link ${WORKING_CLASSES_ROOT}" 
                  output="-o ${WORKING_ROOT}/${APP_PROJECT_NAME}_${SCREEN}_${TARGET_LIB_BUILD}.bndl -l ${WORKING_ROOT}/${APP_PROJECT_NAME}_${SCREEN}_${TARGET_LIB_BUILD}.lst" />
    </target>

    <target name="bundle-clean"
            depends="common">
        <delete failonerror="false" quiet="true">
            <fileset dir="${WORKING_ROOT}" includes="**/*.bndl" />
        </delete>
    </target>

    <!-- mkbundle macro -->
    <macrodef name="mkbundle">
        <attribute name="input" default="" />
        <attribute name="output" />
        <sequential>
            <fileset id="RDB_FILE_SET" dir="${WORKING_RESOURCES_ROOT}" includes="**/*_${SCREEN}.rdb" />
            <pathconvert targetos="unix" pathsep=" " property="RDB_FILE_PATHS" refid="RDB_FILE_SET"/>
            <exec executable="${TOOLS_ROOT}/${TARGET_LIB_VERSION}/mkbundle${TOOLS_EXT}" failonerror="true">
                <arg line="-n ${APP_BUNDLE_NAME} --vorigin '${ORIGIN}' --vpublish 0.0.0.${APP_VERSION_MAJOR}.${APP_VERSION_MINOR}.${APP_VERSION_BUILD} --vbuild ${APP_VERSION_BUILD} @{output} ${RDB_FILE_PATHS} @{input}"/>
            </exec>
        </sequential>
    </macrodef>

    <!-- master clean -->
    <target name="clean"
            depends="common"
            description="Clean up all working files in the project directory">
        <iterate-over-libs target="version-clean" />
        <delete dir="${WORK_ROOT}" failonerror="false" quiet="true" />
    </target>

    <target name="generate-manifest"
            depends="common">
        <manifest file="${MANIFEST_PATH}">
            <attribute name="hiptop-Version-Major" value="${APP_VERSION_MAJOR}"/>
            <attribute name="hiptop-Version-Minor" value="${APP_VERSION_MINOR}"/>
            <attribute name="hiptop-Version-Build" value="${APP_VERSION_BUILD}"/>
            <attribute name="hiptop-App-Classname" value="${APP_FULL_CLASS_NAME}"/>
            <attribute name="hiptop-App-Type" value="${APP_TYPE}"/>
        </manifest>
    </target>

    <target name="pdm"
            depends="clean,pdm-build-all-versions"
            description="Build a jarfile from your application, suitable for PDM submission">
        <mkdir dir="${WORK_ROOT}/META-INF" />
        <antcall target="generate-manifest">
            <param name="MANIFEST_PATH" value="${WORK_ROOT}/META-INF/MANIFEST.MF" />
        </antcall>
        <!-- PDM expects .rdb.dup files, so need to copy .rdb files -->
        <copy todir="${WORK_ROOT}">
            <mapper type="glob" from="*.rdb" to="*.rdb.dup" />
            <fileset dir="${WORK_ROOT}" includes="**/*.rdb" />
        </copy>
        <jar destfile="${WORK_ROOT}/${APP_PROJECT_NAME}_pdm.jar" manifest="${WORK_ROOT}/META-INF/MANIFEST.MF">
            <fileset dir="${WORK_ROOT}" includes="**/*.rdb.dup" />
            <fileset dir="${WORK_ROOT}" includes="**/*.MF" />
            <fileset dir="${WORK_ROOT}" includes="**/*.class" />
        </jar>
    </target>

    <target name="pdm-build-for-version"
            depends="build">
        <move todir="${WORKING_ROOT}">
            <fileset dir="${WORKING_CLASSES_ROOT}" includes="**/*.class" />
        </move>
        <mkdir dir="${WORKING_ROOT}/META-INF" />
        <antcall target="generate-manifest">
            <param name="MANIFEST_PATH" value="${WORKING_ROOT}/META-INF/MANIFEST.MF" />
        </antcall>
    </target>

    <target name="pdm-call-build-for-version"
            depends="common">
        <!-- set CURRENT_TARGET_LIB_VERSION if not set already -->
        <basename property="CURRENT_TARGET_LIB_VERSION" file="${basedir}" />
        <echo>current version: ${CURRENT_TARGET_LIB_VERSION}</echo>
        <ant target="pdm-build-for-version" inheritAll="false" dir="${PROJECT_ROOT}">
            <property name="TARGET_LIB_VERSION" value="${CURRENT_TARGET_LIB_VERSION}" />
        </ant>
    </target>

    <target name="pdm-build-all-versions"
            depends="common">
        <!-- loop over all available libs -->
        <iterate-over-libs target="pdm-call-build-for-version" />
    </target>

    <!-- loop over all available libraries -->
    <macrodef name="iterate-over-libs">
        <attribute name="target" />
        <sequential>
            <subant target="@{target}" inheritAll="false" genericantfile="${ant.file}">
                <property name="PROJECT_ROOT" value="${PROJECT_ROOT}" />
                <dirset dir="${SDK_LIBS_ROOT}" includes="*.*" excludes="${EXCLUDE_LIBS}" />
            </subant>
        </sequential>
    </macrodef>

    <target name="template"
            depends="template-src,template-locale,template-res,template-bin"
            description="Based on the build.xml file, build out the correct project structure, including a standard RSRC file">
        <echo>Project structure created based on information from the build.xml.</echo>
    </target>

    
    <target name="template-src"
            depends="template-src-check"
            unless="NO_SRC_DIR">
        <mkdir dir="${APP_SRC_ROOT}" />
        <echo>CREATED DIRECTORY: ${APP_SRC_ROOT}

In any project being built from source (except for WTK examples), this
directory should contain your source code.

There are three ways to lay out the source for your project, all of 
which are described in the "Understanding the SDK" document at
http://developer.danger.com/docs/sdkbasics.
        </echo>
    </target>

    <target name="template-src-check"
            depends="common">
        <condition property="NO_SRC_DIR">
            <or>
                <isset property="APP_MIDLET_FROM_WTK" />
                <isset property="APP_FROM_JAR" />
            </or>
        </condition>
    </target>

    <target name="template-bin"
            depends="template-bin-midlet,template-bin-hiptop" />

    <target name="template-bin-midlet"
            depends="template-bin-midlet-check"
            unless="NO_BIN_DIR">
        <mkdir dir="${APP_BIN_ROOT}" />
        <echo>CREATED DIRECTORY: ${APP_BIN_ROOT}

In any MIDlet project (except for WTK examples), this directory must 
contain the MIDlet's JAD file.  If the JAD file is not named after the 
app's class name, an alternate filename should be provided in the 
APP_JAD_FILE_NAME property.

In a MIDlet project being built from JAR (except for WTK examples), this 
directory should also contain the JAR file.  If the JAR file is not named
after the app's class name, an alternate filename should be provided in
the APP_JAR_FILE_NAME property.</echo>
    </target>

    <target name="template-bin-midlet-check"
            depends="common">
        <condition property="NO_BIN_DIR">
            <or>
                <isset property="APP_MIDLET_FROM_WTK" />
                <not>
                    <isset property="APP_IS_MIDLET" />
                </not>
            </or>
        </condition>
    </target>

    <target name="template-bin-hiptop"
            depends="common"
            if="APP_FROM_JAR"
            unless="APP_IS_MIDLET">
        <mkdir dir="${APP_BIN_ROOT}" />
        <echo>CREATED DIRECTORY: ${APP_BIN_ROOT}

In a hiptop app being built from JAR, this directory should contain that 
JAR file.  If the JAR file is not named after the app's class name, an 
alternate filename should be provided in the APP_JAR_FILE_NAME property.</echo>
    </target>

    <!-- no way to determine if this should be here or not -->
    <target name="template-res"
            depends="template-res-check"
            unless="NO_RES_DIR">
        <mkdir dir="${APP_RES_ROOT}" />
        <echo>CREATED DIRECTORY: ${APP_RES_ROOT}

This directory is for any Java resources your app uses.</echo>
    </target>

    <target name="template-res-check"
            depends="common">
        <condition property="NO_RES_DIR">
            <or>
                <isset property="NO_SRC_DIR" />
                <not>
                    <isset property="APP_IS_MIDLET" />
                </not>
            </or>
        </condition>
    </target>

    <target name="template-locale"
            depends="template-locale-mkdir,template-locale-midlet,template-locale-hiptop" />

    <target name="template-locale-mkdir"
            depends="common">
        <mkdir dir="${APP_LOCALE_ROOT}/${LOCALE}" />
        <echo>CREATED DIRECTORY: ${APP_LOCALE_ROOT}/${LOCALE}</echo>
        <mkdir dir="${APP_LOCALE_ROOT}/default" />
        <echo>CREATED DIRECTORY: ${APP_LOCALE_ROOT}/default

Assets that are not to be localized should go in the default directory. 
All other assets should be put in the individual locale (${LOCALE} in
this case).
</echo>
    </target>

    <target name="template-locale-midlet"
            depends="common"
            if="APP_IS_MIDLET">
        <echo 
            file="${APP_LOCALE_ROOT}/${LOCALE}/${APP_RSRC_FILE_NAME}"
>string ID_APP_NAME		"MIDlet-${APP_PROJECT_NAME}"
string ID_APP_CLASS		"danger.midlet.Launcher"
string ID_LOCALE		"${LOCALE}"
javaResources			"{resources}"
        </echo>
        <echo>CREATED FILE: ${APP_LOCALE_ROOT}/${LOCALE}/${APP_RSRC_FILE_NAME}

A template RSRC file has been created with default entries in it for a 
standard MIDlet.  You may wish to edit this file to change properties
associated with your MIDlet.  RSRC file format docs can be found on the
developer site at http://developer.danger.com/docs.</echo>
    </target>

    <target name="template-locale-hiptop"
            depends="common"
            unless="APP_IS_MIDLET">
        <!-- calculate the actual package directory for the app -->
        <!-- FIXME: will fail if no package? -->
        <pathconvert dirsep="/" property="APP_PACKAGE_DIR">
            <path path="${APP_PACKAGE_NAME}" />
            <mapper type="unpackage" from="${basedir}${file.separator}*" to="*" />
        </pathconvert>

        <echo
            file="${APP_LOCALE_ROOT}/${LOCALE}/${APP_RSRC_FILE_NAME}"
>package "${APP_PACKAGE_NAME}"

interface "{source}${APP_PACKAGE_DIR}/Resources.java"
events "{source}${APP_PACKAGE_DIR}/Commands.java"

string ID_APP_NAME "${APP_PROJECT_NAME}"
string ID_APP_CLASS "${APP_FULL_CLASS_NAME}"
string ID_APP_FLAGS ""
string ID_LOCALE "${LOCALE}"

#bitmap ID_LARGE_ICON "{base}LargeIcon.png"
#bitmap ID_SMALL_ICON "{base}SmallIcon.png"

# NOTE: see the helloworld example app for an explanation of this logic
@ IF {os_version} = {1.1}
#bitmap ID_SPLASH_SCREEN 			"{base}SplashScreen.png"
#int ID_SPLASH_SCREEN_NO_TITLE 	10
@ ELSEIF {os_version} = {1.3, 2.0, 2.1}
#bitmap ID_SPLASH_SCREEN 			"{base}SplashScreen.png"
#bitmap ID_SPLASH_SCREEN_NO_TITLE 	"{base}SplashScreenNoTitle.png"
@ ELSE
#bitmap ID_SPLASH_SCREEN_NO_TITLE	"{base}SplashScreenTransparent.png"
@ ENDIF</echo>
       <echo>CREATED FILE: ${APP_LOCALE_ROOT}/${LOCALE}/${APP_RSRC_FILE_NAME}

A template RSRC file has been created with default entries in it for a 
standard hiptop application.  You may wish to edit this file to change 
properties associated with your application.  RSRC file format docs can 
be found on the developer site at http://developer.danger.com/docs.</echo>
    </target>
</project>
