<?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 name="apache-alike" xmlns:ivy="antlib:org.apache.ivy.ant"
         default="alike-dist" basedir=".">

    <dirname property="mybase.dir" file="${ant.file.apache-alike}"/>
    <property file="${mybase.dir}/build.properties"/>

    <path id="common.path.lib">
        <fileset dir="${lib.dir}" includes="*.jar" excludes="lucene-*-4.9..0.jar"/>
    </path>

    <!-- ================================================================== -->
    <!-- = BUILD ALIKE                                                    = -->
    <!-- ================================================================== -->
    <target name="resolve" description="retrieve dependencies with ivy">
      <ivy:retrieve/>
    </target>

    <target name="alike-compile" depends="resolve" description="compile alike">
        <mkdir dir="${cls.dir}"/>
        <javac srcdir="${src.dir}/java"
               destdir="${cls.dir}"
               encoding="${compile.encoding}"
               deprecation="${compile.deprecation}"
               target="${compile.target}"
               debug="${compile.debug}">
            <classpath refid="common.path.lib"/>
        </javac>
    </target>

    <target name="alike-javadoc" description="build alike javadoc">
        <property name="javadoc.link.java" value="http://docs.oracle.com/javase/jp/6/api/"/>
        <property name="javadoc.link.mahout" value="https://builds.apache.org/job/Mahout-Quality/javadoc/"/>
        <property name="javadoc.link.lucene.core" value="http://lucene.apache.org/core/4_9_0/core/"/>
        <delete dir="${javadoc.dir}"/>
        <mkdir dir="${javadoc.dir}"/>
        <copy todir="${javadoc.dir}/prettify">
          <fileset dir="${prettify.dir}"/>
        </copy>
        <javadoc
          overview="src/java/overview.html"
          sourcepath="${src.dir}/java"
          packagenames="org.apache.alike.*"
          failonerror="true"
          destdir="${javadoc.dir}"
          access="protected"
          encoding="${compile.encoding}"
          docencoding="${compile.encoding}"
          charset="${compile.encoding}"
          additionalparam="-J-Dfile.encoding=${compile.encoding}"
          author="true"
          version="true"
          use="true"
          locale="en"
          source="${compile.target}"
          windowtitle="${PRODUCT_NAME} ${PRODUCT_VERSION} API"
          doctitle="${PRODUCT_NAME} ${PRODUCT_VERSION} API"
          stylesheetfile="${javadoc.dir}/prettify/stylesheet+prettify.css"
          bottom="Copyright &amp;copy; 2012-2014 &lt;a href=&quot;http://apache.org/&quot;&gt;Apache Software Foundation&lt;/a&gt; All Rights Reserved.">
            <classpath refid="common.path.lib"/>
            <link href="${javadoc.link.java}"/>
            <link href="${javadoc.link.mahout}"/>
            <link href="${javadoc.link.lucene.core}"/>
            <header><![CDATA[
<a href="http://labs.apache.org/labs.html">Apache alike Copyright (c) 2012-2014 Apache Software Foundation</a>
<script src="{@docRoot}/prettify/prettify.js" type="text/javascript"></script>
<script language="JavaScript">window.onload=function(){windowTitle();prettyPrint();}</script>
            ]]></header>
        </javadoc>
    </target>

    <target name="alike-dist" depends="alike-compile" description="make a package for distributed job on hadoop cluster">
        <delete dir="${job.dir}"/>
        <mkdir dir="${job.dir}"/>
        <copy todir="${job.dir}">
            <fileset dir="${cls.dir}"/>
	</copy>
        <unjar dest="${job.dir}">
            <fileset dir="${lib.dir}">
              <include name="**/*.jar"/>
              <exclude name="hadoop-*.jar"/>
              <exclude name="lucene-*-4.9.0.jar"/>
              <exclude name="*-javadoc.jar"/>
              <exclude name="*-sources.jar"/>
            </fileset>
        </unjar>
        <jar destfile="${product.job}" update="false">
            <fileset dir="${job.dir}"/>
        </jar>
    </target>

    <!-- ================================================================== -->
    <!-- = TEST ALIKE                                                     = -->
    <!-- ================================================================== -->
    <target name="test" depends="test-compile" description="test alike">
        <mkdir dir="${test.result.dir}"/>
        <junit printsummary="on"
               haltonfailure="no"
               errorProperty="tests.failed"
               failureProperty="tests.failed"
               dir="${src.dir}/test/test-files">
            <classpath refid="common.path.lib"/>
            <classpath path="${cls.dir}"/>
            <classpath path="${test.cls.dir}"/>
            <classpath path="${src.dir}/test"/>
            <formatter type="brief" usefile="false" if="junit.details"/>
            <formatter type="plain"/>
            <batchtest fork="yes" todir="${test.result.dir}" unless="testcase">
                <fileset dir="${src.dir}/test" includes="**/*Test.java"/>
            </batchtest>
            <batchtest fork="yes" todir="${test.result.dir}" if="testcase">
                <fileset dir="${src.dir}/test" includes="**/${testcase}.java"/>
            </batchtest>
        </junit>
        <fail if="tests.failed">***** Tests failed! *****</fail>
    </target>

    <target name="test-compile" depends="alike-compile" description="compile alike tests">
        <mkdir dir="${test.cls.dir}"/>
        <javac srcdir="${src.dir}/test"
               destdir="${test.cls.dir}"
               encoding="${compile.encoding}"
               deprecation="${compile.deprecation}"
               target="${compile.target}"
               debug="${compile.debug}">
            <classpath refid="common.path.lib"/>
            <classpath path="${cls.dir}"/>
        </javac>
    </target>

    <!-- ================================================================== -->
    <!-- = LAUNCH TOOLS                                                   = -->
    <!-- ================================================================== -->
    <target name="eval" depends="alike-compile" description="run SimpleTopHitsEvaluator program">
        <property name="noidf" value="false"/>
        <property name="nocoord" value="false"/>
        <property name="tddSymbol" value=""/>
        <java classname="org.apache.alike.eval.SimpleTopHitsEvaluator" fork="true">
            <jvmarg line="-Dfile.encoding=UTF-8"/>
            <arg line="${index} ${noidf} ${nocoord} ${tddSymbol}"/>
            <classpath refid="common.path.lib"/>
            <classpath path="${cls.dir}"/>
        </java>
    </target>

    <!-- ================================================================== -->
    <!-- = CLEANING                                                       = -->
    <!-- ================================================================== -->
    <target name="clean" description="clean all">
        <delete dir="${cls.dir}"/>
        <delete dir="${job.dir}"/>
        <delete dir="${test.cls.dir}"/>
        <delete dir="${javadoc.dir}"/>
        <delete dir="${test.result.dir}"/>
        <delete>
            <fileset dir="." includes="**/*~" defaultexcludes="no"/>
            <fileset dir="." includes="*.job"/>
        </delete>
    </target>

    <target name="clean-lib" description="clean libs downloaded by ivy">
        <delete dir="${lib.dir}"/>
    </target>
	
    <target name="clean-eclipse" description="delete Eclipse files, .project, .classpath and bin/">
        <delete dir="bin"/>
        <delete>
            <fileset dir="." includes=".project,.classpath"/>
        </delete>
    </target>

</project>
