<?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="core" default="default">
  <description>Lucene core library</description>

  <property name="build.dir" location="../build/core"/>

  <property name="forbidden-sysout-excludes" value="
    org/apache/lucene/index/CheckIndex.class
    org/apache/lucene/index/IndexUpgrader.class
    org/apache/lucene/store/LockVerifyServer.class
    org/apache/lucene/store/LockStressTest.class
    org/apache/lucene/util/PrintStreamInfoStream.class
  "/>

  <property name="forbidden-rue-excludes" value="
    org/apache/lucene/search/FieldCache$CacheEntry.class
    org/apache/lucene/util/RamUsageEstimator.class
    org/apache/lucene/search/CachingWrapperFilter.class
  "/>

  <import file="../common-build.xml"/>

  <property name="moman.commit-hash" value="5c5c2a1e4dea" />
  <property name="moman.url" value="https://bitbucket.org/jpbarrette/moman/get/${moman.commit-hash}.zip" />

  <path id="classpath"/>
  
  <path id="test.classpath">
    <pathelement location="${common.dir}/build/test-framework/classes/java"/>
    <pathelement location="${common.dir}/build/codecs/classes/java"/>
    <path refid="junit-path"/>
    <pathelement location="${build.dir}/classes/java"/>
    <pathelement location="${build.dir}/classes/test"/>
  </path>

  <path id="junit.classpath">
    <path refid="test.classpath"/>
    <pathelement path="${java.class.path}"/>
  </path>

  <target name="test-core" depends="common.test"/>

  <target name="javadocs-core" depends="javadocs"/>
  <target name="javadocs" description="Generate javadoc for core classes">
  	<sequential>
      <mkdir dir="${javadoc.dir}/core"/>
      <invoke-javadoc destdir="${javadoc.dir}/core" title="${Name} ${version} core API">
        <sources>
          <packageset dir="${src.dir}"/>
          <link href=""/>
        </sources>
      </invoke-javadoc>
      <mkdir dir="${build.dir}"/>
     <jarify basedir="${javadoc.dir}/core" destfile="${build.dir}/${final.name}-javadoc.jar"/>
    </sequential>
  </target>

  <target name="-dist-maven" depends="-dist-maven-src-java"/>

  <macrodef name="createLevAutomaton">
  	<attribute name="n"/>
  	<sequential>
      <exec dir="src/java/org/apache/lucene/util/automaton"
            executable="${python.exe}" failonerror="true">
	 <!-- Tell Python not to write any bytecode cache into the filesystem: -->
	 <arg value="-B"/>
        <arg value="createLevAutomata.py"/>
        <arg value="@{n}"/>
        <arg value="True"/>
      </exec>
      <exec dir="src/java/org/apache/lucene/util/automaton"
            executable="${python.exe}" failonerror="true">
	 <!-- Tell Python not to write any bytecode cache into the filesystem: -->
	 <arg value="-B"/>
        <arg value="createLevAutomata.py"/>
        <arg value="@{n}"/>
        <arg value="False"/>
      </exec>
      <fixcrlf srcdir="src/java/org/apache/lucene/util/automaton" includes="*ParametricDescription.java" encoding="UTF-8"/>
    </sequential>
  </macrodef>

  <target name="createPackedIntSources">
    <exec dir="src/java/org/apache/lucene/util/packed"
          executable="${python.exe}" failonerror="true">
      <!-- Tell Python not to write any bytecode cache into the filesystem: -->
      <arg value="-B"/>
      <arg value="gen_BulkOperation.py"/>
    </exec>
    <exec dir="src/java/org/apache/lucene/util/packed"
          executable="${python.exe}" failonerror="true">
      <!-- Tell Python not to write any bytecode cache into the filesystem: -->
      <arg value="-B"/>
      <arg value="gen_Direct.py"/>
    </exec>
    <exec dir="src/java/org/apache/lucene/util/packed"
          executable="${python.exe}" failonerror="true">
      <!-- Tell Python not to write any bytecode cache into the filesystem: -->
      <arg value="-B"/>
      <arg value="gen_Packed64SingleBlock.py"/>
    </exec>
    <exec dir="src/java/org/apache/lucene/util/packed"
          executable="${python.exe}" failonerror="true">
      <!-- Tell Python not to write any bytecode cache into the filesystem: -->
      <arg value="-B"/>
      <arg value="gen_PackedThreeBlocks.py"/>
    </exec>
    <fixcrlf srcdir="src/java/org/apache/lucene/util/packed" includes="BulkOperation*.java,Direct*.java,Packed64SingleBlock.java,Packed*ThreeBlocks.py" encoding="UTF-8"/>
  </target>

  <target name="createLevAutomata" depends="check-moman,download-moman">
    <createLevAutomaton n="1"/>
    <createLevAutomaton n="2"/>
  </target>
  
  <target name="check-moman">
    <available file="${build.dir}/moman" property="moman.downloaded"/>
  </target>

  <target name="download-moman" unless="moman.downloaded">
    <mkdir dir="${build.dir}/moman"/>
    <get src="${moman.url}" dest="${build.dir}/moman.zip"/>
    <unzip dest="${build.dir}/moman" src="${build.dir}/moman.zip">
      <cutdirsmapper dirs="1"/>
    </unzip>
    <delete file="${build.dir}/moman.zip"/>
  </target>

  <target name="regenerate" depends="createLevAutomata,createPackedIntSources"/>
  
  <macrodef name="startLockStressTestClient">
    <attribute name="clientId"/>
    <attribute name="lockFactoryImpl"/>
    <attribute name="lockFactoryDir"/>
    <sequential>
      <local name="lockverifyserver.port"/>
      <groovy><![CDATA[
        String port;
        while ((port = System.getProperty("lockverifyserver.port")) == null) {
          Thread.sleep(10L);
        }
        properties["lockverifyserver.port"] = port;
      ]]></groovy>
      <java taskname="lockStressTest@{clientId}" fork="true" classpathref="test-lock.classpath" classname="org.apache.lucene.store.LockStressTest" failOnError="true"> 
        <arg value="@{clientId}"/>
        <arg value="${lockverifyserver.host}"/>
        <arg value="${lockverifyserver.port}"/>
        <arg value="@{lockFactoryImpl}"/>
        <arg value="@{lockFactoryDir}"/>
        <arg value="${lockverify.delay}"/>
        <arg value="${lockverify.count}"/>
      </java>
    </sequential>
  </macrodef>
  
  <macrodef name="testLockFactory">
    <attribute name="lockFactoryImpl"/>
    <attribute name="lockFactoryDir"/>
    <sequential>
      <echo taskname="testLockFactory" message="Testing @{lockFactoryImpl}..."/>
      <mkdir dir="@{lockFactoryDir}"/>
      <parallel threadCount="3" failonany="false">
        <sequential>
          <!-- the server runs in-process, so we can wait for the sysproperty -->
          <java taskname="lockVerifyServer" fork="false" classpathref="test-lock.classpath" classname="org.apache.lucene.store.LockVerifyServer" failOnError="true">
            <arg value="${lockverifyserver.host}"/>
            <arg value="2"/>
          </java>
        </sequential>
        <sequential>
          <startLockStressTestClient clientId="1" lockFactoryImpl="@{lockFactoryImpl}" lockFactoryDir="@{lockFactoryDir}" />
        </sequential>
        <sequential>
          <startLockStressTestClient clientId="2" lockFactoryImpl="@{lockFactoryImpl}" lockFactoryDir="@{lockFactoryDir}" />
        </sequential>
      </parallel>
    </sequential>
  </macrodef>
  
  <!-- we ignore our ant-based lock factory test, if user applies test filtering: -->
  <condition property="-ignore-test-lock-factory">
    <or>
      <isset property="tests.class" />
      <isset property="tests.method" />
    </or>
  </condition>
  
  <target name="test-lock-factory" depends="resolve-groovy,compile-core" unless="-ignore-test-lock-factory"
    description="Run LockStressTest with multiple JVMs">
    <property name="lockverifyserver.host" value="127.0.0.1"/>
    <property name="lockverify.delay" value="1"/>
    <groovy taskname="lockVerifySetup"><![CDATA[
      System.clearProperty("lockverifyserver.port"); // make sure it is undefined
      
      if (!properties["lockverify.count"]) {
        int count = Boolean.parseBoolean(properties["tests.nightly"]) ?
          30000 : 2000;
        count *= Integer.parseInt(properties["tests.multiplier"]);
        properties["lockverify.count"] = count;
      }
      
      task.log("Configuration properties:");
      ["lockverify.delay", "lockverify.count"].each {
        k -> task.log(" " + k + "=" + properties[k]);
      }
    ]]></groovy>
    <path id="test-lock.classpath">
      <path refid="classpath"/>
      <pathelement location="${build.dir}/classes/java"/>
    </path>
    <testLockFactory lockFactoryImpl="org.apache.lucene.store.NativeFSLockFactory" lockFactoryDir="${build.dir}/lockfactorytest/native" />
    <testLockFactory lockFactoryImpl="org.apache.lucene.store.SimpleFSLockFactory" lockFactoryDir="${build.dir}/lockfactorytest/simple" />
  </target>
  
  <target name="test" depends="common.test, test-lock-factory"/>

</project>
