<project name="build-application">
	<import file="build-set-globals.xml" as="globals"/>
		
	<target name="init" depends="globals.init">
		<!-- TODO: need to base on user workspace -->
        <property name="adf.userdata.dotadf.dir" location="${oepe.src.root.dir}/adf" />
        <property name="oepe.adfmf.application.whitelist.script" value="whitelist.js" />
	</target>		

	<target name="clean" depends="init">
		<delete dir="${adf.staging.configuration.dir}" failonerror="false" />
		<delete dir="${adf.staging.dotadf.dir}" failonerror="false" />
        <delete dir="${adf.build.root.dir}/.adf" failonerror="false" />
	</target>
	
    <target name="build" depends="init, copyApplicationArtifacts, createWhitelist, createDeviceAccess" />

    <target name="copyApplicationArtifacts">
		<property name="oepe.adfmf.assembly.dotadf.dir.path" value="${adf.userdata.dotadf.dir}" />
		<mkdir dir="${adf.staging.configuration.dir}" />
		<copy todir="${adf.staging.configuration.dir}">
			<fileset
				file="${oepe.adfmf.assembly.dotadf.dir.path}/${oepe.adfmf.dotadf.applicationxml.rel.path}" />
			<fileset
				file="${oepe.adfmf.assembly.dotadf.dir.path}/${oepe.adfmf.dotadf.configxml.rel.path}" />
		</copy>
		<property name="oepe.adfmf.assembly.dotadf.dir.path" value="${adf.userdata.dotadf.dir}" />
        <delete dir="${adf.staging.dotadf.dir}" failonerror="false"/>
		<mkdir dir="${adf.staging.dotadf.dir}" />
		<copy todir="${adf.staging.dotadf.dir}">
			<fileset dir="${oepe.adfmf.assembly.dotadf.dir.path}/META-INF" />
		</copy>

        <copy todir="${adf.staging.jvm.lib}/security" file="${oepe.src.root.dir}/lib/security/cacerts" failonerror="false" />
        <copy todir="${adf.staging.jvm.lib}" file="${oepe.src.root.dir}/META-INF/cvm.properties"  failonerror="false" overwrite="true"/>
        <copy todir="${adf.staging.jvm.lib}" file="${oepe.src.root.dir}/META-INF/logging.properties"  failonerror="false" overwrite="true"/>
	</target>

   <target name="createWhitelist" depends="createWhitelistFromApp, globals.maybeInitWhitelistProperty" />

   <!-- Reads the staged application descriptor, parses out the device access flags and 
        stores them to device-info.properties.  This is as common routine on all platforms.
        Platform-specific scripts must pick up the properties file and map the contents to
        platform-specific device permissions per the ER 16358602 specification --> 
    <target name="createDeviceAccess">
        <script language="javascript" src="${oepe.bin.js.dir}/device-access.js" />
    </target>
    

    <!-- Reads staged and copied app resources and generates a properties file called whitelist.properties 
         in the staging area.  This is not a runtime artifact.  Rather, it lists all the white list urls that
         need to be updated in the cordova configuration.  The location and semantics of the cordova configuration
         varies by platform, so the actual update from whitelist.properties to cordova config happens in the
         platform specific build file (build-android.xml, build-ios.xml etc.). -->
    <target name="createWhitelistFromApp" depends="init">
        <script language="javascript" src="${oepe.bin.js.dir}/${oepe.adfmf.application.whitelist.script}" />
    </target>
</project>
