<project name="build-stage">
	<import file="build-set-globals.xml" as="globals" />
	<import file="validate-android.xml" as="validate"/>

    <target name="init" depends="globals.init" />
 
	<target name="-createStaging">
		<mkdir dir="${adf.build.root.dir}" />
		<mkdir dir="${adf.build.root.dir}/src" />
	</target>

	<target name="-copyAndroidManifestTemplate" unless="oepe.adfmf.androidmanifest.paths.available">
        <copy todir="${adf.build.root.dir}">
            <fileset dir="${adf.rt.zip.root}/${adf.rt.runtime.root}">
                <include name="${adf.template.android.manifest.template.path}" />
            </fileset>
            <mapper type="merge" to="${manifest.file.name}" />
       </copy>
	</target>

	<!-- <target name="-copyGCMIntentTemplate" unless="oepe.adfmf.gcmintentservice.paths.available">
		<unzip src="${adf.rt.zip}" dest="${adf.staging.gcmintentservice.dir}">
			<patternset>
				<include name="${adf.template.gcmintent.template.zippath}" />
			</patternset>
			<mapper type="flatten" />
		</unzip>
	</target> -->

	<target name="-copyJavaResAssets"  unless="oepe.adfmf.assets.paths.available">
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/Oracle_ADFmf_Framework.zip" dest="${adf.staging.java_res.assets.dir}" prefix="${adf.template.android.java_res.assets.zippath}" />
	</target>

	<target name="-tempCopyJavaResJsAssets" unless="oepe.adfmf.jsassets.paths.available">
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/${adf.template.js.path}" dest="${adf.staging.java_res.www.js.dir}" prefix="${adf.template.android.js.zippath}" />
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/${adf.template.js.path.android}" dest="${adf.staging.java_res.www.js.dir}" prefix="${adf.template.android.js.zippath}" />
	</target>

	<target name="-copyJavaResBuildTypeAssets" unless="oepe.adfmf.buildtypeassets.paths.available">
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/Oracle_ADFmf_Framework.zip" dest="${adf.staging.java_res.assets.dir}/jvm/lib" prefix="framework/Common/build/jar" />
	</target>

	<target name="-copyRes" unless="oepe.adfmf.res.available">
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/Oracle_ADFmf_Framework.zip" dest="${adf.staging.res.dir}" prefix="${adf.template.android.res.zippath}" />
	</target>

	<target name="-copyTemplateRes" unless="oepe.adfmf.templateres.available">
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/Oracle_ADFmf_Framework.zip" dest="${adf.staging.res.dir}" prefix="${adf.template.android.res.template.zippath}" />
	</target>

    <target name="-tempCopyStringsXml" unless="oepe.adfmf.stringxml.available">
        <copy todir="${adf.staging.res.values.dir}">
            <fileset dir="${adf.rt.zip.root}">
                <include name="strings.xml" />
            </fileset>
        </copy>
    </target>

    <target name="-copyNativeLibs" unless="oepe.adfmf.nativelibs.available">
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/Oracle_ADFmf_Framework.zip" dest="${adf.staging.lib.dir}/x86" prefix="${adf.template.android.java_res_debug.libs.zippath}" />
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/Oracle_ADFmf_Framework.zip" dest="${adf.staging.lib.dir}/armeabi" prefix="${adf.template.android.java_res_debug_arm.libs.zippath}" />
        <unzip src="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/Oracle_ADFmf_Framework.zip" dest="${adf.build.root.dir}">
            <patternset>
                <include name="framework/Android/build/java_res_jvmti/jvmlibs.zip" />
            </patternset>
            <mapper type="glob" from="framework/Android/build/java_res_jvmti/*" to="*" />
        </unzip>
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/Oracle_ADFmf_Framework.zip" dest="${adf.build.root.dir}" prefix="framework/Android/build/java_res" />
    </target>
 
	<target name="-copyPreCompileJars" unless="oepe.adfmf.precompilejars.available">
        <unzip_glob src="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/Oracle_ADFmf_Framework.zip" dest="${adf.staging.precompilejars.dir}" prefix="${adf.template.build.jar.zippath}" />
	</target>

	<target name="-process-adf-android-manifest-template">
	   <fail unless="var.app.package" />
        <replace file="${adf.staging.manifest.abs.file}">
			<replacefilter token="$$$$app.package$$$$" value="${var.app.package}" />
			<replacefilter token="$$$$app.icon$$$$" value="${var.app.icon}" />
			<replacefilter token="$$$$app.name$$$$" value="${var.app.name}" />
			<replacefilter token="$$$$app.configChanges$$$$" value="${var.app.configChanges}" />
			<replacefilter token="$$$$app.label$$$$" value="${var.app.label}" />

            <!-- TODO: don't know what this is for -->
            <replacefilter token="$$$$app.scheme$$$$" value="" />
		</replace>
	</target>

	<!-- <target name="-process-adf-gcmintent-template">
		<replace file="${adf.staging.gcmintentservice.java.src}">
			<replacefilter token="$$$$app.package$$$$" value="${var.app.package}" />
		</replace>
	</target> -->

    <target name="-process-strings">
        <replace file="${adf.build.root.dir}/res/values/strings.xml">
            <replacefilter token="$strings.appName" value="${var.app.name.literal}" />
        </replace>
    </target>

	<!-- this must be done before -build-setup is called so that the Manifest 
		file and anything else where ADF has template variables, get resolved before 
		-build-setup and its deps set properties based on their content -->
    <target name="-process-adf-template" depends="-process-adf-android-manifest-template, -process-strings" />
    <!-- We need to overwrite the cordova.plist each time to ensure that whitelist defaults are present so they
     can be correctly merged with user sources -->
    <target name="-ensureFreshConfigXml" unless="oepe.build.ctrl.disable.updateDeviceAccess">
        <delete file="${adf.staging.res.xml.dir}/config.xml" failonerror="false"/>
        <copy file="${adf.rt.zip.root}/${adf.rt.runtime.root}/Android/config.xml" tofile="${adf.staging.res.xml.dir}/config.xml"/>
    </target>

    <target name="build" depends="init, -createStaging, -copyAndroidManifestTemplate, -copyJavaResAssets, -tempCopyJavaResJsAssets, -copyJavaResBuildTypeAssets, -copyRes, -copyTemplateRes, -tempCopyStringsXml, -copyNativeLibs, -copyPreCompileJars, -process-adf-template, -ensureFreshConfigXml" />

	<target name="clean" depends="init">
        <delete dir="${adf.build.root.dir}" includes="**/**" includeemptydirs="true" failonerror="true"/>
	</target>

	<!-- TODO: move to common include -->
	<macrodef name="unzip_glob">
		<attribute name="src" />
		<attribute name="dest" />
		<attribute name="prefix" />
		<sequential>
			<unzip src="@{src}" dest="@{dest}">
				<patternset>
					<include name="@{prefix}/**" />
				</patternset>
				<mapper type="glob" from="@{prefix}/*" to="*" />
			</unzip>
		</sequential>
	</macrodef>

</project>
