<?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="royale-compiler" default="main" basedir=".">

    <property file="${basedir}/env.properties"/>
    <property environment="env"/>
    <property file="${basedir}/local.properties"/>
    <property file="${basedir}/build.properties"/>

    <property name="kit.prefix" value="apache-royale-compiler-${release.version}"/>
    <property name="source.kit" value="${kit.prefix}-src"/>
    <property name="binary.kit" value="${kit.prefix}-bin"/>
    <property name="staging-dir" value="${basedir}/temp" />
    
    <!--
     Optional jars but should be in place for a real release build.
     
     For <grep> put AntelopeTasks_*.jar in this path.
     See version-update.
     
     For <rat> apache-rat-0.8.jar and apache-rat-tasks-0.8.jar should be in classpath.
     See rat-check.
     -->
    <path id="anttask.classpath">
        <fileset dir="${env.ANT_HOME}/lib">
            <include name="**/AntelopeTasks_*.jar"/>
            <include name="**/apache-rat*.jar"/>
        </fileset>
        <fileset dir="${user.home}">
            <include name=".ant/lib/**/AntelopeTasks_*.jar"/>
            <include name=".ant/lib/**/apache-rat*.jar"/>
        </fileset>
    </path>

    <target name="javadoc" description="Builds Royale's Javadoc at generated/javadoc.">
        <!--<ant dir="compiler" target="javadoc"/>-->
    </target>

    <target name="eclipse" description="Prepares or updates Royale's project(s) for use in Eclipse. This takes care of generating Java files for lexers, parsers, and BURMs.">
        <ant dir="compiler" target="eclipse"/>
        <ant dir="compiler.tests" target="eclipse"/>
    </target>

    <target name="sdk" description="Produces an set of jars in compiler/lib.">
        <ant dir="compiler-build-tools" />
        <ant dir="compiler-jburg-types" />
        <ant dir="compiler-test-utils" />
        <ant dir="compiler-common" />
        <ant dir="compiler-externc" />
        <ant dir="compiler-playerglobalc" />
        <ant dir="compiler" />
    </target>

    <target name="swf" depends="sdk, javadoc, tests" description="Builds SWF compiler, builds Royale Javadoc, and runs SWF compiler tests."/>

    <target name="main" depends="swf, jx, oem, debugger, formatter, linter, anttasks, royaleunit.anttasks" description="Builds Royale SWF compiler, then Royale JS Transpiler"/>

    <target name="jx" depends="compiler.jx, compiler.jx.tests" description="Builds Royale JS Transpiler" />
    
    <target name="oem" depends="compiler.oem" description="Builds FB Integration JAR" />

    <target name="tests" description="Runs the tests." unless="skip.compiler.tests">
        <ant dir="compiler" target="test"/>
    </target>

    <target name="compiler.oem" depends="swfutils,compiler.jx" description="Builds FB Integration JAR">
        <ant dir="flex-compiler-oem" target="main"/>
    </target>

    <target name="swfutils" description="Builds SWFUtils for debugger and compiler.oem">
        <ant dir="swfutils" target="main"/>
    </target>

    <target name="anttasks" description="Builds ant tasks JAR">
        <ant dir="royale-ant-tasks" target="jar"/>
    </target>

    <target name="royaleunit.anttasks" description="Builds RoyaleUnit ant tasks JAR">
        <ant dir="royaleunit-ant-tasks" target="jar"/>
    </target>

    <target name="debugger" depends="swfutils" description="Builds FDB JAR">
        <ant dir="debugger" target="jar"/>
    </target>

    <target name="formatter" description="Builds formatter JAR">
        <ant dir="formatter" target="main"/>
    </target>

    <target name="linter" description="Builds linter JAR">
        <ant dir="linter" target="main"/>
    </target>
    
    <target name="compiler.jx" description="Builds Royale JS Transpiler.">
        <ant dir="compiler-jx" target="main"/>
    </target>

    <target name="compiler.jx.tests" description="Runs the tests." unless="skip.compiler.jx.tests">
        <ant dir="compiler-jx" target="test"/>
    </target>

    <target name="ant.tests" description="Runs the tests." unless="skip.compiler.jx.tests">
        <ant dir="royale-ant-tasks" target="test"/>
    </target>

    <target name="royaleunit.ant.tests" description="Runs the tests." unless="skip.compiler.jx.tests">
        <ant dir="royaleunit-ant-tasks" target="test"/>
    </target>

    <target name="copyLastSuccessfulBuild" description="Copies last royale-asjs build.">
        <ant dir="compiler-jx/src/test" target="copyLastSuccessfulBuild"/>
    </target>
    
    <target name="clean-swf" description="Cleans the build output but leaves any downloaded JARs.">
        <ant dir="compiler" target="clean"/>
        <ant dir="compiler/src/test" target="clean"/>
    </target>

    <target name="clean" description="Cleans the build output but leaves any downloaded JARs.">
        <ant dir="compiler-build-tools" target="clean"/>
        <ant dir="compiler-test-utils" target="clean"/>
        <ant dir="compiler-jburg-types" target="clean"/>
        <ant dir="compiler-common" target="clean"/>
        <ant dir="compiler-externc" target="clean"/>
        <ant dir="compiler-playerglobalc" target="clean"/>
        <ant dir="compiler" target="clean"/>
        <ant dir="compiler/src/test" target="clean"/>
        <ant dir="compiler-jx" target="clean"/>
        <ant dir="compiler-jx/src/test" target="clean"/>
        <ant dir="flex-compiler-oem" target="clean"/>
        <ant dir="swfutils" target="clean"/>
        <ant dir="royale-ant-tasks" target="clean"/>
        <ant dir="royaleunit-ant-tasks" target="clean"/>
        <ant dir="debugger" target="clean"/>
        <ant dir="formatter" target="clean"/>
        <ant dir="linter" target="clean"/>
    </target>

    <target name="wipe" description="Wipes out everything that didn't come from Git.">
        <ant dir="compiler-build-tools" target="wipe"/>
        <ant dir="compiler-test-utils" target="wipe"/>
        <ant dir="compiler-jburg-types" target="wipe"/>
        <ant dir="compiler-common" target="wipe"/>
        <ant dir="compiler-externc" target="wipe"/>
        <ant dir="compiler-playerglobalc" target="wipe"/>
        <ant dir="compiler" target="wipe"/>
        <ant dir="compiler/src/test" target="wipe"/>
    </target>
    
    <target name="wipe-all" description="Wipes out everything that didn't come from Git.">
        <ant dir="compiler-build-tools" target="wipe"/>
        <ant dir="compiler-jburg-types" target="wipe"/>
        <ant dir="compiler-test-utils" target="wipe"/>
        <ant dir="compiler-common" target="wipe"/>
        <ant dir="compiler-externc" target="wipe"/>
        <ant dir="compiler-playerglobalc" target="wipe"/>
        <ant dir="compiler" target="wipe"/>
        <ant dir="compiler/src/test" target="wipe"/>
		<ant dir="compiler-jx" target="super-clean"/>
        <ant dir="compiler-jx/src/test" target="wipe"/>
        <ant dir="flex-compiler-oem" target="wipe"/>
        <ant dir="swfutils" target="wipe"/>
        <ant dir="royale-ant-tasks" target="wipe"/>
        <ant dir="royaleunit-ant-tasks" target="wipe"/>
        <ant dir="debugger" target="clean"/>
        <ant dir="formatter" target="clean"/>
        <ant dir="linter" target="clean"/>
        <delete dir="${basedir}/out" failonerror="false" includeEmptyDirs="true"/>
        <delete dir="${basedir}/temp" failonerror="false" includeEmptyDirs="true"/>
        <!-- remove legacy folders if they exist -->
        <delete dir="${basedir}/compiler.jx" failonerror="false" includeEmptyDirs="true"/>
        <delete dir="${basedir}/compiler/generated" failonerror="false" includeEmptyDirs="true"/>
        <delete dir="${basedir}/externs" failonerror="false" includeEmptyDirs="true"/>
    </target>

    <target name="maven-artifacts" description="Installs Royale artifacts to a local maven repository">
        <ant dir="compiler" target="maven-artifacts"/>
    </target>

    <target name="check-flex-home" unless="mxmlc.jar.exists"
        description="Check FLEX_HOME for both a directory and an exe file">
        
        <echo message="FLEX_HOME is ${env.FLEX_HOME}"/>
        
        <available file="${env.FLEX_HOME}"
        type="dir"
        property="FLEX_SDK_HOME"
        value="${env.FLEX_HOME}" />
        
        <available file="${basedir}/../flex-sdk"
        type="dir"
        property="FLEX_SDK_HOME"
        value="${basedir}/../flex-sdk" />
        
        <fail message="The environment variable FLEX_HOME is not set to a directory"
        unless="FLEX_SDK_HOME"/>
        
        <condition property="mxmlc.jar.exists">
            <available file="${FLEX_SDK_HOME}/lib/mxmlc.jar" type="file"/>
        </condition>
        
        <fail message="The directory ${FLEX_SDK_HOME} does not contain mxmlc.jar"
        unless="mxmlc.jar.exists"/>
    </target>

    <!--
     Can set build.additional-packages to build additional-packages as part of a release.
     This does not build the docs.  Use doc-packages or asdoc-packages.
     -->
    <target name="release"
        depends="source-release,binary-release,rat-check"
        description="Creates source and binary kits for Apache Royale Compiler."/>
        
    <target name="source-release"
        depends="wipe-all,source-package"
        description="Packages the source release kit which is the official Apache release."/>
        
    <target name="binary-release"
        depends="setup-binary-release,main,binary-package,javadoc"
        description="Builds and packages the binary kit which is provided as a convenience."/>
        
    <!--
        Package up the Apache Royale sources.  Do not include empty directories.
                                                                                       
        Note: even if cleaned, this will package up unversioned files that happen
        to be in the tree.
                                                                                       
        FixMe: clean needs to clean - add code to detect unversioned files
    -->
    <target name="source-package" depends="stage-all,source-package-zip,source-package-tgz"
        description="Package source files required to build in zip and tar-gzip file">
    </target>
    
    <target name="stage-all" depends="stage-source,stage-source-jx" />
    
    <!--
     Packages the source distribution with ZIP.
     -->
    <target name="source-package-zip" unless="no.zip">
        <mkdir dir="${basedir}/out"/>
        <zip destfile="${basedir}/out/${source.kit}.zip" basedir="${basedir}/temp"/>
    </target>
        
    <!--
     Packages the source distribution with TAR-GZIP.
     -->
    <target name="source-package-tgz" unless="no.zip">
        <tar-gzip name="${source.kit}" />
    </target>

    <target name="stage-source"
        description="Package source files required to build in zip file" >
        <antcall target="clean-temp"/>
        <sleep seconds="3" />
        <mkdir dir="${staging-dir}"/>

        <copy todir="${staging-dir}" includeEmptyDirs="false">
            <fileset dir="${basedir}">
                <include name=".mvn/extensions.xml"/>
                <include name="build.xml"/>
                <include name="build.properties"/>
                <include name="env-template.properties"/>
                <include name="README"/>
                <include name="README_MAVEN.txt"/>
                <include name="LICENSE"/>
                <include name="LICENSE.base"/>
                <include name="NOTICE"/>
                <include name="NOTICE.base"/>
                <include name="NOTICE.fdb"/>
                <include name="NOTICE.oem"/>
                <include name="RELEASE_NOTES.md"/>
                <include name="installer.xml"/>
                <include name="installer.properties/**"/>
                <include name="pom.xml"/>
                <include name="settings-template.xml"/>
                <include name="royale-maven-plugin/pom.xml"/>
                <include name="royale-maven-plugin/src/**"/>
                <include name="src/main/**"/>
            </fileset>
        </copy>
        
        <!-- compiler -->
        <antcall target="stage-compiler"/>
        
        <!-- formatter -->
        <antcall target="stage-formatter"/>
        
        <!-- linter -->
        <antcall target="stage-linter"/>
        
        <!-- flex-compiler-oem and fdb -->
        <antcall target="stage-fb-integration"/>
        
        <!-- royale ant tasks -->
        <antcall target="stage-ant-tasks"/>
        
        <!--
         Source files have Windows line endings.  Most UNIX editors can handle
         either type of line endings but the converse is often not true.
         -->
        <fixcrlf srcdir="${staging-dir}" eol="crlf" fixlast="false">
            <exclude name="compiler/src/assembly/**"/>
            <exclude name="compiler-jx/src/assembly/**"/>
            <exclude name="compiler/target/**"/>
            <exclude name="**/assets/**"/>
            <exclude name="**/*.fla"/>
            <exclude name="**/*.flv"/>
            <exclude name="**/*.gif"/>
            <exclude name="**/*.jar"/>
            <exclude name="**/*.jpg"/>
            <exclude name="**/*.mp3"/>
            <exclude name="**/*.pbj"/>
            <exclude name="**/*.png"/>
            <exclude name="**/*.sh"/>
            <exclude name="**/*.swf"/>
            <exclude name="**/*.swc"/>
        </fixcrlf>
        <fixcrlf srcdir="${staging-dir}/compiler/src/assembly" eol="crlf" fixlast="false">
            <include name="**.bat"/>
        </fixcrlf>
        
        <!-- 
         Unix shell scripts need the correct line endings. 
         -->
        <fixcrlf srcdir="${staging-dir}" eol="unix" fixlast="false">  
            <include name="**.sh"/>
        </fixcrlf>
    </target>

    <target name="stage-compiler">
        <copy todir="${staging-dir}/compiler" includeEmptyDirs="false">
            <fileset dir="${basedir}/compiler">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="in/**"/>
                <exclude name="lib/**"/>
                <exclude name="target/classes/**"/>
                <exclude name="target/test-classes/**"/>
                <exclude name="target/junit-temp/**"/>
                <exclude name="target/junit-reports/**"/>
                <exclude name="**/unittest.properties" />
            </fileset>
        </copy>
        <copy todir="${staging-dir}/compiler-build-tools" includeEmptyDirs="false">
            <fileset dir="${basedir}/compiler-build-tools">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="in/**"/>
                <exclude name="lib/**"/>
                <exclude name="target/classes/**"/>
                <exclude name="target/test-classes/**"/>
                <exclude name="target/junit-temp/**"/>
                <exclude name="target/junit-reports/**"/>
                <exclude name="**/unittest.properties" />
            </fileset>
        </copy>
        <copy todir="${staging-dir}/compiler-test-utils" includeEmptyDirs="false">
            <fileset dir="${basedir}/compiler-test-utils">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="in/**"/>
                <exclude name="lib/**"/>
                <exclude name="target/classes/**"/>
                <exclude name="target/test-classes/**"/>
                <exclude name="target/junit-temp/**"/>
                <exclude name="target/junit-reports/**"/>
                <exclude name="**/unittest.properties" />
            </fileset>
        </copy>
        <copy todir="${staging-dir}/compiler-jburg-types" includeEmptyDirs="false">
            <fileset dir="${basedir}/compiler-jburg-types">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="in/**"/>
                <exclude name="lib/**"/>
                <exclude name="target/classes/**"/>
                <exclude name="target/test-classes/**"/>
                <exclude name="target/junit-temp/**"/>
                <exclude name="target/junit-reports/**"/>
                <exclude name="**/unittest.properties" />
            </fileset>
        </copy>
        <copy todir="${staging-dir}/compiler-common" includeEmptyDirs="false">
            <fileset dir="${basedir}/compiler-common">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="in/**"/>
                <exclude name="lib/**"/>
                <exclude name="target/classes/**"/>
                <exclude name="target/test-classes/**"/>
                <exclude name="target/junit-temp/**"/>
                <exclude name="target/junit-reports/**"/>
                <exclude name="**/unittest.properties" />
            </fileset>
        </copy>
        <copy todir="${staging-dir}/compiler-externc" includeEmptyDirs="false">
            <fileset dir="${basedir}/compiler-externc">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="in/**"/>
                <exclude name="lib/**"/>
                <exclude name="target/classes/**"/>
                <exclude name="target/test-classes/**"/>
                <exclude name="target/junit-temp/**"/>
                <exclude name="target/junit-reports/**"/>
                <exclude name="**/unittest.properties" />
            </fileset>
        </copy>
	<copy todir="${staging-dir}/compiler-playerglobalc" includeEmptyDirs="false">
		<fileset dir="${basedir}/compiler-playerglobalc">
			<include name="**"/>
			<exclude name=".classpath" />
			<exclude name=".project" />
			<exclude name=".settings/**" />
			<exclude name="in/**"/>
			<exclude name="lib/**"/>
			<exclude name="target/classes/**"/>
			<exclude name="target/test-classes/**"/>
			<exclude name="target/junit-temp/**"/>
			<exclude name="target/junit-reports/**"/>
			<exclude name="**/unittest.properties" />
		</fileset>
	</copy>

        <tstamp>
            <format property="build.number.date" pattern="yyyyMMdd" />
        </tstamp>
        <property name="build.version" value="${release.version}.${build.number.date}" />
        
        <!-- Update all Version.as files in the kit frameworks directory with build.number -->
        <!--<antcall target="version-update"/>-->
    </target>

    <target name="stage-formatter">
        <copy todir="${staging-dir}/formatter" includeEmptyDirs="false">
            <fileset dir="${basedir}/formatter">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="in/**"/>
                <exclude name="lib/**"/>
                <exclude name="target/classes/**"/>
                <exclude name="target/test-classes/**"/>
                <exclude name="target/junit-temp/**"/>
                <exclude name="target/junit-reports/**"/>
                <exclude name="**/unittest.properties" />
            </fileset>
        </copy>
    </target>

    <target name="stage-linter">
        <copy todir="${staging-dir}/linter" includeEmptyDirs="false">
            <fileset dir="${basedir}/linter">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="in/**"/>
                <exclude name="lib/**"/>
                <exclude name="target/classes/**"/>
                <exclude name="target/test-classes/**"/>
                <exclude name="target/junit-temp/**"/>
                <exclude name="target/junit-reports/**"/>
                <exclude name="**/unittest.properties" />
            </fileset>
        </copy>
    </target>

    <target name="stage-fb-integration">
        <copy todir="${staging-dir}/debugger" includeEmptyDirs="false">
            <fileset dir="${basedir}/debugger">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="target/flex/**"/>
                <exclude name="target/flash/**"/>
                <exclude name="target/generated-sources/**"/>
                <exclude name="target/META-INF/**"/>
            </fileset>
        </copy>
        <copy todir="${staging-dir}/flex-compiler-oem" includeEmptyDirs="false">
            <fileset dir="${basedir}/flex-compiler-oem">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="target/classes/**"/>
            </fileset>
        </copy>
        <copy todir="${staging-dir}/swfutils" includeEmptyDirs="false">
            <fileset dir="${basedir}/swfutils">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="target/classes/**"/>
            </fileset>
        </copy>
    </target>

    <target name="stage-ant-tasks">
        <copy todir="${staging-dir}/royale-ant-tasks" includeEmptyDirs="false">
            <fileset dir="${basedir}/royale-ant-tasks">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="target/classes/**"/>
                <exclude name="target/flex/**"/>
                <exclude name="target/flash/**"/>
                <exclude name="target/generated-sources/**"/>
                <exclude name="target/META-INF/**"/>
            </fileset>
        </copy>
        <copy todir="${staging-dir}/royaleunit-ant-tasks" includeEmptyDirs="false">
            <fileset dir="${basedir}/royaleunit-ant-tasks">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="target/classes/**"/>
                <exclude name="target/flex/**"/>
                <exclude name="target/flash/**"/>
                <exclude name="target/generated-sources/**"/>
                <exclude name="target/META-INF/**"/>
            </fileset>
        </copy>
    </target>

    <target name="stage-source-jx"
        description="Package jx source files with other royale files required to build in zip file" >
        
        <copy todir="${staging-dir}" >
            <fileset dir="${basedir}">
                <include name="LICENSE.bin"/>
            </fileset>
        </copy>
        
        <copy todir="${staging-dir}/compiler-jx" includeEmptyDirs="false">
            <fileset dir="${basedir}/compiler-jx">
                <include name="**"/>
                <exclude name=".classpath" />
                <exclude name=".project" />
                <exclude name=".settings/**" />
                <exclude name="classes/**"/>
                <exclude name="in/**"/>
                <exclude name="lib/**"/>
                <exclude name="target/classes/**"/>
                <exclude name="target/test-classes/**"/>
                <exclude name="target/junit-temp/**"/>
                <exclude name="target/junit-results/**"/>
                <exclude name="**/unittest.properties" />
            </fileset>
        </copy>
        
        <!--
         Source files have Windows line endings.  Most UNIX editors can handle
         either type of line endings but the converse is often not true.
         -->
        <fixcrlf srcdir="${staging-dir}" eol="crlf" fixlast="false">
            <exclude name="compiler/src/assembly/**"/>
            <exclude name="compiler-jx/src/assembly/**"/>
            <exclude name="**/assets/**"/>
            <exclude name="**/*.fla"/>
            <exclude name="**/*.flv"/>
            <exclude name="**/*.gif"/>
            <exclude name="**/*.jar"/>
            <exclude name="**/*.jpg"/>
            <exclude name="**/*.mp3"/>
            <exclude name="**/*.pbj"/>
            <exclude name="**/*.png"/>
            <exclude name="**/*.sh"/>
            <exclude name="**/*.swf"/>
            <exclude name="**/*.swc"/>
        </fixcrlf>
        <fixcrlf srcdir="${staging-dir}/compiler/src/assembly" eol="crlf" fixlast="false">
            <include name="**.bat"/>
        </fixcrlf>
        <fixcrlf srcdir="${staging-dir}/compiler-jx/src/assembly" eol="crlf" fixlast="false">
            <include name="**.bat"/>
        </fixcrlf>
        <fixcrlf srcdir="${staging-dir}/compiler/src/assembly" eol="unix" fixlast="false">
            <include name="**"/>
            <exclude name="**.bat"/>
        </fixcrlf>
        <fixcrlf srcdir="${staging-dir}/compiler-jx/src/assembly" eol="unix" fixlast="false">
            <include name="**"/>
            <exclude name="**.bat"/>
        </fixcrlf>
        <chmod dir="${staging-dir}/compiler/src/assembly" excludes="**/*.bat" perm="+x" />
        <chmod dir="${staging-dir}/compiler-jx/src/assembly" excludes="**/*.bat" perm="+x" />
        
        <!--
         Unix shell scripts need the correct line endings.
         -->
        <fixcrlf srcdir="${staging-dir}" eol="unix" fixlast="false">
            <include name="**.sh"/>
        </fixcrlf>
    </target>

    <target name="setup-binary-release" depends="wipe-all"
        description="Set properties needed to turn on features for release sdk">
	    <!--
         For a release build, download everything fresh.
         Build changes to the downloads, might not be caught by the refresh logic.
         thirdparty-clean should leave this directory empty but just in case...
         -->
	    <delete dir="${basedir}/in" failonerror="false" includeEmptyDirs="true" />
        
    </target>
    
    <target name="binary-package"
        description="Package binary files in zip and tar-gzip file.">
        
        <antcall target="stage-source"/>
        <antcall target="stage-binary"/>
        
        <antcall target="binary-package-zip"/>
        <antcall target="binary-package-tgz"/>
        
    </target>

    <target name="stage-binary" >
        <!-- these files are in addition to the remaining source files -->
        
        <!-- generated -->
        <copy todir="${staging-dir}/compiler/lib">
            <fileset dir="${basedir}/compiler/lib">
                <include name="**"/>
                <exclude name="external/**"/>
                <exclude name="**/env.properties"/>
            </fileset>
        </copy>
        
        <antcall target="stage-source-jx" />
        
        <!-- generated -->
        <mkdir dir="${staging-dir}/js"/>
        <copy todir="${staging-dir}/js">
            <fileset dir="${basedir}/compiler-jx">
                <include name="lib/**"/>
                <exclude name="lib/commons-io**"/>
                <exclude name="lib/flex-tool**"/>
                <exclude name="lib/google/**"/>
                <exclude name="lib/args4j**"/>
                <exclude name="lib/guava**"/>
                <exclude name="lib/org.json**"/>
                <exclude name="lib/junit**"/>
            </fileset>
        </copy>
    </target>
    
    <!--
     Packages the binary distribution with ZIP.
     -->
    <target name="binary-package-zip" unless="no.zip">
        <mkdir dir="${basedir}/out"/>
        <zip destfile="${basedir}/out/${binary.kit}.zip" basedir="${basedir}/temp"/>
    </target>

    <!--
     Packages the binary distribution with TAR-GZIP.
     -->
    <target name="binary-package-tgz" unless="no.zip">
        <tar-gzip name="${binary.kit}" />
    </target>

    <!-- was colliding with the one in royale-asjs?  we aren't packaging up compiler artifacts for now anyway.
     tar with gzip compression, the temp directory and put it in the out directory.
     The shell scripts in the bin directory (no extension) and other .sh files have
     mode set to execute.
     
     name - the basename name of the kit in out directory, without the .tar.gz extension
    <macrodef name="tar-gzip">
        <attribute name="name"/>
        <sequential>
            <mkdir dir="${basedir}/out"/>
            <tar destfile="${basedir}/out/@{name}.tar.gz"
                compression="gzip"
                longfile="gnu">
                <tarfileset dir="${basedir}/temp" prefix="@{name}">
                    <include name="**" />
                    <exclude name="**/*.sh" />
                </tarfileset>
                <tarfileset dir="${basedir}/temp" prefix="@{name}">
                    <include name="**/*.bat" />
                </tarfileset>
                <tarfileset dir="${basedir}/temp" prefix="@{name}" mode="755">
                    <include name="**/*.sh" />
                    <exclude name="**/*.bat" />
                </tarfileset>
            </tar>
        </sequential>
    </macrodef>
     -->

    <!--
     Run the Apache Rat audit tool against the source in the source kit.
     The report is written to rat.report.
     
     To check a subset of files run with -Drat.dir=<dir>.
     
     You need to place apache-rat-tasks-0.8.jar and apache-rat-0.8.jar in the
     anttask.classpath in order to do this.  If the jar isn't found, the report
     will not be generated.
     -->
    <target name="rat-check" depends="rat-taskdef" if="have.rattasks"
        description="Report on licenses in source kit.">
        
        <property name="rat.dir" value="${basedir}/temp"/>
        <antcall target="rat-unzip" />
        
        <property name="rat.report" value="${basedir}/rat.report"/>
        <echo message="Checking files at ${rat.dir}, report is ${rat.report}"/>
        
        <rat:report xmlns:rat="antlib:org.apache.rat.anttasks" reportFile="${rat.report}">
            <fileset dir="${rat.dir}">
                <!--          Start of binary files           -->
                <!-- exclude media (png, gif, jpg, mp3, flv) -->
                <exclude name="**/*.png"/>
                <exclude name="**/*.gif"/>
                <exclude name="**/*.jpg"/>
                <exclude name="**/*.mp3"/>
                <exclude name="**/*.flv"/>
                <exclude name="**/org.apache.royale.tools.RoyaleToolGroup"/>
                <exclude name="debugger/META-INF/MANIFEST.MF"/>
                <exclude name="**/*.patch"/>
                <exclude name="LICENSE.bin"/>
                <exclude name="README_MAVEN.txt"/>
                <exclude name="NOTICE.base"/>
                <exclude name="NOTICE.fdb"/>
                <exclude name="NOTICE.oem"/>
            </fileset>
        </rat:report>
    </target>

    <target name="rat-unzip" unless="no.zip">
        <antcall target="clean-temp" />
        <unzip src="${basedir}/out/${source.kit}.zip" dest="${rat.dir}"/>
    </target>

    <target name="rat-taskdef" description="Rat taskdef">
        <available property="have.rattasks"
        resource="org/apache/rat/anttasks/antlib.xml"
        classpathref="anttask.classpath"/>
        
        <antcall target="have-rattasks"/>
        <antcall target="no-rattasks"/>
    </target>

    <target name="have-rattasks" if="have.rattasks">
        <typedef resource="org/apache/rat/anttasks/antlib.xml"
        uri="antlib:org.apache.rat.anttasks"
        classpathref="anttask.classpath"/>
    </target>

    <target name="no-rattasks" unless="have.rattasks">
        <echo message="Rat report not generated."/>
        <echo message="rat jars (apache-rat-*.jar, apache-rat-tasks-*.jar)"/>
        <echo message="not found in anttask.classpath"/>
    </target>

    <target name="clean-temp" unless="noclean.temp">
        <delete dir="${basedir}/temp" failonerror="false" includeEmptyDirs="true"/>
    </target>

    <target name="create-md5" >
        <echo message="Generating MD5 hashes for release artifacts"/>
        <checksum algorithm="md5" file="${basedir}/out/${binary.kit}.tar.gz" forceOverwrite="yes"/>
        <checksum algorithm="md5" file="${basedir}/out/${binary.kit}.zip" forceOverwrite="yes"/>
        <checksum algorithm="md5" file="${basedir}/out/${source.kit}.tar.gz" forceOverwrite="yes"/>
        <checksum algorithm="md5" file="${basedir}/out/${source.kit}.zip" forceOverwrite="yes"/>
    </target>

    <target name="sign" >
        <exec executable="gpg">
            <arg value="--armor" />
            <arg value="--output" />
            <arg value="${basedir}/out/${source.kit}.zip.asc" />
            <arg value="--detach-sig" />
            <arg value="${basedir}/out/${source.kit}.zip" />
        </exec>
        
        <exec executable="gpg">
            <arg value="--armor" />
            <arg value="--output" />
            <arg value="${basedir}/out/${source.kit}.tar.gz.asc" />
            <arg value="--detach-sig" />
            <arg value="${basedir}/out/${source.kit}.tar.gz" />
        </exec>
        
        <exec executable="gpg">
            <arg value="--armor" />
            <arg value="--output" />
            <arg value="${basedir}/out/${binary.kit}.zip.asc" />
            <arg value="--detach-sig" />
            <arg value="${basedir}/out/${binary.kit}.zip" />
        </exec>
        
        <exec executable="gpg">
            <arg value="--armor" />
            <arg value="--output" />
            <arg value="${basedir}/out/${binary.kit}.tar.gz.asc" />
            <arg value="--detach-sig" />
            <arg value="${basedir}/out/${binary.kit}.tar.gz" />
        </exec>
        
    </target>

    <!-- may not work on windows -->
    <target name="inject-asf-header" >
        <replaceregexp match="${generated.by.match}"
            replace="${asfheader}${generated.by.comment}"
            byline="false"
            flags="s">
            <fileset dir="${staging-dir}">
                <include name="**/*.js" />
            </fileset>
        </replaceregexp>
    </target>

    <target name="sdk.dependent.tests" >
        <ant dir="compiler/src/test" target="sdk.dependent.tests" />
    </target>
    <target name="royale.dependent.tests" >
        <ant dir="compiler-jx/src/test" target="integration.tests.asjs" />
    </target>

    <!--
     Update various files for a new release version.
     -->
    <target name="update-version">
        <fail message="old-version and new-version must be set in the form major.minor.hotfix i.e. 0.7.0">
            <condition>
                <or>
                    <not>
                        <isset property="old-version"/>
                    </not>
                    <not>
                        <isset property="new-version"/>
                    </not>
                </or>
            </condition>
        </fail>
        <replace file="${basedir}/build.properties"
        token="${old-version}"
        value="${new-version}" />
    </target>


</project>
