<?xml version="1.0" encoding="UTF-8"?>
<!--
 ~ 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 default="jar" name="word_count" xmlns:ivy="antlib:org.apache.ivy.ant">
    <property name="cassandra.dir" value="../.." />
    <property name="cassandra.dir.lib" value="${cassandra.dir}/lib" />
    <property name="cassandra.classes" value="${cassandra.dir}/build/classes" />
    <property name="build.src" value="${basedir}/src" />
    <property name="build.dir" value="${basedir}/build" />
    <property name="ivy.lib.dir" value="${build.dir}/lib" />
    <property name="build.classes" value="${build.dir}/classes" />
    <property name="final.name" value="word_count" />
    <property name="ivy.version" value="2.1.0" />
    <property name="ivy.url"
              value="http://repo2.maven.org/maven2/org/apache/ivy/ivy" />

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

    <path id="autoivy.classpath">
        <fileset dir="${ivy.lib.dir}">
            <include name="**/*.jar" />
        </fileset>
        <pathelement location="${build.dir}/ivy-${ivy.version}.jar"/>
    </path>

    <path id="wordcount.build.classpath">
        <fileset dir="${ivy.lib.dir}">
            <include name="**/*.jar" />
        </fileset>
        <!-- cassandra dependencies -->
        <fileset dir="${cassandra.dir.lib}">
            <include name="**/*.jar" />
        </fileset>
        <fileset dir="${cassandra.dir}/build/lib/jars">
            <include name="**/*.jar" />
        </fileset>
        <pathelement location="${cassandra.classes}/main" />
        <pathelement location="${cassandra.classes}/thrift" />
    </path>

    <target name="init">
        <mkdir dir="${build.classes}" />
    </target>

    <target depends="init,ivy-retrieve-build" name="build">
        <javac destdir="${build.classes}">
            <src path="${build.src}" />
            <classpath refid="wordcount.build.classpath" />
        </javac>
    </target>

    <target name="jar" depends="build">
        <mkdir dir="${build.classes}/META-INF" />
        <jar jarfile="${build.dir}/${final.name}.jar">
           <fileset dir="${build.classes}" />
           <fileset dir="${cassandra.classes}/main" />
           <fileset dir="${cassandra.classes}/thrift" />
           <fileset dir="${cassandra.dir}">
               <include name="lib/**/*.jar" />
           </fileset>
           <zipfileset dir="${cassandra.dir}/build/lib/jars/" prefix="lib">
               <include name="**/*.jar" />
           </zipfileset>
           <fileset file="${basedir}/cassandra.yaml" />
        </jar>
    </target>

    <target name="clean">
        <delete dir="${build.dir}" />
    </target>

    <!--
        Ivy Specific targets
            to fetch Ivy and this project's dependencies
    -->
	<target name="ivy-download" unless="ivy.jar.exists">
      <echo>Downloading Ivy...</echo>
      <mkdir dir="${build.dir}" />
      <get src="${ivy.url}/${ivy.version}/ivy-${ivy.version}.jar"
           dest="${build.dir}/ivy-${ivy.version}.jar" usetimestamp="true" />
    </target>

    <target name="ivy-init" depends="ivy-download" unless="ivy.initialized">
      <mkdir dir="${ivy.lib.dir}"/>
      <taskdef resource="org/apache/ivy/ant/antlib.xml"
               uri="antlib:org.apache.ivy.ant"
               classpathref="autoivy.classpath"/>
      <property name="ivy.initialized" value="true"/>
    </target>

    <target name="ivy-retrieve-build" depends="ivy-init">
      <ivy:retrieve type="jar,source" sync="true"
             pattern="${ivy.lib.dir}/[type]s/[artifact]-[revision].[ext]" />
    </target>
</project>
