<?xml version="1.0" encoding="UTF-8"?>
<!--
  ANT Buildfile

  Copyright 2014-2017 IS2T. All rights reserved.
  IS2T PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

-->
<project name="external-resource-loader-init">

	<import file="${scripts.dir}/extension-init.xml" />

	<target name="init/external-resource-loader/context" extensionOf="init/context">
		<condition property="external-resource-loader.input.dir" value="${ej.externalResources.input.dir}" else="${output.dir}/externalResources">
			<and>
				<isset property="ej.externalResources.input.dir"/>
				<not>
					<equals arg1="${ej.externalResources.input.dir}" arg2="" trim="true"/>
				</not>
			</and>
		</condition>
	</target>
	
	<target name="init/external-resource-loader" extensionOf="init/execution">
		<!-- CLASSPATH -->
		<augment id="init.application.classpath">
			<fileset dir="${jpf.dir}/javaLibs" includes="external-resource-loader-*.jar" />
			<fileset dir="${jpf.dir}/javaLibs" includes="sni-*.jar" />
			<fileset dir="${vm.dir}/javaLibs" includes="bon-*.jar" />
		</augment>
		
		<!-- The entry point of this library is only reference by a property -->
		<augment id="init.requires">
			<string value="com.is2t.vm.support.ExternalResourceLoader"/>
		</augment>
		
		<!-- copy all user external resource in application output dir -->
		<copy todir="${externalResources.output.dir}" failonerror="false">
			<fileset dir="${external-resource-loader.input.dir}" erroronmissingdir="false"/>
		</copy>
	</target>
	
	<!--
		Complete the S3 classpath with the folder which contains the external resources
	-->
	<target name="init/external-resource-loader/s3" extensionOf="init/execution" if="onS3">
		
		<!-- CLASSPATH -->
		<basename file="${externalResources.output.dir}" property="external-resource-loader.output.dirname"/>
		<dirname file="${externalResources.output.dir}" property="external-resource-loader.output.dir.parent"/>
		<augment id="init.application.classpath">
			<dirset dir="${external-resource-loader.output.dir.parent}" includes="${external-resource-loader.output.dirname}"/>
		</augment>
	
		<!--
			The list of resources must be added into the application's resources list for the simulator.
			Otherwise some resources will be ignored.
			
			The tools which add some files into the external resources folder have the responsability
			to update the path "init.resources"; because we are not able to fix the resolution order
			of targets which depends on extension point "init/execution".
			
			May be some files will be listed both but it is not an issue.
		-->
		<property name="external-resource-loader.output.dir2" location="${externalResources.output.dir}" description="use an unix path"/>
		<pathconvert property="external-resource-loader.files.property">
			<resources id="external-resource-loader.files">
				<fileset dir="${external-resource-loader.output.dir2}"/>
			</resources>
			<mapper>
				<chainedmapper>
					<!-- get relative path -->
					<filtermapper>
						<replacestring from="${external-resource-loader.output.dir2}${file.separator}" to=""/>
					</filtermapper>
					<!-- get unix relative path -->
					<filtermapper>
						<replacestring from="\" to="/"/>
					</filtermapper>
				</chainedmapper>
			</mapper>
		</pathconvert>
		<augment id="init.resources">
			<string value="${external-resource-loader.files.property}"/>
		</augment>
	</target>
	
</project>