<?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="downloads" default="main" basedir=".">
    <echo>basedir is ${basedir}</echo>
    <echo>ROYALE_COMPILER_HOME is ${ROYALE_COMPILER_HOME}</echo>

  <!-- 
      Notes:
          For Apache, the JARS must be removed from the repository.

          Licenses:
              commons-io (2.11.0) - Apache 2.0
              closure (9.2) - Apache 2.0
  -->

  <!-- this script supports the usingDownloadCache property and
       downloadCacheFolder property to cache downloads in the
       folder specified by downloadCacheFolder.  This can make
       a huge difference in future runs although there is some
       risk around caching bad downloads and/or needing to
       clean up the cache -->

  <property name="ROYALE_COMPILER_HOME" location="${basedir}/../../.."/>
  <echo>ROYALE_COMPILER_HOME is ${ROYALE_COMPILER_HOME}</echo>

  <property name="lib.dir" value="${ROYALE_COMPILER_HOME}/lib"/>
  <property name="download.dir" value="${ROYALE_COMPILER_HOME}/in"/>

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

  <property name="maven.search.url" value="https://repo1.maven.org/maven2"/>

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

  <!-- 
      Because the downloads requires a network connection and the JARs don't change very often, 
      they are each downloaded only if they don't already exist. 
  -->
  <target name="main" depends="prepare, all" description="Downloads all the required thirdparty JARs"/>
  
  <target name="prepare">
    <echo message="Making lib directory ${lib.dir}"/>
    <mkdir dir="${lib.dir}"/>
  </target>



  <!--
      Downloads
  -->

  <target name="all" description="Downloads and copies all dependencies to the lib directory.">
    <!--  args4j -->
    <property name="args4j.name" value="args4j"/>
    <property name="args4j.version" value="2.0.28"/>
    <!-- property name="args4j.version" value="2.0.16"/ -->
    <antcall target="download-dependency">
      <param name="name" value="${args4j.name}"/>
      <param name="src.server" value="${maven.search.url}"/>
      <param name="src.folder" value="args4j/args4j/${args4j.version}"/>
      <param name="src.filename" value="args4j-${args4j.version}.jar"/>
      <param name="src.checksum" value="e0593b60af743a63f9678399d275272a"/>
      <!-- param name="src.checksum" value="6571d69d142dd2a003c4ffae6138f0ee"/ --><!-- version 2.0.16 -->
      <param name="dest.folder" value=""/>
      <param name="dest.filename" value="${args4j.name}.jar"/>
    </antcall>
    <!-- the download-dependency copies ALv2, but args4j seems to be under MIT so overwrite it -->
    <get src="https://raw.githubusercontent.com/kohsuke/args4j/master/LICENSE" dest="${lib.dir}/args4j-LICENSE.txt" ignoreerrors="true"/>

    <!--  closure -->
    <property name="closure.version" value="20181210"/>
    <property name="closure.name" value="closure-compiler-v${closure.version}"/>
    <property name="closure.dest.name" value="compiler"/>
    <property name="closure.dest.folder" value="google/closure-compiler"/>
    <property name="closure.dest.filename" value="${closure.dest.name}.jar"/>
    <antcall target="download-dependency-closure">
      <param name="name" value="${closure.name}"/>
      <param name="src.server" value="https://dl.google.com"/>
      <param name="src.folder" value="closure-compiler"/>
      <param name="src.filename" value="compiler-${closure.version}.zip"/>
      <param name="src.checksum" value="c67fdbf6512d1de6bbb84a69ed3d5b87"/>
    </antcall>
    
    <!--  commons-io -->
    <property name="commons-io.name" value="commons-io"/>
    <property name="commons-io.version" value="2.11.0"/>
    <antcall target="download-dependency">
        <param name="name" value="${commons-io.name}"/>
        <param name="src.server" value="${maven.search.url}"/>
        <param name="src.folder" value="commons-io/commons-io/${commons-io.version}"/>
        <param name="src.filename" value="${commons-io.name}-${commons-io.version}.jar"/>
        <param name="src.checksum" value="3b4b7ccfaeceeac240b804839ee1a1ca"/>
        <param name="dest.folder" value=""/>
        <param name="dest.filename" value="${commons-io.name}.jar"/>
    </antcall>

    <!--  guava -->
    <property name="guava.name" value="guava"/>
    <property name="guava.version" value="32.0.0-jre"/>
    <antcall target="download-dependency">
      <param name="name" value="${guava.name}"/>
      <param name="src.server" value="${maven.search.url}"/>
      <param name="src.folder" value="com/google/guava/guava/${guava.version}"/>
      <param name="src.filename" value="guava-${guava.version}.jar"/>
      <param name="src.checksum" value="677b5dfdedfbdbc13bc00478878de7aa"/>
      <param name="dest.folder" value=""/>
      <param name="dest.filename" value="${guava.name}.jar"/>
    </antcall>

    <property name="failureaccess.name" value="failureaccess"/>
    <property name="failureaccess.version" value="1.0.2"/>
    <antcall target="download-dependency">
      <param name="name" value="${failureaccess.name}"/>
      <param name="src.server" value="${maven.search.url}"/>
      <param name="src.folder" value="com/google/guava/failureaccess/${failureaccess.version}"/>
      <param name="src.filename" value="failureaccess-${failureaccess.version}.jar"/>
      <param name="src.checksum" value="3f75955b49b6758fd6d1e1bd9bf777b3"/>
      <param name="dest.folder" value=""/>
      <param name="dest.filename" value="${failureaccess.name}.jar"/>
      <param name="license.use.apache" value="true"/>
    </antcall>

    <!--  flex-tool-api -->
    <property name="flexToolApi.name" value="flex-tool-api"/>
    <property name="flexToolApi.version" value="1.0.0"/>
    <antcall target="download-dependency">
      <param name="name" value="${flexToolApi.name}"/>
      <param name="src.server" value="${maven.search.url}"/>
      <param name="src.folder" value="org/apache/flex/${flexToolApi.name}/${flexToolApi.version}"/>
      <param name="src.filename" value="${flexToolApi.name}-${flexToolApi.version}.jar"/>
      <param name="src.checksum" value="b8f76174b2c4d3b83e5c9fa78703c52b"/>
      <param name="dest.folder" value=""/>
      <param name="dest.filename" value="${flexToolApi.name}.jar"/>
      <param name="license.use.apache" value="true"/>
    </antcall>
  </target>



  <!--
      Utilities
  -->

  <target name="check-dependency" description="Checks if project jar is in lib directory.">
    <available file="${lib.dir}/${dest.folder}/${name}.jar" property="project.jar.exists"/>
  </target>

    <target name="check-dependency-closure" description="Checks if project jar is in lib directory.">
        <available file="${lib.dir}/${closure.dest.folder}/${closure.dest.name}.jar" property="project.jar.exists"/>
    </target>

  <target name="echo-project-jar" if="installer">
    <echo file="${basedir}/project.properties">project.echo=${INFO_DOWNLOADING_FILE_FROM}</echo>
    <replace file="${basedir}/project.properties" token="{0}" value="${srcPath}"/>
    <replace file="${basedir}/project.properties" token="{1}" value="${srcDomain}"/>
    <property file="${basedir}/project.properties"/>
    <delete file="${basedir}/project.properties"/>
    <echo>${project.echo}</echo>
  </target>
  
  <target name="download-dependency" depends="check-dependency" unless="project.jar.exists" description="Downloads a jar + license to the lib directory.">
    <antcall target="echo-project-jar">
      <param name="srcDomain" value="${src.server}"/>
      <param name="srcPath" value="${src.folder}/${src.filename}"/>
    </antcall>
    <mkdir dir="${lib.dir}/${dest.folder}"/>

    <get src="http://www.apache.org/licenses/LICENSE-2.0" dest="${lib.dir}/${dest.folder}/${name}-LICENSE.txt" ignoreerrors="true"/>
    <antcall target="get-from-cache-if-needed" >
        <param name="srcFile" value="LICENSE-2.0.html" />
        <param name="destFile" value="${name}-LICENSE.txt" />
        <param name="destDir" value="${lib.dir}/${dest.folder}" />
    </antcall>
    <antcall target="fail-if-not-found" >
        <param name="destFile" value="${name}-LICENSE.txt" />
        <param name="destDir" value="${lib.dir}/${dest.folder}" />
    </antcall>

    <condition property="project.download.jar">
      <contains string="${src.filename}" substring=".jar" />
    </condition>
    
    <antcall target="download-dependency-jar">
      <param name="server" value="${src.server}"/>
      <param name="folder" value="${src.folder}"/>
      <param name="srcFileName" value="${src.filename}"/>
      <param name="destDir" value="${lib.dir}/${dest.folder}"/>
      <param name="destFile" value="${dest.filename}"/>
      <param name="checksum" value="${src.checksum}"/>
    </antcall>

    <antcall target="download-dependency-zip">
      <param name="server" value="${src.server}"/>
      <param name="folder" value="${src.folder}"/>
      <param name="srcFileName" value="${src.filename}"/>
      <param name="srcJarPath" value="${src.jarPath}"/>
      <param name="destDir" value="${lib.dir}/${dest.folder}"/>
      <param name="destFile" value="${dest.filename}"/>
      <param name="checksum" value="${src.checksum}"/>
    </antcall>
  </target>

  <target name="download-dependency-closure" depends="check-dependency-closure" unless="project.jar.exists" description="Downloads a jar to the lib directory.">
    <antcall target="echo-project-jar">
      <param name="srcDomain" value="${src.server}"/>
      <param name="srcPath" value="${src.folder}/${src.filename}"/>
    </antcall>
    <mkdir dir="${lib.dir}/${closure.dest.folder}"/>

    <antcall target="download-dependency-zip-closure">
      <param name="server" value="${src.server}"/>
      <param name="folder" value="${src.folder}"/>
      <param name="srcFileName" value="${src.filename}"/>
      <param name="checksum" value="${src.checksum}"/>
    </antcall>

    <copy file="${download.dir}/temp/${closure.name}.jar" toFile="${lib.dir}/${closure.dest.folder}/${closure.dest.filename}" verbose="true"/>
    <copy file="${download.dir}/temp/README.md" toFile="${lib.dir}/${closure.dest.folder}/README.md" verbose="true"/>

  </target>
  
  <target name="download-dependency-jar" if="project.download.jar" description="Downloads a jar to the lib directory.">
    <antcall target="download-jar">
      <param name="srcDomain" value="${server}"/>
      <param name="srcFolder" value="${folder}"/>
      <param name="cacheFolder" value="${folder}"/>
      <param name="srcFile" value="${srcFileName}"/>
      <param name="md5" value="${checksum}"/>
    </antcall>
  </target>

  <target name="download-dependency-zip" unless="project.download.jar" description="Downloads a zip to the lib directory.">
    <antcall target="download-zip">
      <param name="srcDomain" value="${server}"/>
      <param name="srcFolder" value="${folder}"/>
      <param name="srcFile" value="${srcFileName}"/>
      <param name="srcJarPath" value="${srcJarPath}"/>
      <param name="md5" value="${checksum}"/>
    </antcall>
  </target>

  <target name="download-dependency-zip-closure" unless="project.download.jar" description="Downloads a zip to the lib directory.">
    <antcall target="download-zip">
      <param name="srcDomain" value="${server}"/>
      <param name="srcFolder" value="${folder}"/>
      <param name="srcFile" value="${srcFileName}"/>
      <param name="md5" value="${checksum}"/>
    </antcall>
  </target>
    
  <!--
      Download a zip or gz file, extracts the jar file, and optionally copies the jar
      file to a different location and optinally verifies the checksum and optionally
      caches the file, and optionally pulls the file from the cache instead of downloading.  
      If the checksum fails, this script fails.

      Params are:
          srcDomain - the domain
          srcFolder - path to file
          srcFile - a .gz file for untar with gzip, else unzip
          [md5]
          [srcJarPath] - both src and dest required for the copy
          [destDir]
          [destFile]

      Note: This is purposely coded without <if><else><then> so that a dependency on
      ant-contrib.jar isn't required.        
  -->
  <target name="download-zip" depends="check-cache" description="Downloads tar/zip, and optionally verifies checksum and copies extracted jar.">
    <mkdir dir="${download.dir}"/>
    <antcall target="get-if-not-cached">
      <param name="destDir" value="${download.dir}"/>
      <param name="destFile" value="${srcFile}"/>
      <param name="message" value="Checksum mismatch for ${download.dir}/${srcFile}"/>
    </antcall>
    <antcall target="copy-if-cached">
        <param name="destDir" value="${download.dir}"/>
        <param name="destFile" value="${srcFile}"/>
    </antcall>
    <condition property="zip.compressed">
      <matches string="${srcFile}" pattern="^*.zip$"/>
    </condition>
    <antcall target="untar-file"/>
    <antcall target="unzip-file"/>
    <condition property="destination.known">
      <and>
        <isset property="srcJarPath"/>
        <isset property="destDir"/>
        <isset property="destFile"/>
      </and>
    </condition>
    <antcall target="copy-downloaded-jar"/>
  </target>

  <target name="download-bz2" depends="check-cache" description="Downloads bz2, and optionally verifies checksum and copies extracted jar.">
    <mkdir dir="${download.dir}"/>
    <antcall target="get-if-not-cached">
        <param name="destDir" value="${download.dir}"/>
        <param name="destFile" value="${srcFile}"/>
      <param name="message" value="Checksum mismatch for ${download.dir}/${srcFile}"/>
    </antcall>
    <antcall target="copy-if-cached">
        <param name="destDir" value="${download.dir}"/>
        <param name="destFile" value="${srcFile}"/>
    </antcall>
    <untar src="${download.dir}/${srcFile}" dest="${download.dir}/temp" compression="bzip2"/>
    <condition property="destination.known">
      <and>
        <isset property="srcJarPath"/>
        <isset property="destDir"/>
        <isset property="destFile"/>
      </and>
    </condition>
    <antcall target="copy-downloaded-jar"/>
  </target>

  <!--
      Download a jar file and optionally verify the checksum.
      If the checksum fails, this script fails.
        
      Params are:
          srcDomain
          srcFolder
          srcFile
          destDir
          destFile
          [md5]
  -->
  <target name="download-jar" depends="check-cache" description="Downloads jar, and optionally verifies checksum.">
    <antcall target="get-if-not-cached">
      <param name="message" value="Checksum mismatch for ${destDir}/${destFile}"/>
    </antcall>
    <antcall target="copy-if-cached"/>
  </target>

  <target name="untar-file" unless="zip.compressed" description="Untars zipFile">
    <untar src="${download.dir}/${srcFile}" dest="${download.dir}/temp" compression="gzip"/>
  </target>

  <target name="unzip-file" if="zip.compressed" description="Unzips zipFile">
    <unzip src="${download.dir}/${srcFile}" dest="${download.dir}/temp"/>
  </target>

  <target name="get-if-not-cached" unless="found-in-cache">
    <get src="${srcDomain}/${srcFolder}/${srcFile}" dest="${destDir}/${destFile}" ignoreerrors="true"/>
    <!-- this is a different cache for the CI servers.  It tries the network first, then the cache if failure -->
    <antcall target="get-from-cache-if-needed" />
    <antcall target="fail-if-not-found" />
    <antcall target="check-sum">
      <param name="message" value="Checksum mismatch for ${destDir}/${destFile}"/>
    </antcall>
    <antcall target="put-in-cache"/>
  </target>

  <target name="copy-if-cached" if="found-in-cache">
    <!-- this string comes from the Royale en_US.properties because for now, this
         target won't get called unless this script is called from the Royale install -->
    <echo>${INFO_USING_CACHED_FILE} ${downloadCacheFolder}/${srcFolder}/${srcFile}</echo>
    <copy file="${downloadCacheFolder}/${srcFolder}/${srcFile}" tofile="${destDir}/${destFile}" overwrite="true"/>
  </target>

  <target name="check-cache" if="usingDownloadCache">
    <available file="${downloadCacheFolder}/${srcFolder}/${srcFile}" property="found-in-cache"/>
  </target>

  <target name="put-in-cache" if="usingDownloadCache">
    <copy tofile="${downloadCacheFolder}/${srcFolder}/${srcFile}" file="${destDir}/${destFile}"/>
  </target>

  <target name="check-sum" if="md5" description="Verifies MD5 checksum, and fails if checksum doesn't match">
    <local name="checksum.result"/>
    <checksum file="${destDir}/${destFile}" algorithm="MD5" property="checksum.result"/>
    <fail message="${message} Expected: ${md5} Received: ${checksum.result}">
      <condition>
        <not>
          <equals arg1="${md5}" arg2="${checksum.result}"/>
        </not>
      </condition>
    </fail>
  </target>

  <target name="copy-downloaded-jar" if="destination.known">
    <mkdir dir="${lib.dir}"/>
    <copy file="${download.dir}/temp/${srcJarPath}" toFile="${destDir}/${destFile}" verbose="true"/>
  </target>

  <target name="fail-with-message" if="we.failed" description="Conditionally fails with the specified message">
    <fail message="${message}"/>
  </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" >
        <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>


  <!--
      Cleanup
  -->
  <target name="clean" description="Removes thirdparty downloads.">
    <delete failonerror="false" includeEmptyDirs="true">
      <fileset dir="${download.dir}"/>
    </delete>
  </target>
    
</project>
