<?xml version="1.0" encoding="utf-8"?>
<!--
	Build file for a Flex project with:
	- Optimized Modules
	- A single library
	- CSS compiled into a SWF and loaded dynamically
	- An HTML wrapper that includes a Remoting Endpoint
	- An image directory for runtime assets
	@author John Valentino (jvalentino.blogspot.com)
-->
<project name="OnlineSchoolClient" basedir="." default="main">
	
	<!-- This is what you have to include to be able to use "mxml" and "compc" ant tasks -->
    <taskdef resource="flexTasks.tasks" classpath="libs/flexTasks.jar"/>
	
	<!-- This is what you have to include to be able to use the "foreach" ant task -->
	<taskdef resource="net/sf/antcontrib/antlib.xml">
	  <classpath>
	    <pathelement location="libs/ant-contrib-1.0b3.jar"/>
	  </classpath>
	</taskdef>
	
	<!-- This uses a build file for all the dynamic properties -->
	<property file="build.properties" />
	
	<!-- 
		Target: main
		Specifies the main target as running the following other targets: 
		- clean: 			deletes the deployment directory
		- compile:			compiles the main application SWF file
		- compiles-modules:	compiles all the modules SWF files
		- compile-css:		compiles the CSS into a SWF file
		- compile-wrapper:	builds the HTML wrapper for the application
		- copy-images:		copies runtime images into the deployment directory
		- clean-up:			deletes generated file after the build
	-->
	<target name="main" depends="clean, compile, compile-modules, compile-css, compile-wrapper, copy-images, clean-up" />
	
	<!--
		Target: clean
		Deletes the deployment directory
	-->
	<target name="clean">
		<delete dir="${DEPLOY_DIR}"/>
	</target>
	
	<!--
		Target: compile
		Compiles the main application SWF file. Generates a link report in order for modules to be later
		optimized using it. Includes a single library SWC file.
	-->
	<target name="compile">
		<echo>${FLEX_HOME}/ant/lib/flexTasks.jar</echo>
		<mxmlc 
            file="src/${APP_NAME}.mxml" 
			debug="false"
            output="${DEPLOY_DIR}/${APP_NAME}.swf"
            actionscript-file-encoding="UTF-8"
            keep-generated-actionscript="false"
			link-report="MyReport.xml"
            incremental="true">
						
            <!-- Get default compiler options. -->
            <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>

            <!-- List of path elements that form the roots of ActionScript class hierarchies. -->
            <source-path path-element="${FLEX_HOME}/frameworks"/>

            <!-- List of SWC files or directories that contain SWC files. -->
            <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
                <include name="libs" />
                <include name="../bundles/{locale}" />
            </compiler.library-path>
			
			<library-path dir="${LIB_DIR}" append="true">
				<include name="*.swc" />
			</library-path>	

        </mxmlc>
	</target>
	
	<!--
		Target: compile-modules
		Compiles all of the modules in the module directoy into SWF files. Works by calling the
		compile-module target in a for loop on all the file in the module directory.
	-->
	<target name="compile-modules">
		<!-- 
			This looks at the module directory and converts it to a list of module names separated by commas
		 	and places it in the variable "flex.modules"
		-->
		<pathconvert property="flex.modules" pathsep=",">
			<fileset dir="${MODULE_DIR}">
		    	<include name="**/*.mxml"/>
		    </fileset>
			<!-- Strips the module name out of the full path to the module -->
			<compositemapper>
				<mapper type="regexp" from="^(.*)[\\|/](.*)(\.mxml)$$" to="\2"/>
			</compositemapper>
		</pathconvert>
		<!-- Calls the "compile-module" target and gives it the parameter "moduleName"  -->
		<foreach list="${flex.modules}" delimiter="," parallel="false" param="moduleName" trim="true" target="compile-module"/>
	</target>
	
	<!--
		Target: compile-module
		Takes the full path to the module in the module.path variable, finds the module name, and compiles
		the module optimized for the main application by linking the report XML file.
	-->
	<target name="compile-module">
		<echo>Compiling ${moduleName}</echo>
		<!-- Get the package path to the module from the full path -->
		<pathconvert property="module.path" pathsep=",">
        	<fileset dir="${MODULE_DIR}">
            	<include name="**/*.mxml"/>
         	</fileset>
         	<compositemapper>
             	<mapper type="regexp" from="^(.*)[\\|/](src)[\\|/](.*)[\\|/](${moduleName}\.mxml)$$" to="\3"/>
         	</compositemapper>
     	</pathconvert>
		<mxmlc 
            file="${MODULE_DIR}/${moduleName}.mxml" 
            output="${DEPLOY_DIR}/${module.path}/${moduleName}.swf"
            actionscript-file-encoding="UTF-8"
            keep-generated-actionscript="false"
			optimize="true"
			debug="false"
			fork="true"
			load-externs="MyReport.xml"
            incremental="false">
            <!-- Get default compiler options. -->
            <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>

            <!-- List of path elements that form the roots of ActionScript class hierarchies. -->
            <source-path path-element="${FLEX_HOME}/frameworks"/>
			
			<compiler.source-path path-element="src"/>

            <!-- List of SWC files or directories that contain SWC files. -->
            <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
                <include name="libs" />
                <include name="../bundles/{locale}" />
            </compiler.library-path>
			
			<library-path dir="${LIB_DIR}" append="true">
				<include name="*.swc" />
			</library-path>	

        </mxmlc>
	</target>
	
	<!--
		Target: compile-css
		Compiles the CSS file into a SWF file.
	-->
	<target name="compile-css">
		<!-- copy the CSS to the root level because MXMLC will not find embedded relative assets elsewhere -->
		<copy file="src/${STYLE_PATH}/${STYLE}.css" toDir="src"/>
				
		<!-- compile the CSS SWF -->
		<mxmlc 
			file="src/${STYLE}.css" 
			output="${DEPLOY_DIR}/${STYLE_PATH}/${STYLE}.swf"			
			maxmemory="256m" fork="true">
			<compiler.debug>false</compiler.debug>
			<compiler.optimize>true</compiler.optimize>
			<compiler.accessible>false</compiler.accessible>
		</mxmlc>
		
		<!-- delete the copy of the CSS file at the root level -->
		<delete file="src/${STYLE}.css"/>
	</target>
	
	<!--
		Target: copy-images
		Copies runtime images into the deployment directory
	-->
	<target name="copy-images">
		<copy todir="${DEPLOY_DIR}/${IMAGE_PATH}">
			<fileset dir="src/${IMAGE_PATH}"/>
		</copy>
	</target>
	
	<!--
		Target: compile-wrapper
		Copies runtime images into the deployment directory
	-->
	<target name="compile-wrapper">
		<copy file="${basedir}/html-template/AC_OETags.js" toFile="${DEPLOY_DIR}/AC_OETags.js" />
		<copy todir="${DEPLOY_DIR}/history">
			<fileset dir="${basedir}/html-template/history"/>
		</copy>
		<copy file="${basedir}/html-template/${HTML_TEMPLATE}" tofile="${DEPLOY_DIR}/${HTML_OUTPUT}" />
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${swf}" value="${APP_NAME}"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${width}" value="${WIDTH}"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${height}" value="${HEIGHT}"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${bgcolor}" value="${BGCOLOR}"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${application}" value="${APP_NAME}"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${version_major}" value="9"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${version_minor}" value="0"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${version_revision}" value="124"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${title}" value="${TITLE}"/>
		<!-- This is for configuring the endpoint -->
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${endpoint}" value="${ENDPOINT}"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${destination}" value="${DESTINATION}"/>
		<replace file="${DEPLOY_DIR}/${HTML_OUTPUT}" token="$${source}" value="${SOURCE}"/>
	</target>
	
	<!--
		Target: clean-up
		Cleans up after the build by deleting generated file.
	-->
	<target name="clean-up">
		<delete file="MyReport.xml" />
	</target>
    
</project>
