<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
  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 basedir="." name="apache-cassandra-resolver-tasks"
         xmlns:resolver="antlib:org.apache.maven.resolver.ant"
         xmlns:if="ant:if"
         xmlns:unless="ant:unless">

    <!-- details of what version of Resolver ANT Tasks to fetch -->
    <property name="resolver-ant-tasks.version" value="1.3.0" />
    <property name="resolver-ant-tasks.local" value="${local.repository}/org/apache/maven/resolver/maven-resolver-ant-tasks/${resolver-ant-tasks.version}/maven-resolver-ant-tasks-${resolver-ant-tasks.version}.jar"/>
    <property name="resolver-ant-tasks.url" value="https://repo1.maven.org/maven2/org/apache/maven/resolver/maven-resolver-ant-tasks" />
    <condition property="resolver-ant-tasks.jar.exists">
        <available file="${resolver-ant-tasks.local}" />
    </condition>

    <property name="artifact.python.pypi" value="https://files.pythonhosted.org/packages" />
    <property name="artifact.github.release" value="https://github.com" />

    <!-- some artifacts are fetched from github as blobs; these are all in the cassandra project, but in an older commit -->
    <property name="lib.download.sha" value="1371883db3d8bf7d7c54e0baaca89c6c2d2a5abe"/>
    <property name="lib.download.base.url" value="https://raw.githubusercontent.com/apache/cassandra/${lib.download.sha}" />
    <property name="lib.download.url.postfix" value="" />

    <path id="resolver-ant-tasks.classpath" path="${resolver-ant-tasks.local}" />

    <!--
       Fetch Resolver Ant Tasks and Cassandra's dependencies.
    -->

    <target name="_resolver_download" unless="resolver-ant-tasks.jar.exists" description="Fetch Resolver ANT Tasks from Maven Central Repository">
        <echo>Downloading Resolver ANT Tasks...</echo>
        <mkdir dir="${local.repository}/org/apache/maven/resolver/maven-resolver-ant-tasks/${resolver-ant-tasks.version}" />

        <retry retrycount="3" retrydelay="10" >
            <get src="${resolver-ant-tasks.url}/${resolver-ant-tasks.version}/maven-resolver-ant-tasks-${resolver-ant-tasks.version}-uber.jar"
                 dest="${resolver-ant-tasks.local}" usetimestamp="true" quiet="true"/>
        </retry>
    </target>

    <target name="resolver-init" depends="init,_resolver_download" unless="resolver-ant-tasks.initialized" description="Initialize Resolver ANT Tasks">

        <typedef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml" classpathref="resolver-ant-tasks.classpath" />
        <resolver:remoterepos id="all">
            <remoterepo id="resolver-central" url="${artifact.remoteRepository.central}"/>
            <remoterepo id="resolver-apache" url="${artifact.remoteRepository.apache}"/>
            <!-- Snapshot artifacts must not exist in nor be downloaded by any Cassandra release artifact.
                 Please validate that all artifacts included in parent-pom-template.xml are release
                 artifacts before committing.

                 To build with a snapshot dependency, ensure build.properties or build.properties.default
                 contain the correct resolution for "artifact.remoteRepository.apacheSnapshot" and uncomment
                 the following line.

                 You may also need to add an exclusion for "resolver-apache-snapshot" to your mirrors in ~/.m2/settings.xml
                 or ~/.ant/settings.xml (maven ant resolver supersedes) as mirrors will, by default, catch and override
                 all dependency resolution regardless of it being a -SNAPSHOT lib or not.
             -->
            <!-- <remoterepo id="resolver-apache-snapshot" url="${artifact.remoteRepository.apacheSnapshot}" releases="false" snapshots="true" updates="always" checksums="fail" /> -->
        </resolver:remoterepos>

        <macrodef name="resolve">
            <!--
              maven-resolver-ant-tasks's resolve logic doesn't have retry logic and does not respect settings.xml,
              this causes issues when overriding maven central is required (such as when behind a corporate firewall);
              it is critical to always provide the 'all' remoterepos to override resolve's default hard coded logic.

              This macro simplifies this logic by centralizing retries and forcing the remoterepo 'all' to be used.
            -->
            <attribute name="failonmissingattachments" default="true"/>
            <element name="elements" implicit="yes"/>
            <sequential>
                <retry retrycount="3">
                    <resolver:resolve failonmissingattachments="@{failonmissingattachments}">
                        <resolver:remoterepos refid="all"/>
                        <elements/>
                    </resolver:resolve>
                </retry>
            </sequential>
        </macrodef>
        <macrodef name="resolvepom">
            <attribute name="file"/>
            <attribute name="id"/>
            <element name="elements" implicit="yes" optional="yes"/>
            <sequential>
                <retry retrycount="3">
                    <resolver:pom file="@{file}" id="@{id}">
                        <remoterepos refid="all"/>
                        <elements/>
                    </resolver:pom>
                </retry>
            </sequential>
        </macrodef>

        <resolve>
            <dependencies>
                <dependency groupId="com.datastax.wikitext" artifactId="wikitext-core-ant" version="1.3"/>
                <dependency groupId="org.fusesource.wikitext" artifactId="textile-core" version="1.3"/>
            </dependencies>
            <path refid="wikitext.classpath" classpath="runtime"/>
        </resolve>
        <resolve>
            <dependencies>
                <dependency groupId="org.apache.rat" artifactId="apache-rat-tasks" version="0.6" />
            </dependencies>
            <path refid="rat.classpath" classpath="runtime"/>
        </resolve>
        <resolve>
            <dependencies>
                <dependency groupId="org.jacoco" artifactId="org.jacoco.ant" version="${jacoco.version}" />
            </dependencies>
            <path refid="jacocoant.classpath" classpath="runtime"/>
        </resolve>
        <resolve>
            <dependencies>
                <dependency groupId="org.antlr" artifactId="antlr" version="3.5.2"/>
                <dependency groupId="org.antlr" artifactId="antlr-runtime" version="3.5.2"/>
                <dependency groupId="org.antlr" artifactId="ST4" version="4.0.8"/>
            </dependencies>
            <path refid="cql3-grammar.classpath" classpath="runtime"/>
        </resolve>
        <resolve>
            <dependencies>
                <dependency groupId="de.jflex" artifactId="jflex" version="${jflex.version}" />
            </dependencies>
            <path refid="jflex.classpath" classpath="runtime"/>
        </resolve>

        <macrodef name="install">
            <attribute name="pomFile"/>
            <attribute name="file"/>
            <attribute name="classifier" default=""/>
            <attribute name="packaging" default="jar"/>
            <sequential>
                <exec dir="." executable="sh">
                    <arg line="-c 'mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file -DpomFile=@{pomFile} -Dfile=@{file} -Dclassifier=@{classifier} -Dpackaging=@{packaging}'" />
                </exec>
            </sequential>
        </macrodef>

        <macrodef name="deploy">
            <attribute name="pomFile"/>
            <attribute name="file"/>
            <attribute name="classifier" default=""/>
            <attribute name="packaging" default="jar"/>
            <sequential>
                <exec dir="." executable="sh">
                    <arg line="-c 'mvn org.apache.maven.plugins:maven-gpg-plugin:1.6:sign-and-deploy-file -DretryFailedDeploymentCount=5 -Durl=${maven-repository-url} -DrepositoryId=${maven-repository-id} -DpomFile=@{pomFile} -Dfile=@{file} -Dclassifier=@{classifier} -Dpackaging=@{packaging}'" />
                </exec>
            </sequential>
        </macrodef>

        <macrodef name="sign-dist">
            <attribute name="file"/>
            <sequential>
                <echo message="gpg signing @{file}" />
                <exec dir="." executable="sh">
                    <!-- dummy out the deploy parameters, only the signing is required here -->
                    <arg line="-c 'mvn -q org.apache.maven.plugins:maven-gpg-plugin:1.6:sign-and-deploy-file -Dfile=@{file} -DgroupId=org.apache.cassandra -DartifactId=cassandra-parent -Dversion=${version} -Durl=file:///tmp/ -DrepositoryId=tmp'" />
                </exec>
            </sequential>
        </macrodef>

        <property name="resolver-ant-tasks.initialized" value="true"/>
    </target>

    <target name="resolver-retrieve-build" depends="resolver-init,write-poms">
        <resolvepom file="${build.dir}/${final.name}.pom" id="all-pom" />
        <resolvepom file="${build.dir}/tmp-${final.name}-deps.pom" id="pom-deps" />

        <retry retrycount="3" retrydelay="10" >
            <resolve>
                <dependencies pomRef="all-pom"/>
                <files dir="${build.dir.lib}/jars" layout="{artifactId}-{version}-{classifier}.{extension}" scopes="compile,provided,!system"/>
            </resolve>
        </retry>
        <retry retrycount="3" retrydelay="10" >
            <resolve>
                <dependencies pomRef="pom-deps"/>
                <!-- Needed to include compile here, so ant _build-test would not fail on missing jimfs dependency -->
                <files dir="${test.lib}/jars" layout="{artifactId}-{version}-{classifier}.{extension}" scopes="compile,test,!provide,!system"/>
            </resolve>
        </retry>


        <!-- jacoco agent jar comes wrapped in a jar -->
        <unzip src="${local.repository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}.jar" dest="${build.dir.lib}/jars">
            <patternset>
                <include name="*.jar"/>
            </patternset>
            <mapper type="flatten"/>
        </unzip>
    </target>

    <target name="resolver-dist-lib" depends="resolver-retrieve-build">
        <resolvepom file="${build.dir}/${final.name}.pom" id="all-pom" />

        <retry retrycount="3" retrydelay="10" >
            <resolve failOnMissingAttachments="true">
                <dependencies pomRef="all-pom"/>
                <files dir="${build.lib}" layout="{artifactId}-{version}-{classifier}.{extension}" scopes="compile,!provide,!system"/>
            </resolve>
        </retry>
        <mkdir dir="${local.repository}/org/apache/cassandra/deps/sigar-bin"/>
        <mkdir dir="${build.lib}/sigar-bin"/>
        <mkdir dir="${build.lib}/x86_64"/>
        <mkdir dir="${build.lib}/aarch64"/> <!-- uname -m on arm prints aarch64 instead of aarch_64 -->

        <!-- artifacts needs AmazonCorrettoCryptoProvider for multiple archs -->
        <retry retrycount="3" retrydelay="10" >
            <resolve>
                <dependencies>
                    <dependency groupId="software.amazon.cryptools" artifactId="AmazonCorrettoCryptoProvider" version="2.2.0" classifier="linux-x86_64" />
                </dependencies>
                <files dir="${build.lib}/x86_64" layout="{artifactId}-{version}-{classifier}.{extension}" />
            </resolve>
        </retry>
        <retry retrycount="3" retrydelay="10" >
            <resolve>
                <dependencies>
                    <dependency groupId="software.amazon.cryptools" artifactId="AmazonCorrettoCryptoProvider" version="2.2.0" classifier="linux-aarch_64" />
                </dependencies>
                <files dir="${build.lib}/aarch64" layout="{artifactId}-{version}-{classifier}.{extension}" />
            </resolve>
        </retry>

        <retry retrycount="3" retrydelay="10" >
            <antcall target="_resolver-dist-lib_get_files"/>
        </retry>

        <copy todir="${build.lib}" quiet="true">
            <file file="${local.repository}/org/apache/cassandra/deps/futures-2.1.6-py2.py3-none-any.zip"/>
            <file file="${local.repository}/org/apache/cassandra/deps/geomet-0.1.0.zip"/>
            <file file="${local.repository}/org/apache/cassandra/deps/pure_sasl-0.6.2-py2-none-any.zip"/>
            <file file="${local.repository}/org/apache/cassandra/deps/wcwidth-0.2.5-py2.py3-none-any.zip"/>
        </copy>
        <copy todir="${build.lib}/sigar-bin/" quiet="true">
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-amd64-freebsd-6.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-amd64-linux.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-amd64-solaris.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-ia64-hpux-11.sl"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-ia64-linux.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-pa-hpux-11.sl"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-ppc-aix-5.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-ppc-linux.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-ppc64-aix-5.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-ppc64-linux.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-ppc64le-linux.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-s390x-linux.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-sparc-solaris.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-sparc64-solaris.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-universal-macosx.dylib"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-universal64-macosx.dylib"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-x86-freebsd-5.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-x86-freebsd-6.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-x86-linux.so"/>
            <file file="${local.repository}/org/apache/cassandra/deps/sigar-bin/libsigar-x86-solaris.so"/>
        </copy>

        <!-- as resolver will copy all dependencies into lib dir, and we are copying jars to lib/{x86_64|aarch64} as well, we would have duplicities -->
        <delete file="${build.lib}/AmazonCorrettoCryptoProvider-2.2.0-linux-x86_64.jar" failonerror="false"/>
        <delete file="${build.lib}/AmazonCorrettoCryptoProvider-2.2.0-linux-aarch_64.jar" failonerror="false"/>

        <delete file="${build.lib}/netty-tcnative-boringssl-static-2.0.70.Final-windows-x86_64.jar" failonerror="false"/>
        <delete file="${build.dir.lib}/jars/netty-tcnative-boringssl-static-2.0.70.Final-windows-x86_64.jar" failonerror="false"/>
    </target>

    <target name="_resolver-dist-lib_get_files">
        <!-- files.pythonhosted.org/packages -->
        <get src="${artifact.python.pypi}/59/a0/cf4cd997e1750f0c2d91c6ea5abea218251c43c3581bcc2f118b00baf5cf/futures-2.1.6-py2.py3-none-any.whl" dest="${local.repository}/org/apache/cassandra/deps/futures-2.1.6-py2.py3-none-any.zip" usetimestamp="true" quiet="true" skipexisting="true"/>
        <get src="${artifact.python.pypi}/37/b2/ef1124540ee2c0b417be8d0f74667957e6aa084a3f26621aa67e2e77f3fb/pure_sasl-0.6.2-py2-none-any.whl" dest="${local.repository}/org/apache/cassandra/deps/pure_sasl-0.6.2-py2-none-any.zip" usetimestamp="true" quiet="true" skipexisting="true"/>
        <get src="${artifact.python.pypi}/59/7c/e39aca596badaf1b78e8f547c807b04dae603a433d3e7a7e04d67f2ef3e5/wcwidth-0.2.5-py2.py3-none-any.whl" dest="${local.repository}/org/apache/cassandra/deps/wcwidth-0.2.5-py2.py3-none-any.zip" usetimestamp="true" quiet="true" skipexisting="true"/>

        <!-- apache/cassandra/lib -->
        <get src="${lib.download.base.url}/lib/geomet-0.1.0.zip" dest="${local.repository}/org/apache/cassandra/deps/geomet-0.1.0.zip" usetimestamp="true" quiet="true" skipexisting="true"/>
        <get dest="${local.repository}/org/apache/cassandra/deps/sigar-bin/" quiet="true" usetimestamp="true" skipexisting="true">
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-amd64-freebsd-6.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-amd64-linux.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-amd64-solaris.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-ia64-hpux-11.sl"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-ia64-linux.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-pa-hpux-11.sl"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-ppc-aix-5.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-ppc-linux.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-ppc64-aix-5.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-ppc64-linux.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-ppc64le-linux.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-s390x-linux.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-sparc-solaris.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-sparc64-solaris.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-universal-macosx.dylib"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-universal64-macosx.dylib"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-x86-freebsd-5.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-x86-freebsd-6.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-x86-linux.so"/>
            <url url="${lib.download.base.url}/lib/sigar-bin/libsigar-x86-solaris.so"/>
        </get>
    </target>

</project>
