<!--
ADOBE SYSTEMS INCORPORATED
Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
 
NOTICE:   Adobe permits you to modify and distribute this file only in accordance with
the terms of Adobe AIR SDK license agreement.  You may have received this file from a
source other than Adobe.  Nonetheless, you may modify or distribute this file only in 
accordance with such agreement.
-->

<!--
  Before trying to build the Adobe AIR SDK frameworks, please set the flex.home property to a 
  valid Flex SDK location.
-->

<project default="main" basedir=".">
    <!-- property name="flex.home" value="C:/Flex/sdks/3.2.0"/-->
    
    <condition property="valid.flex.home">
        <and>
          <isset property="flex.home"/>
        </and>
    </condition>
    
    <target name="init">
      <fail message="Property flex.home must be set'" unless="valid.flex.home"/>
    </target>
    
    <target name="main" depends="frameworks.application_updater,frameworks.aircore"/>
    
    <property name="results" value="${basedir}/build/results" />
    <property name="intermediate" value="${basedir}/build/intermediate" />
    
    <uptodate property="application_updater_headless.uptodate" targetfile="${results}/applicationupdater.swc">
        <srcfiles dir="ApplicationUpdater/src" includes="**"/>
    </uptodate>

    <uptodate property="application_updater_ui.uptodate" targetfile="${results}/applicationupdater_ui.swc">
      <srcfiles dir="ApplicationUpdater/src" includes="**"/>
    </uptodate>
    
    <condition property="amxmlc3.1" value="${flex.home}/bin/amxmlc.bat">
        <os family="windows"/>
    </condition>
    <property name="amxmlc3.1" value="${flex.home}/bin/amxmlc"/>
    
    <macrodef name="amxmlc3.1">
        <attribute name="strict" default="true"/>
        <attribute name="output"/>
        <attribute name="source-path" default=""/>
        <attribute name="source"/>
        <attribute name="locale-path" default=""/>
        <attribute name="locales" default="" />
        <attribute name="target-player" default="9.0.124"/>
        <element name="optional-args" optional="true" />
        <element name="locale-arg" optional="true" />
        <element name="locale-path-arg" optional="true" />
        <sequential>
            <exec executable="${amxmlc3.1}" failonerror="true">
                <arg value="-strict=@{strict}"/>
                <arg value="-target-player=@{target-player}"/>
                <arg value="-headless-server"/>
                <locale-arg/>
                <arg value="-source-path+=@{source-path}"/>
                <locale-path-arg/>
                <optional-args/>
                <arg value="-output=@{output}"/>
                <arg value="@{source}"/>
            </exec>
        </sequential>
    </macrodef>
    
    <condition property="acompc3.1" value="${flex.home}/bin/acompc.bat">
        <os family="windows"/>
    </condition>
    <property name="acompc3.1" value="${flex.home}/bin/acompc"/>
        
    <macrodef name="acompc3.1">
        <attribute name="strict" default="true"/>
        <attribute name="source-path"/>
        <attribute name="output"/>
        <attribute name="debug" default="true"/>
        <attribute name="include-classes"/>
        <element name="locale-arg" optional="true" />
        <sequential>
        <exec executable="${acompc3.1}" failonerror="true">
                <arg value="-source-path=@{source-path}"/>
                <arg value="-output=@{output}"/>
                <arg value="-strict=@{strict}"/>
                <arg value="-headless-server"/>
                <arg value="-show-actionscript-warnings=true"/>
                <arg value="-compute-digest=false"/>
                <arg value="-library-path="/>
                <arg line="-debug=@{debug}"/>
                <arg line="-include-classes @{include-classes}"/>
                <locale-arg />
            </exec>
        </sequential>
    </macrodef>    
    
    <macrodef name="build_framework">
        <attribute name="strict" default="true"/>
        <attribute name="framework-path" default="."/>
        <attribute name="framework-name" default="."/>
        <attribute name="include-classes"/>
        <element name="locale-path-arg" optional="true" />
        <sequential>
                <!-- 
            COMPC with AIR classes (linked externally) and no Flex components.
                -->
            <acompc3.1
                strict="@{strict}"
                source-path="@{framework-path}"
                output="@{framework-path}/@{framework-name}.swc" 
                debug="true"
                include-classes="@{include-classes}">
                <locale-path-arg/>
            </acompc3.1>
            <acompc3.1
                strict="@{strict}"
                source-path="@{framework-path}"
                output="@{framework-path}/@{framework-name}-nodebug.swc" 
                debug="false"
                include-classes="@{include-classes}"
                >
                <locale-path-arg/>
            </acompc3.1>
            <!--
               Pull the SWF file out of the no-debug SWC.  And then delete the SWC.
               The SWF can be dynamically loaded from either FLASH or HTML applcations.
            -->
            <unzip src="@{framework-path}/@{framework-name}-nodebug.swc"
                   dest="@{framework-path}">
                <patternset>
                    <include name="library.swf"/>
                </patternset>
            </unzip>
            <delete file="@{framework-path}/@{framework-name}-nodebug.swc"/>
            <move file="@{framework-path}/library.swf" tofile="@{framework-path}/@{framework-name}.swf"/>
            <!--
            Move into the ${results} dir.
            -->
            <move file="@{framework-path}/@{framework-name}.swc" tofile="${results}/@{framework-name}.swc"/>
            <move file="@{framework-path}/@{framework-name}.swf" tofile="${results}/@{framework-name}.swf"/>
        </sequential>
    </macrodef>    
    

    <target name="frameworks.application_updater" depends="init,application_updater.application_updater_ui"/>

    <target name="application_updater.application_updater_ui" unless="application_updater_ui.uptodate" depends="application_updater.application_updater_headless">
        <mkdir dir="${intermediate}/ApplicationUpdater/ui" />
        <copy todir="${intermediate}/ApplicationUpdater/ui">
            <fileset dir="${basedir}/ApplicationUpdater/src/ApplicationUpdater" includes="**"/>
        </copy>
        <amxmlc3.1 
            output="${intermediate}/ApplicationUpdater/ui/assets/ApplicationUpdaterDialogs.swf" 
            source="${basedir}/ApplicationUpdater/src/ApplicationUpdaterDialogs/ApplicationUpdaterDialogs.mxml"
            source-path="${basedir}/ApplicationUpdater/src/ApplicationUpdaterDialogs"
            target-player="9.0.124"
        >
			<optional-args>
				<arg value="-accessible" />
			</optional-args>
            <locale-arg>
                <arg line="-locale=en_US,de,es,fr,it,ja,ko,pt,ru,zh_Hans,zh_Hant,cs,nl,pl,sv,tr" />
                <arg line="-allow-source-path-overlap=true" />
            </locale-arg>
            <locale-path-arg>
                <!-- The library-path is added to avoid adding locales to flex in SDK -->
                <arg value="-source-path+=${basedir}/ApplicationUpdater/src/ApplicationUpdaterDialogs/locale/{locale}" />
                <arg value="-library-path=${flex.home}/frameworks/libs/air/airglobal.swc" />
                <arg value="-library-path+=${flex.home}/frameworks/libs" />
                <arg value="-library-path+=${flex.home}/frameworks/libs/air" />
                <arg value="-library-path+=${flex.home}/frameworks/locale/en_US" />
                <arg value="-library-path+=${results}/applicationupdater.swc" />
            </locale-path-arg>
        </amxmlc3.1>
        <build_framework
            framework-path="${intermediate}/ApplicationUpdater/ui"
            framework-name="applicationupdater_ui"
            include-classes="air.update.ApplicationUpdaterUI"
        >
            <locale-path-arg>
                <locale-arg>
                    <!-- The library-path is added to avoid adding locales to flex in SDK -->
                    <arg value="-locale=en_US,de,es,fr,it,ja,ko,pt,ru,zh_Hans,zh_Hant,cs,nl,pl,sv,tr" />
                    <arg value="-library-path=${flex.home}/frameworks/libs" />
                    <arg value="-library-path+=${flex.home}/frameworks/libs/air" />
                    <arg value="-library-path+=${flex.home}/frameworks/locale/en_US" />
                    <arg value="-source-path+=${basedir}/ApplicationUpdater/src/ApplicationUpdaterDialogs/locale/{locale}" />
                    <arg line="-include-resource-bundles ApplicationUpdaterDialogs" />
                </locale-arg>
            </locale-path-arg>
        </build_framework>
        <move file="${results}/applicationupdater_ui.swc" tofile="${results}/applicationupdater_ui.swc"/>
    </target>
    
    <target name="application_updater.application_updater_headless" unless="application_updater_headless.uptodate">
        <mkdir dir="${intermediate}/ApplicationUpdater/headless" />
        <copy todir="${intermediate}/ApplicationUpdater/headless">
            <fileset dir="${basedir}/ApplicationUpdater/src/ApplicationUpdater">
                <exclude name="**/assets/**" />
                <exclude name="**/ui/**" />
                <exclude name="**/ApplicationUpdaterUI.as" />
            </fileset>
        </copy>
        <build_framework
            framework-path="${intermediate}/ApplicationUpdater/headless"
            framework-name="applicationupdater" 
            include-classes="air.update.ApplicationUpdater">
            <locale-path-arg>
                <locale-arg>
                    <!-- Add manifest and namespace for Flash CS3 compatibility -->
                    <arg line="-namespace http://ns.adobe.com/air/framework/update ${basedir}/ApplicationUpdater/manifest.xml"/>
                    <arg line="-include-namespaces http://ns.adobe.com/air/framework/update"/>
                </locale-arg>
            </locale-path-arg>
        </build_framework>
    </target>
    
    <uptodate property="aircore.uptodate" targetfile="${results}/lib/aircore.swc">
        <srcfiles dir="Core" includes="**"/>
    </uptodate>

    <target name="frameworks.aircore" unless="aircore.uptodate">
        <build_framework
            framework-path="${basedir}/Core/src"
            framework-name="aircore"
            include-classes="air.net.ServiceMonitor air.net.SocketMonitor air.net.URLMonitor air.net.SecureSocketMonitor air.desktop.URLFilePromise">
            <locale-path-arg>
                <locale-arg>
                    <!-- Add manifest and namespace for Flash CS3 compatibility -->
                    <arg line="-namespace http://ns.adobe.com/air/framework/core ${basedir}/Core/manifest.xml"/>
                    <arg line="-include-namespaces http://ns.adobe.com/air/framework/core"/>
                </locale-arg>
            </locale-path-arg>
        </build_framework>
    </target>

</project>
