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

<!-- Imported by contrib/*/build.xml files to share generic targets. -->

<project name="zookeeperbuildcontrib" xmlns:ivy="antlib:org.apache.ivy.ant">

  <property name="name" value="${ant.project.name}"/>
  <property name="version" value="dev"/>
  <property name="root" value="${basedir}"/>

  <property name="zk.root" location="${root}/../../"/>

  <property name="src.dir"  location="${root}/src/main/java"/>
  <property name="src.test" location="${root}/src/test"/>

  <property name="lib.dir"  location="${zk.root}/zookeeper-server/src/main/resources/lib"/>

  <property name="build.dir" location="${zk.root}/zookeeper-contrib/zookeeper-contrib-${name}/target"/>
  <property name="build.classes" location="${build.dir}/classes"/>
  <property name="build.test" location="${build.dir}/test"/>

  <property name="javac.deprecation" value="on"/>
  <property name="javac.debug" value="on"/>

  <property name="build.encoding" value="utf8"/>

  <property name="ivy.version" value="2.4.0"/>
  <property name="ivy.url"
            value="https://repo1.maven.org/maven2/org/apache/ivy/ivy" />
  <property name="ivy.home" value="${user.home}/.ant" />
  <property name="ivy.lib" value="${build.dir}/lib"/>
  <property name="ivy.test.lib" value="${build.test}/lib"/>
  <property name="ivysettings.xml" value="${zk.root}/zookeeper-contrib/ivysettings.xml"/>

  <!-- to be overridden by sub-projects -->
  <target name="check-contrib"/>
  <target name="init-contrib"/>

  <property name="lib.jars.includes" value="lib/*.jar" />
  <property name="lib.jars.excludes" value="" />

  <!-- prior to ant 1.7.1 fileset always fails if dir doesn't exist
       so just point to bin directory and provide settings that exclude
       everything - user can change as appropriate -->
  <property name="additional.lib.dir" value="${zk.root}/bin" />
  <property name="additional.lib.dir.includes" value="**/*.jar" />
  <property name="additional.lib.dir.excludes" value="**/*.jar" />

  <fileset id="lib.jars" dir="${root}">
    <include name="${lib.jars.includes}" />
    <exclude name="${lib.jars.excludes}" />
  </fileset>

  <path id="classpath">
    <pathelement location="${build.classes}"/>
    <!-- allow the user to override (e.g. if there are local versions) -->
    <fileset dir="${additional.lib.dir}">
      <include name="${additional.lib.dir.includes}" />
      <exclude name="${additional.lib.dir.excludes}" />
    </fileset>
    <fileset refid="lib.jars"/>
    <pathelement location="${zk.root}/build/classes"/>
    <fileset dir="${ivy.lib}">
      <include name="**/*.jar" />
    </fileset>
    <fileset dir="${ivy.test.lib}">
      <include name="**/*.jar" />
    </fileset>
    <fileset dir="${zk.root}/zookeeper-server/src/main/resources/lib">
      <include name="**/*.jar" />
    </fileset>
    <fileset dir="${ant.home}/lib">
      <include name="ant.jar" />
    </fileset>
  </path>

  <!-- ====================================================== -->
  <!-- Stuff needed by all targets                            -->
  <!-- ====================================================== -->
  <target name="init" depends="check-contrib" unless="skip.contrib">
    <echo message="contrib: ${name}"/>
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${build.classes}"/>
    <mkdir dir="${build.test}"/>

    <mkdir dir="${ivy.lib}"/>
    <mkdir dir="${ivy.test.lib}"/>
    <condition property="ivy.jar.exists">
      <available file="${lib.dir}/ivy-${ivy.version}.jar"/>
    </condition>

    <antcall target="init-contrib"/>
  </target>

  <!-- ====================================================== -->
  <!-- Compile a contrib's files                              -->
  <!-- ====================================================== -->
  <target name="compile" depends="init"
          unless="skip.contrib">
    <echo message="contrib: ${name}"/>

    <javac
     encoding="${build.encoding}"
     srcdir="${src.dir}"
     includes="**/*.java"
     destdir="${build.classes}"
     debug="${javac.debug}"
     deprecation="${javac.deprecation}">
      <classpath refid="classpath"/>
    </javac>
  </target>

  <!-- ====================================================== -->
  <!-- Make a contrib's jar                                   -->
  <!-- ====================================================== -->
  <target name="jar" depends="compile" unless="skip.contrib">
    <echo message="contrib: ${name}"/>
    <jar
      jarfile="${build.dir}/zookeeper-${version}-${name}.jar"
      basedir="${build.classes}"      
    />
  </target>

  <!-- ====================================================== -->
  <!-- Package a contrib's files                              -->
  <!-- ====================================================== -->
  <target name="package" depends="jar" unless="skip.contrib"> 
    <echo message="contrib: ${name}"/>

    <mkdir dir="${dist.dir}${package.share}/zookeeper-contrib/zookeeper-contrib-${name}"/>
    <copy todir="${dist.dir}${package.share}/zookeeper-contrib/zookeeper-contrib-${name}" includeEmptyDirs="false"
          flatten="true">
      <fileset dir="${build.dir}">
        <include name="zookeeper-${version}-${name}.jar" />
      </fileset>
    </copy>
  </target>

  <!-- ====================================================== -->
  <!-- Package a contrib's files                              -->
  <!-- ====================================================== -->
  <target name="package-native" depends="jar" unless="skip.contrib">
    <echo message="contrib: ${name}"/>
  </target>

  <!-- ================================================================== -->
  <!-- Clean.  Delete the build files, and their directories              -->
  <!-- ================================================================== -->
  <target name="clean">
    <echo message="contrib: ${name}"/>
    <delete dir="${build.dir}"/>
  </target>


  <!-- ================================================================== -->
  <!-- Utility features                                                   -->
  <!-- ================================================================== -->

  <target name="checkMainIsAvailable">
    <available classname="org.apache.zookeeper.ZooKeeperMain"
               property="mainIsCompiled">
      <!-- we can't use id=classpath, because available fails if fileset directory
           doesn't exist -->
      <classpath>
        <pathelement location="${zk.root}/zookeeper-server/target/classes"/>
      </classpath>
    </available>
  </target>

  <target name="checkMainCompiled" unless="mainIsCompiled" depends="checkMainIsAvailable">
    <fail message="ZooKeeper main must first be compiled (toplevel build.xml)"/>
  </target>


  <target name="checkMainTestIsAvailable">
    <available file="${zk.root}/zookeeper-server/target/test-classes/org/apache/zookeeper/test/ClientBase.class"
               property="mainTestIsCompiled">
    </available>
  </target>

  <target name="checkMainTestCompiled" unless="mainTestIsCompiled" depends="checkMainTestIsAvailable">
    <fail message="ZooKeeper test must first be compiled (toplevel build.xml) using 'ant compile-test'"/>
  </target>

  <!-- ====================================================== -->
  <!-- Ivy                                                    -->
  <!-- ====================================================== -->
  <target name="ivy-download" unless="ivy.jar.exists" depends="init">
    <delete dir="${lib.dir}"
            includes="ivy-*.jar" excludes="ivy-${ivy.version}.jar"/>
    <get src="${ivy.url}/${ivy.version}/ivy-${ivy.version}.jar"
         dest="${lib.dir}/ivy-${ivy.version}.jar" usetimestamp="true"/>
  </target>

  <target name="ivy-init" depends="ivy-download" unless="ivy.initialized">
    <taskdef resource="org/apache/ivy/ant/antlib.xml"
             uri="antlib:org.apache.ivy.ant" classpathref="classpath"/>
    <!-- ensure that ivy taskdef is only run once, otw ant will error -->
    <property name="ivy.initialized" value="true"/>
    <ivy:settings id="${ant.project.name}" file="${ivysettings.xml}"/> 
  </target>

  <target name="ivy-retrieve" depends="init,ivy-init">
    <ivy:retrieve settingsRef="${ant.project.name}" type="jar" conf="default"
                  pattern="${ivy.lib}/[artifact]-[revision].[ext]"/>
    <ivy:retrieve settingsRef="${ant.project.name}" type="bundle" conf="default"
  				  pattern="${ivy.lib}/[artifact]-[revision].[ext]"/>
  </target>

  <target name="ivy-retrieve-test" depends="init,ivy-init">
    <ivy:retrieve settingsRef="${ant.project.name}" type="jar" conf="test"
                  pattern="${ivy.test.lib}/[artifact]-[revision].[ext]"/>
  </target>


</project>
