<?xml version="1.0" encoding="UTF-8"?>
<!--
	ANT
	
 	Copyright 2019-2025 MicroEJ Corp. All rights reserved.
 	This library is provided in source code for use, modification and test, subject to license terms.
 	Any modification of the source code will break MicroEJ Corp. warranties on the whole library.
-->
<project name="frontPanelInit">

	<dirname file="${ant.file.frontPanelInit}" property="frontPanelInit.dir"/>
	
	<import file="${frontPanelInit.dir}/../extension-init.xml" />
	<import file="${frontPanelInit.dir}/../init-hil/HILinit.xml" />
	
	<target name="init/frontpanel" if="onS3">
		<property name="fronpanel.mock.dir" location="${jpf.dir}/mocks/frontpanel"/>
		
		<!--
			List of paths separated by the path separator that will be added to the S3 classpath.
			Used by the workbench to add frontpanel local projects when 'Resolve in Workspace' option is enabled.
			Empty path by default.
		-->
		<property name="frontpanel.workbench.classpath" value=""/>
	</target>
	
	<!--
		Add the front panel mock classpath in HIL or S3 classpath.
		By default, update HIL classpath (== run front panel mock with HIL)
		Set property ${frontpanel.runWithS3} in a 'init/context' target to run front panel mock with S3.
		(note: this property must be only set when ${onS3} is set)
	-->
	
	<target name="init/frontpanel/classpath" extensionOf="init/context" depends="init/hil/context, init/frontpanel" if="onS3">
		
		<!-- exclude user ivy dependency "framework.jar" (already included in engine.jar) -->
		<patternset id="frontpanel.mock.excludes"/>
		<excludeJar name="framework"/>
		
		<!-- exclude user ivy dependency "HILEngine.jar" (already included in HIL or S3) -->
		<excludeJar name="HILEngine"/> <!-- architecture before mock-api 2.6.0 -->
		<excludeJar name="mock-api"/> <!-- architecture since mock-api 2.6.0 -->
		
		<!-- retrieve internal and user framework version to compare them at runtime -->
		<extractVersion name="framework" prefix="user"/>
		<extractVersion name="framework" prefix="intern" jar="engine"/>
		
	</target>
	
	<!-- 
		Can override the fp file to use:
			- ${frontpanel.file} not set
				-> ${ej.duik.parser.xml} == ""
				-> front panel will load the fp file defined in front panel extension jar
			- ${frontpanel.file} is set
				-> ${ej.duik.parser.xml} == ${frontpanel.file}
				-> front panel will load this fp 
	-->
	<target name="init/frontpanel/fpFile" extensionOf="init/context" depends="init/frontpanel" if="onS3">
		<property name="frontpanel.file" value=""/>
		<property name="ej.duik.parser.xml" value="${frontpanel.file}"/>
		<augment id="hil.main.properties">
			<propertyref name="ej.duik.parser.xml"/>
		</augment>
	</target>
	
	<target name="init/frontpanel/exec" if="onS3">
		<echo message="Front Panel init" level="verbose" />
		
		<!-- checks two properties actually: ${frontpanel.runWithS3} AND ${onS3} -->
		<condition property="frontpanel.runWithHIL">
			<not><isset property="frontpanel.runWithS3"/></not>
		</condition>
	</target>
	
	<target name="init/frontpanel/hil" extensionOf="init/execution" depends="init/frontpanel/exec" if="frontpanel.runWithHIL">
		<augment id="hil.main.classpath">
			<path path="${frontpanel.workbench.classpath}"/>
			<fileset dir="${fronpanel.mock.dir}">
				<patternset refid="frontpanel.mock.excludes"/>
			</fileset>
		</augment>
	</target>
	
	<target name="init/frontpanel/s3" extensionOf="init/execution" depends="init/frontpanel/exec" if="frontpanel.runWithS3">
		<augment id="s3.mock.includes">
			<path path="${frontpanel.workbench.classpath}"/>
			<fileset dir="${fronpanel.mock.dir}">
				<patternset refid="frontpanel.mock.excludes"/>
			</fileset>
		</augment>
	</target>
	
	<macrodef name="excludeJar">
		<attribute name="name"/>
		<sequential>
		
			<!-- augment fp mock exclude pattern -->
			<augment id="frontpanel.mock.excludes">
				<exclude name="@{name}*.jar"/>
			</augment>
			
			<!-- exclude pattern from ${frontpanel.workbench.classpath} -->
			<scriptdef name="excludeJarInProperty" language="javascript">
				<attribute name="pattern"/>
				<attribute name="property"/>
				<attribute name="separator"/>
				<![CDATA[
				
					var path = attributes.get("property");
					var pattern = attributes.get("pattern");
				    var source = project.getProperty(path);
					var sep = attributes.get("separator");
					
					var filteredItems = [];
				    var items = source.split(sep);
					for (var i = 0; i < items.length; i++) {
				      if (!items[i].contains(pattern)) {
						filteredItems.push(items[i]);
				      }
				    }
				
				    project.setProperty(path, filteredItems.join(sep));
				]]>
			</scriptdef>
			<excludeJarInProperty pattern="@{name}" property="frontpanel.workbench.classpath" separator="${path.separator}"/>
		
		</sequential>
	</macrodef>
	
    <macrodef name="extractVersion">
		<attribute name="name"/>
		<attribute name="prefix"/>
		<attribute name="jar" default="@{name}"/>
		<attribute name="propertiesFile" default="properties/@{name}.properties"/>
    	
        <sequential>
        	<!-- extract version from jar -->
            <loadproperties prefix="frontpanel.@{name}.@{prefix}">
            	<zipentry name="@{propertiesFile}">
	            	<fileset dir="${fronpanel.mock.dir}" includes="@{jar}*.jar"/>
            	</zipentry>
            </loadproperties>
			
			<!-- add this property to HIL context -->
			<augment id="hil.main.properties">
				<propertyref prefix="frontpanel.@{name}.@{prefix}."/>
			</augment>
        </sequential>
    </macrodef>
	
</project>