<?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.

-->

<project name="build_example" basedir=".">

    <condition property="ROYALE_COMPILER_HOME" value="${env.ROYALE_COMPILER_HOME}">
        <and>
            <not>
                <isset property="ROYALE_COMPILER_HOME" />
            </not>
            <available file="${env.ROYALE_COMPILER_HOME}/lib/jsc.jar" type="file" />
        </and>
    </condition>

    <condition property="ROYALE_COMPILER_HOME" value="${ROYALE_HOME}/js">
        <and>
            <not>
                <isset property="ROYALE_COMPILER_HOME" />
            </not>
            <available file="${ROYALE_HOME}/js/lib/jsc.jar" type="file" />
        </and>
    </condition>

    <condition property="ROYALE_COMPILER_HOME" value="${ROYALE_HOME}/../royale-compiler/compiler-jx">
        <and>
            <not>
                <isset property="ROYALE_COMPILER_HOME" />
            </not>
            <available file="${ROYALE_HOME}/../royale-compiler/compiler-jx/lib/jsc.jar" type="file" />
        </and>
    </condition>

    <condition property="GOOG_HOME" value="${env.GOOG_HOME}">
        <and>
            <not>
                <isset property="GOOG_HOME" />
            </not>
            <available file="${env.GOOG_HOME}/closure/goog/base.js" type="file" />
        </and>
    </condition>

    <condition property="GOOG_HOME" value="${ROYALE_HOME}/js/lib/google/closure-library">
        <and>
            <not>
                <isset property="GOOG_HOME" />
            </not>
            <available file="${ROYALE_HOME}/js/lib/google/closure-library/closure/goog/base.js" type="file" />
        </and>
    </condition>

    <condition property="playerglobal_arg" value="+env.PLAYERGLOBAL_HOME=${env.PLAYERGLOBAL_HOME}">
        <isset property="env.PLAYERGLOBAL_HOME"/>
    </condition>
    <property name="playerglobal_arg" value="-define+=CONFIG::dummy2,true" />

    <condition property="adl" value="adl.exe">
        <os family="windows"/>
    </condition>

    <condition property="adl" value="adl">
        <os family="mac"/>
    </condition>

    <condition property="runtime" value="win">
        <os family="windows"/>
    </condition>

    <condition property="runtime" value="mac">
        <os family="mac"/>
    </condition>
    
    <property name="swf.version" value="14" />

    <target name="get.browser" >
        <condition property="browser" value="${env.ProgramFiles}/Mozilla Firefox/firefox.exe">
            <and>
                <os family="windows"/>
                <available file="${env.ProgramFiles}/Mozilla Firefox/firefox.exe"
                type="file" />
            </and>
        </condition>
        <condition property="browser" value="${env.ProgramFiles(x86)}/Mozilla Firefox/firefox.exe">
            <and>
                <os family="windows"/>
                <available file="${env.ProgramFiles(x86)}/Mozilla Firefox/firefox.exe"
                type="file" />
            </and>
        </condition>
        <condition property="browser" value="C:/Program Files/Internet Explorer/iexplore.exe">
            <and>
                <os family="windows"/>
                <available file="C:/Program Files/Internet Explorer/iexplore.exe"
                    type="file" />
            </and>
        </condition>
        <condition property="browser" value="/Applications/Firefox.app/Contents/MacOS/Firefox">
            <and>
                <os family="mac"/>
                <available file="/Applications/Firefox.app/Contents/MacOS/Firefox"
                type="file" />
            </and>
        </condition>
        <condition property="browser" value="/Applications/Safari.app/Contents/MacOS/Safari">
            <and>
                <os family="mac"/>
                <available file="/Applications/Safari.app/Contents/MacOS/Safari"
                type="file" />
            </and>
        </condition>
        <fail message="No browser found.  See build_example.xml or set -Dbrowser=&lt;path to browser&gt;"
            unless="browser"/>
    </target>
    
    <path id="lib.path">
      <fileset dir="${ROYALE_COMPILER_HOME}/lib" includes="compiler-royaleTasks.jar"/>
      <fileset dir="${ROYALE_COMPILER_HOME}/lib" includes="jsc.jar"/>
    </path>
    
    <target name="compile" description="Compiles ${example}">
        <echo message="Compiling ${example}.swf"/>
        <echo message="ROYALE_HOME: ${ROYALE_HOME}"/>
        <echo message="ROYALE_COMPILER_HOME: ${ROYALE_COMPILER_HOME}"/>
        <echo message="GOOG_HOME: ${GOOG_HOME}"/>
        <available file="${basedir}/src/main/config/compile-app-config.xml"
            type="file"
            property="optional_arg" value="-load-config+=&quot;${basedir}/src/main/config/compile-app-config.xml&quot;" />

        <!-- Load the <compc> task. We can't do this at the <project> level -->
        <!-- because targets that run before flexTasks.jar gets built would fail. -->
        <taskdef resource="flexTasks.tasks" classpathref="lib.path"/>
        <property name="config_arg" value="royale" />
        <property name="file_suffix" value="mxml" />
        <property name="optional_arg" value="-define+=CONFIG::dummy,true" />
        <mxmlc fork="true"
               debug="true"
               configname="${config_arg}"
               swf-version="${swf.version}"
               closure-lib="${GOOG_HOME}"
               file="${basedir}/src/main/royale/${example}.${file_suffix}"
               output="${basedir}/bin-debug/${example}.swf">
            <jvmarg line="${mxmlc.jvm.args}"/>
            <arg line="${optional_arg}" />
            <arg value="+playerglobal.version=${playerglobal.version}" />
            <arg value="${playerglobal_arg}" />
        </mxmlc>

        <antcall target="build_example.wrapper" />
        
        <antcall target="build_example.copy-resources" />
        <copy todir="${basedir}/bin-debug/fonts" failonerror="false">
            <fileset dir="${ROYALE_HOME}/frameworks/fonts">
                <include name="**/*.swf" />
            </fileset>
        </copy>
        <antcall target="build_example.compile-js-release" />
        <antcall target="build_example.copy-app-xml" />
    </target>

    <target name="compile-js-debug">
        <property name="js.release.target" value="JSRoyale" />
        <mxmlc fork="true"
            debug="true"
            configname="${config_arg}"
            swf-version="${swf.version}"
            closure-lib="${GOOG_HOME}"
            file="${basedir}/src/main/royale/${example}.${file_suffix}"
            output="${basedir}/bin-release/${example}.swf">
            <jvmarg line="${mxmlc.jvm.args}"/>
            <arg line="${optional_arg}" />
            <arg value="-js-dynamic-access-unknown-members=true"/>
            <arg value="+playerglobal.version=${playerglobal.version}" />
            <arg value="${playerglobal_arg}" />
        </mxmlc>
    </target>

    <target name="compile-js-release">
        <property name="js.release.target" value="JSRoyale" />
        <mxmlc fork="true"
            debug="false"
            configname="${config_arg}"
            swf-version="${swf.version}"
            closure-lib="${GOOG_HOME}"
            file="${basedir}/src/main/royale/${example}.${file_suffix}"
            output="${basedir}/bin-release/${example}.swf">
            <jvmarg line="${mxmlc.jvm.args}"/>
            <arg line="${optional_arg}" />
            <arg value="-js-dynamic-access-unknown-members=true"/>
            <arg value="+playerglobal.version=${playerglobal.version}" />
            <arg value="${playerglobal_arg}" />
        </mxmlc>
    </target>

    <target name="compile-js-only-debug">
	    <available file="${basedir}/src/main/config/compile-app-config.xml"
		    type="file"
		    property="optional_arg" value="-load-config+=&quot;${basedir}/src/main/config/compile-app-config.xml&quot;" />

	    <taskdef resource="flexTasks.tasks" classpathref="lib.path"/>
	    <property name="config_arg" value="royale" />
	    <property name="file_suffix" value="mxml" />
	    <property name="optional_arg" value="-define+=CONFIG::dummy,true" />
	    <antcall target="build_example.compile-js-debug" />
    </target>
    <!-- support for skipping swf build -->
    <target name="compile-js-only">
        <available file="${basedir}/src/main/config/compile-app-config.xml"
                   type="file"
                   property="optional_arg" value="-load-config+=&quot;${basedir}/src/main/config/compile-app-config.xml&quot;" />

        <taskdef resource="flexTasks.tasks" classpathref="lib.path"/>
        <property name="config_arg" value="royale" />
        <property name="file_suffix" value="mxml" />
        <property name="optional_arg" value="-define+=CONFIG::dummy,true" />
        <antcall target="build_example.compile-js-release" />
    </target>
    
    <target name="check-air">
        <condition property="is.air" value="true">
            <equals arg1="${config_arg}" arg2="air" />
        </condition>
    </target>
    
    <target name="wrapper" depends="check-air" unless="is.air">
        <html-wrapper
        height="100%"
        width="100%"
        bgcolor="#ffffff"
        name="${example}"
        versionmajor="11"
        versionminor="1"
        versionrevision="0"
        output="${basedir}/bin-debug"/>
    </target>
    
    <target name="copy-app-xml" depends="check-air" if="is.air">
        <copy toDir="${basedir}/bin-debug" file="${basedir}/${example}-app.xml" />
        <copy toDir="${basedir}/bin-release" file="${basedir}/${example}-app.xml" />
    </target>
    
    <macrodef name="html-wrapper">
        <attribute name="width"/>
        <attribute name="height"/>
        <attribute name="bgcolor"/>
        <attribute name="name"/>
        <attribute name="versionmajor"/>
        <attribute name="versionminor"/>
        <attribute name="versionrevision"/>
        <attribute name="output"/>
            <sequential>
                <copy toFile="@{output}/@{name}.html" 
                    file="${ROYALE_HOME}/templates/swfobject/index.template.html" />
                <copy toDir="@{output}/history">
                    <fileset dir="${ROYALE_HOME}/templates/swfobject/history">
                        <include name="**"/>
                    </fileset>
                </copy>
                <copy toDir="@{output}">
                    <fileset dir="${ROYALE_HOME}/templates/swfobject">
                        <include name="*"/>
                        <exclude name="index.template.html"/>
                    </fileset>
                </copy>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${title}"
                        value="@{name}"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${bgcolor}"
                        value="@{bgcolor}"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${useBrowserHistory}"
                        value="--"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${version_major}"
                        value="@{versionmajor}"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${version_minor}"
                        value="@{versionminor}"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${version_revision}"
                        value="@{versionrevision}"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${expressInstallSwf}"
                        value="expressInstall.swf"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${expressInstallSwf}"
                        value="expressInstall.swf"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${width}"
                        value="@{width}"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${height}"
                        value="@{height}"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${application}"
                        value="@{name}"/>
                </replace>
                <replace file="@{output}/@{name}.html">
                    <replacefilter
                        token="$${swf}"
                        value="@{name}"/>
                </replace>
        </sequential>
    </macrodef>
    
    <target name="check-resources" >
        <available file="${basedir}/src/main/resources"
        type="dir"
        property="has-resources"/>
    </target>
    
    <target name="copy-resources" depends="check-resources" if="has-resources">
        <copy todir="${basedir}/bin-debug">
            <fileset dir="${basedir}/src/main/resources">
                <include name="**/*.png" />
                <include name="**/*.jpg" />
                <include name="**/*.json" />
            </fileset>
        </copy>
    </target>
    
    <target name="copy-js-resources" depends="check-resources" if="has-resources">
        <mkdir dir="${basedir}/bin/js-debug/assets" />
        <copy todir="${basedir}/bin/js-debug/assets" >
            <fileset dir="${basedir}/src/main/resources/assets">
                <include name="**" />
            </fileset>
        </copy>
        <mkdir dir="${basedir}/bin/js-release/assets" />
        <copy todir="${basedir}/bin/js-release/assets" >
            <fileset dir="${basedir}/src/main/resources/assets">
                <include name="**" />
            </fileset>
        </copy>
    </target>
    
    <target name="run.air">
        <property name="profile" value="mobileDevice" />
        <property name="screensize" value="640x960:640x960" />
        <property name="dpi" value="240" />
        <property name="platform" value="AND" />
        <exec executable="${AIR_HOME}/bin/${adl}" dir="${basedir}/bin-${which}" failonerror="true">
            <arg value="-runtime" />
            <arg value="${AIR_HOME}/runtimes/air/${runtime}" />
            <arg value="-profile" />
            <arg value="${profile}" />
            <arg value="-screensize" />
            <arg value="${screensize}" />
            <arg value="-XscreenDPI" />
            <arg value="${dpi}" />
            <arg value="-XversionPlatform" />
            <arg value="${platform}" />
            <arg value="${basedir}/bin-${which}/${example}-app.xml" />
        </exec>
    </target>

    <target name="run.air.desktop">
        <property name="profile" value="extendedDesktop" />
        <property name="screensize" value="640x960:640x960" />
        <property name="dpi" value="240" />
        <property name="platform" value="AND" />
        <exec executable="${AIR_HOME}/bin/${adl}" dir="${basedir}/bin-${which}" failonerror="true">
            <arg value="-runtime" />
            <arg value="${AIR_HOME}/runtimes/air/${runtime}" />
            <arg value="-profile" />
            <arg value="${profile}" />
            <arg value="${basedir}/bin-${which}/${example}-app.xml" />
        </exec>
    </target>

    <target name="compile.cordova" description="Executes the Cordova build script to run the app on a device." if="has.cordova">
        <property name="mobile.platform" value="android" />
        <ant antfile="${ROYALE_HOME}/cordova-build.xml" target="main" >
            <property name="projectdir" value="${basedir}" />
        </ant>
        <ant antfile="${ROYALE_HOME}/cordova-build.xml" target="load-platform.${mobile.platform}" >
            <property name="projectdir" value="${basedir}" />
        </ant>
    </target>

    <target name="run.cordova" description="Executes the Cordova build script to run the app on a device." if="has.cordova">
        <property name="mobile.platform" value="android" />
        <ant antfile="${ROYALE_HOME}/cordova-build.xml" target="run.${mobile.platform}" >
            <property name="projectdir" value="${basedir}" />
        </ant>
    </target>

</project>
