<?xml version="1.0"?>
<!--

  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

-->

<!-- Note:
    If you modify this file you may have to make the same change in build_framework.xml.
    build_framework.xml is renamed to build.xml when it is packaged.
    It is used to build the frameworks directory from the zip file. 
-->
<project name="frameworks" default="main" basedir=".">

    <property name="FLEX_HOME" location=".."/>

    <!-- Required for OSX 10.6 / Snow Leopard Performance. -->
    <!-- Java 7 on Mac requires OSX 10.7.3 or higher and is 64-bit only -->
    <!-- local.d32 is set/used in build.properties so this needs to be done first. -->
    <condition property="local.d32" value="-d32">
        <and>
            <os family="mac"/>
            <matches pattern="1.6.*" string="${java.version}"/>
            <equals arg1="${sun.arch.data.model}" arg2="64"/>
            <equals arg1="${os.arch}" arg2="x86_64"/>
        </and>
    </condition>

    <!-- Property for the platform.  -->
    <condition property="isMac">
        <os family="mac"/>
    </condition>
    <condition property="isWindows">
        <os family="windows" />
    </condition>   
    <condition property="isLinux">
        <and>
          <os family="unix"/>    
          <not>
            <os family="mac"/>    
          </not>
        </and>
    </condition>  

    <property file="${FLEX_HOME}/env.properties"/>
    <property environment="env"/>
    <property file="${FLEX_HOME}/build.properties"/>
 
    <target name="main" depends="check-compile-env,clean,prepare,compile" 
        description="Clean build of all SWCs"/>

    <target name="check-compile-env" description="Check for the required environment variables">
        <ant antfile="${FLEX_HOME}/build.xml" target="check-compile-env" dir="${FLEX_HOME}"/>
    </target>
    
    <target name="prepare" depends="local-fonts-snapshot, thirdparty-downloads"/>
    
    <!--
        Font snapshot files for the JREFontManager.  
        For some fonts it is possible to determine if it is illegal to embed them.  
        If that information is known for a particular font, a warning message can be 
        displayed.if an attempt is made to embed that font.
    -->
    <target name="local-fonts-snapshot" 
        description="Files required by FlashBuilder but not by Flex.">
        <touch file="localFonts.ser"/>
        <touch file="macFonts.ser"/>
        <touch file="winFonts.ser"/>
    </target>
        
    <target name="thirdparty-downloads" unless="no.thirdparty-downloads" description="Downloads all the required thirdparty code.">
        <ant antfile="${basedir}/downloads.xml" dir="${basedir}"/>
    </target>
    
    <target name="compile" description="Builds all SWCs but not their resource bundles" depends="flex-config">

        <!-- libraries -->
        <antcall target="framework"/>
        <antcall target="core"/>
        <antcall target="authoringsupport"/>
        <antcall target="starling"/>
        <antcall target="feathers"/>
        <antcall target="feathers-mxml"/>
        
        <!-- themes -->
        <antcall target="metalworks-mobile-theme"/>
        <antcall target="metalworks-desktop-theme"/>
        <antcall target="minimal-mobile-theme"/>
        <antcall target="minimal-desktop-theme"/>
        <antcall target="aeon-desktop-theme"/>
        <antcall target="topcoat-light-mobile-theme"/>
    </target>
	
    <target name="test" description="Runs the tests for all projects that have FlexUnit tests">
        <delete dir="${FLEX_HOME}/test-reports"/>

        <antcall target="starling-test"/>
        <antcall target="feathersui-test"/>
    </target>

	<target name="flex-config" depends="playerglobal-setswfversion" description="Copy the flex/air/airmobile config templates to flex/air/airmobile-config.xml and inject version numbers">
		<copy file="${basedir}/flex-config-template.xml" tofile="${basedir}/flex-config.xml" overwrite="true">
			<filterset>
				<filter token="playerversion" value="${playerglobal.version}"/>
				<filter token="swfversion" value="${playerglobal.swfversion}"/>
				<filter token="locale" value="${locale}"/>
			</filterset>
		</copy>
        <copy file="${basedir}/air-config-template.xml" tofile="${basedir}/air-config.xml" overwrite="true">
            <filterset>
                <filter token="playerversion" value="${playerglobal.version}"/>
                <filter token="swfversion" value="${playerglobal.swfversion}"/>
                <filter token="locale" value="${locale}"/>
            </filterset>
        </copy>
        <copy file="${basedir}/airmobile-config-template.xml" tofile="${basedir}/airmobile-config.xml" overwrite="true">
            <filterset>
                <filter token="playerversion" value="${playerglobal.version}"/>
                <filter token="swfversion" value="${playerglobal.swfversion}"/>
                <filter token="locale" value="${locale}"/>
            </filterset>
        </copy>
	</target>
		
	<target name="playerglobal-setswfversion" description="Set the swfversion to insert into the flex config file">
		<condition property="playerglobal.swfversion" value="11">
			<equals arg1="${playerglobal.version}" arg2="10.2" />
		</condition>
		<condition property="playerglobal.swfversion" value="12">
			<equals arg1="${playerglobal.version}" arg2="10.3" />
		</condition>
		<condition property="playerglobal.swfversion" value="13">
			<equals arg1="${playerglobal.version}" arg2="11.0" />
		</condition>
		<condition property="playerglobal.swfversion" value="14">
			<equals arg1="${playerglobal.version}" arg2="11.1" />
		</condition>
		<condition property="playerglobal.swfversion" value="15">
			<equals arg1="${playerglobal.version}" arg2="11.2" />
		</condition>
		<condition property="playerglobal.swfversion" value="16">
			<equals arg1="${playerglobal.version}" arg2="11.3" />
		</condition>
		<condition property="playerglobal.swfversion" value="17">
			<equals arg1="${playerglobal.version}" arg2="11.4" />
		</condition>
		<condition property="playerglobal.swfversion" value="18">
			<equals arg1="${playerglobal.version}" arg2="11.5" />
		</condition>
		<condition property="playerglobal.swfversion" value="19">
			<equals arg1="${playerglobal.version}" arg2="11.6" />
		</condition>
		<condition property="playerglobal.swfversion" value="20">
			<equals arg1="${playerglobal.version}" arg2="11.7" />
		</condition>
		<condition property="playerglobal.swfversion" value="21">
			<equals arg1="${playerglobal.version}" arg2="11.8" />
		</condition>
		<condition property="playerglobal.swfversion" value="22">
			<equals arg1="${playerglobal.version}" arg2="11.9" />
		</condition>
		<condition property="playerglobal.swfversion" value="23">
			<equals arg1="${playerglobal.version}" arg2="12.0" />
		</condition>
		<condition property="playerglobal.swfversion" value="24">
			<equals arg1="${playerglobal.version}" arg2="13.0" />
		</condition>
		<condition property="playerglobal.swfversion" value="25">
			<equals arg1="${playerglobal.version}" arg2="14.0" />
		</condition>
		<condition property="playerglobal.swfversion" value="26">
			<equals arg1="${playerglobal.version}" arg2="15.0" />
		</condition>
		<condition property="playerglobal.swfversion" value="27">
			<equals arg1="${playerglobal.version}" arg2="16.0" />
		</condition>
        <condition property="playerglobal.swfversion" value="28">
            <equals arg1="${playerglobal.version}" arg2="17.0" />
        </condition>
        <condition property="playerglobal.swfversion" value="29">
            <equals arg1="${playerglobal.version}" arg2="18.0" />
        </condition>
        <condition property="playerglobal.swfversion" value="30">
            <equals arg1="${playerglobal.version}" arg2="19.0" />
        </condition>
        <condition property="playerglobal.swfversion" value="31">
            <equals arg1="${playerglobal.version}" arg2="20.0" />
        </condition>
	</target>
    
    <target name="other.locales" description ="Builds resource SWCs for all locales">
        <ant dir="${basedir}/projects/framework" target="other.locales"/>
        <ant dir="${basedir}/projects/playerglobal" target="other.locales" />
    </target>
    
    <target name="doc" >
        <ant dir="${basedir}/projects/framework" target="doc" />
        <ant dir="${basedir}/projects/core" target="doc" />
        <ant dir="${basedir}/projects/authoringsupport" target="doc" />
        <ant dir="${basedir}/projects/starling" target="doc" />
        <ant dir="${basedir}/projects/feathers" target="doc" />
        <ant dir="${basedir}/projects/feathers-mxml" target="doc" />
    </target>
   
    <!--
		Cleanup
	-->

    <target name="super-clean" depends="thirdparty-clean,clean" description="Cleans everything including thirdparty downloads."/>
	
    <target name="thirdparty-clean" unless="no.thirdparty-clean" description="Removes all thirdparty downloads.">
        <ant antfile="${basedir}/downloads.xml" target="clean" dir="${basedir}"/>
    </target>
    
    <target name="clean" depends="airsdk-clean" description="Cleans all SWCs and their resource bundles">
        <delete file="${basedir}/flex-config.xml"/>
        <delete file="${basedir}/air-config.xml"/>
        <delete file="${basedir}/airmobile-config.xml"/>

        <!-- Delete output from libraries -->
        <ant dir="${basedir}/projects/framework" target="clean"/>
        <ant dir="${basedir}/projects/core" target="clean"/>
        <ant dir="${basedir}/projects/authoringsupport" target="clean"/>
        <ant dir="${basedir}/projects/starling" target="clean"/>
        <ant dir="${basedir}/projects/feathers" target="clean"/>
        <ant dir="${basedir}/projects/feathers-mxml" target="clean"/>

        <!-- delete output from themes -->
        <ant dir="${basedir}/projects/metalworks-mobile-theme" target="clean"/>
        <ant dir="${basedir}/projects/metalworks-desktop-theme" target="clean"/>
        <ant dir="${basedir}/projects/minimal-mobile-theme" target="clean"/>
        <ant dir="${basedir}/projects/minimal-desktop-theme" target="clean"/>
        <ant dir="${basedir}/projects/aeon-desktop-theme" target="clean"/>
        <ant dir="${basedir}/projects/topcoat-light-mobile-theme" target="clean"/>

        <!-- delete the FlashBuilder executable directories -->
        <delete includeemptydirs="true" failonerror="false">
            <fileset dir="${basedir}/projects">
                <include name="*/bin/**"/>
            </fileset>
        </delete> 
        <!-- Delete empty folders -->
        <delete dir="${basedir}/libs"/>
        <delete dir="${basedir}/locale">
            <exclude name="**/metadata.properties"/>
        </delete>
        <delete dir="${basedir}/libs/generated"/>
        <!-- Delete only if it exists and it is empty.  air and swfobject put dirs here. -->
        <delete includeemptydirs="true" failonerror="false">
            <fileset dir="${FLEX_HOME}/templates" excludes="**/*" />
        </delete> 
        <!-- remove these if/when they actually have content -->
        <delete file="localFonts.ser"/>
        <delete file="macFonts.ser"/>
        <delete file="winFonts.ser"/>
    </target>

    <target name="airsdk-clean" description="Delete files copied from the AIR SDK">
        <!-- Delete AIR files and directories from 'prepare' target -->
        <delete file="${FLEX_HOME}/AIR SDK license.pdf"/>
        <delete file="${FLEX_HOME}/AIR SDK Readme.txt"/>
        <delete file="${FLEX_HOME}/bin/adl"/>
        <delete file="${FLEX_HOME}/bin/adl.exe"/>
        <delete file="${FLEX_HOME}/bin/adt"/>
        <delete file="${FLEX_HOME}/bin/adt.bat"/>
        <delete dir="${FLEX_HOME}/include"/>
        <delete dir="${FLEX_HOME}/install"/>
        <delete dir="${FLEX_HOME}/frameworks/libs/air"/>
        <delete dir="${FLEX_HOME}/frameworks/projects/air"/>
        <delete file="${FLEX_HOME}/lib/adt.jar"/>
        <delete file="${FLEX_HOME}/lib/win/FlashRuntimeExtensions.lib"/>
        <delete dir="${FLEX_HOME}/runtimes"/>
        <delete includeEmptyDirs="true" failonerror="false">
            <fileset dir="${FLEX_HOME}/samples">
                <include name="badge/**" />
                <include name="descriptor-sample.xml" />
                <include name="icons/**" />
            </fileset>
        </delete>
        <delete includeEmptyDirs="true" failonerror="false">
            <fileset dir="${FLEX_HOME}/templates">
            	<include name="sdk/**" />
                <include name="air/**" />
                <include name="extensions/**" />
            </fileset>
        </delete>
    </target>
    
    <target name="core" description="Clean build of core.swc">
        <ant dir="${basedir}/projects/core"/>
    </target>

    <target name="framework" description="Clean build of framework.swc and its resource bundles">
        <ant dir="${basedir}/projects/framework"/>
    </target>
    
    <target name="authoringsupport" description="Clean build of authoringsupport.swc">
        <ant dir="${basedir}/projects/authoringsupport"/>
    </target>

    <target name="starling" description="Clean build of starling.swc">
        <ant dir="${basedir}/projects/starling"/>
    </target>

    <target name="feathers" description="Clean build of feathers.swc">
        <ant dir="${basedir}/projects/feathers"/>
    </target>

    <target name="feathers-mxml" description="Clean build of feathers-mxml.swc">
        <ant dir="${basedir}/projects/feathers-mxml"/>
    </target>

    <target name="minimal-mobile-theme" description="Clean build of MinimalMobileTheme.swc">
        <ant dir="${basedir}/projects/minimal-mobile-theme"/>
    </target>

    <target name="minimal-desktop-theme" description="Clean build of MinimalDesktopTheme.swc">
        <ant dir="${basedir}/projects/minimal-desktop-theme"/>
    </target>

    <target name="aeon-desktop-theme" description="Clean build of AeonDesktopTheme.swc">
        <ant dir="${basedir}/projects/aeon-desktop-theme"/>
    </target>

    <target name="metalworks-mobile-theme" description="Clean build of MetalWorksDesktopTheme.swc">
        <ant dir="${basedir}/projects/metalworks-mobile-theme"/>
    </target>

    <target name="metalworks-desktop-theme" description="Clean build of MetalWorksDesktopTheme.swc">
        <ant dir="${basedir}/projects/metalworks-desktop-theme"/>
    </target>

    <target name="topcoat-light-mobile-theme" description="Clean build of TopcoatLightMobileTheme.swc">
        <ant dir="${basedir}/projects/topcoat-light-mobile-theme"/>
    </target>

    <target name="starling-test" description="Tests for Starling Framework">
        <ant dir="${basedir}/projects/starling" target="test"/>
    </target>

    <target name="feathersui-test" description="Tests for Feathers UI">
        <ant dir="${basedir}/projects/feathers" target="test"/>
    </target>
</project>
