<?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="google_maps" default="main" basedir=".">
    
    <property name="google_maps.version" value="3.11" />
    
    <property name="ROYALE_HOME" value="${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/compiler-mxmlc.jar" type="file" />
        </and>
    </condition>
    
    <condition property="ROYALE_COMPILER_HOME" value="${ROYALE_HOME}/../royale-compiler/compiler">
        <and>
            <not>
                <isset property="ROYALE_COMPILER_HOME" />
            </not>
            <available file="${ROYALE_HOME}/../royale-compiler/compiler/lib/compiler-mxmlc.jar" type="file" />
        </and>
    </condition>
    
    <condition property="ROYALE_COMPILER_HOME" value="${ROYALE_HOME}">
        <and>
            <not>
                <isset property="ROYALE_COMPILER_HOME" />
            </not>
            <available file="${ROYALE_HOME}/lib/compiler-mxmlc.jar" type="file" />
        </and>
    </condition>
    
    <condition property="ROYALE_TRANSPILER_HOME" value="${env.ROYALE_TRANSPILER_HOME}">
        <and>
            <not>
                <isset property="ROYALE_TRANSPILER_HOME" />
            </not>
            <available file="${env.ROYALE_TRANSPILER_HOME}/lib/jsc.jar" type="file" />
        </and>
    </condition>
    
    <condition property="ROYALE_TRANSPILER_HOME" value="${ROYALE_HOME}/js">
        <and>
            <not>
                <isset property="ROYALE_TRANSPILER_HOME" />
            </not>
            <available file="${ROYALE_HOME}/js/lib/jsc.jar" type="file" />
        </and>
    </condition>
    
    <condition property="ROYALE_TRANSPILER_HOME" value="${ROYALE_HOME}/../royale-compiler/compiler-jx">
        <and>
            <not>
                <isset property="ROYALE_TRANSPILER_HOME" />
            </not>
            <available file="${ROYALE_HOME}/../royale-compiler/compiler-jx/lib/jsc.jar" type="file" />
        </and>
    </condition>
    
    <tstamp>
        <format property="royale.swc-date" pattern="MM/dd/yy HH:mm Z"/>
    </tstamp>
    <echo>swc-date is ${royale.swc-date}</echo>
    
    <target name="main" depends="preprocess, externc, postprocess, compc" />
    
    <target name="preprocess" depends="download">
        <exec executable="git" dir="${basedir}/..">
            <arg value="apply"/>
            <arg value="--ignore-whitespace"/>
            <arg value="--whitespace=nowarn"/>
            <arg value="${basedir}/src/main/patch/google_maps.patch"/>
        </exec>
    </target>
    
    <!-- we need to insert a line near the beginning of the file to make the
         compiler happy. -->
    <target name="make_patch" depends="download">
        <mkdir dir="${basedir}/target/reference" />
        <copy todir="${basedir}/target/reference" file="${basedir}/target/downloads/google_maps_api_v3_11.js" />
        <antcall target="preprocessfile" >
            <param name="file" value="google_maps_api_v3_11.js" />
        </antcall>
        <antcall target="diff" />
    </target>

    <target name="download" >
        <mkdir dir="${basedir}/target/downloads" />
        <get src="https://raw.githubusercontent.com/royale-extras/closure-compiler/royale/contrib/externs/maps/google_maps_api_v3_11.js" dest="${basedir}/target/downloads/google_maps_api_v3_11.js" ignoreerrors="true"/>
        <antcall target="get-from-cache-if-needed" >
            <param name="srcFile" value="google_maps_api_v3_11.js" />
            <param name="destFile" value="google_maps_api_v3_11.js" />
            <param name="destDir" value="${basedir}/target/downloads" />
        </antcall>
        <antcall target="fail-if-not-found" >
            <param name="destFile" value="google_maps_api_v3_11.js" />
            <param name="destDir" value="${basedir}/target/downloads" />
        </antcall>
    </target>
    
    <target name="preprocessfile" >
        <basename property="file.base" file="${file}" suffix=".js"/>
        <echo>file.base is ${file.base}</echo>
        <replace file="${basedir}/target/downloads/${file}" token="// Namespace" value="/** @const */" />
        <replace file="${basedir}/target/downloads/${file}">
        	<replacetoken>google.maps = {};</replacetoken>
        	<replacevalue><![CDATA[
/** @const */
var google = {};

/** @const */
google.maps = {};]]></replacevalue>
        </replace>
    </target>
    
    <target name="diff" >
        <delete dir="${basedir}/src/main/patch" />
        <mkdir dir="${basedir}/src/main/patch" />
        <exec executable="git" output="${basedir}/src/main/patch/google_maps.temp.patch" >
            <arg value="diff"/>
            <arg value="--no-index"/>
            <arg value="--"/>
            <arg value="${basedir}/target/reference"/>
            <arg value="${basedir}/target/downloads"/>
        </exec>
        <replace file="${basedir}/src/main/patch/google_maps.temp.patch" token="target/reference" value="target/downloads" />
        <replace file="${basedir}/src/main/patch/google_maps.temp.patch" token="${basedir}/target/downloads" value="/google_maps/target/downloads" />
        <concat destfile="${basedir}/src/main/patch/google_maps.patch" >
            <header filtering="no" trimleading="yes">
                Generated from Ant script
            </header>
            <filelist dir="${basedir}/src/main/patch" files="google_maps.temp.patch"/>
        </concat>
        <delete file="${basedir}/src/main/patch/google_maps.temp.patch" />
    </target>

    <target name="externc" >
        <java jar="${ROYALE_TRANSPILER_HOME}/lib/externc.jar" fork="true"
            failonerror="true">
            <arg value="+royalelib=externs" />
            <arg value="-debug" />
            <arg value="-load-config=${basedir}/src/main/config/externc-config.xml" />
        </java>
    </target>
    
    <target name="postprocess" >
    </target>
    
    <target name="compc" >
        <copy file="${basedir}/src/main/config/compile-as-config.xml"
        todir="${basedir}/target" />
        <java jar="${ROYALE_TRANSPILER_HOME}/lib/compc.jar" fork="true"
            failonerror="true">
            <arg value="+royalelib=externs/frameworks" />
            <arg value="-targets=SWF" />
            <arg value="-external-library-path+=${basedir}/../js/target/js.swc" />
            <arg value="-load-config=${basedir}/target/compile-as-config.xml" />
            <arg line="-metadata.date=&quot;${royale.swc-date}&quot;" />
            <arg line="-metadata.dateFormat=&quot;MM/dd/yy HH:mm Z&quot;" />
            <arg value="-output=${basedir}/target/google_maps.swc" />
        </java>
    </target>
    
    <target name="clean">
        <delete failonerror="false" includeEmptyDirs="true" >
            <fileset dir="${basedir}/target">
                <include name="**/**"/>
                <exclude name="downloads/**"/>
            </fileset>
        </delete>
    </target>
    
    <target name="wipe">
        <delete failonerror="false" includeEmptyDirs="true" >
            <fileset dir="${basedir}/target">
                <include name="**/**"/>
            </fileset>
        </delete>
    </target>

    <target name="fail-if-not-found">
        <fail message="${destDir}/${destFile} could not be downloaded or found in cache">
            <condition>
                <not>
                    <available file="${destDir}/${destFile}" />
                </not>
            </condition>
        </fail>
    </target>

    <target name="double-check-file" >
        <echo>${env.ROYALE_DOWNLOAD_CACHE}</echo>
        <condition property="still-no-file" value="true">
            <and>
                <not>
                    <available file="${destDir}/${destFile}" />
                </not>
                <isset property="env.ROYALE_DOWNLOAD_CACHE" />
            </and>
        </condition>
        <echo>Need file: ${still_no_file}</echo>
    </target>
    <target name="get-from-cache-if-needed" depends="double-check-file" if="still-no-file">
        <copy file="${env.ROYALE_DOWNLOAD_CACHE}/${srcFile}" tofile="${destDir}/${destFile}" />
    </target>
</project>
