<?xml version="1.0"?>
<project name="gs3-solr-ext" default="usage" basedir=".">

  <!-- ============ classpath =================== -->
  <path id="project.classpath">
    <fileset dir="../../lib/java">
      <include name="**/*.jar"/>
    </fileset>
  </path>
  
  <!-- ============ self defined tasks =================== -->
  <taskdef name="if" classname="ise.antelope.tasks.IfTask" classpathref="project.classpath"/>
  
  <property environment="env"/>
  
  <!-- DIRECTORY LOCATIONS -->
  <!-- local ext paths -->
  <loadproperties prefix="gs3." srcFile="${basedir}/../../build.properties"/> 
  <property name="src.home" value="${basedir}/src/java"/>
  <property name="build.home" value="${basedir}/build"/>
  <available property="ext.web.exists" file="${basedir}/web" type="dir" />

  <!-- greenstone3 paths -->
  <!-- http://stackoverflow.com/questions/3136849/how-do-i-convert-a-relative-path-in-ant-to-an-absolute-path 
    http://stackoverflow.com/questions/8295860/pathconvert-with-relative-file-names -->
  <property name="web.home" location="${basedir}/../../web"/> <!-- location property creates an absolute path -->
  <pathconvert targetos="unix" property="web.home.unix">
      <path path="${web.home}"/><!-- creates an absolute path-->
  </pathconvert>

  <property name="web.writablehome" location="${basedir}/../../web"/>
  <pathconvert targetos="unix" property="web.writablehome.unix">
      <path path="${web.writablehome}"/><!-- creates an absolute path-->
  </pathconvert>

  <property name="localsite.collectdir" value="${web.home}/sites/localsite/collect"/>
  <property name="web.classesdir" value="${web.writablehome}/WEB-INF/classes"/>
  <property name="web.libdir" value="${web.writablehome}/WEB-INF/lib"/>  
  <property name="web.extdir" value="${web.writablehome}/ext/solr"/>

  <condition property="tomcat.dir" value="${gs3.tomcat.installed.path}" else="${basedir}/../../packages/tomcat">
    <and>
      <isset property="gs3.tomcat.installed.path"/>
      <not>
        <equals arg1="${gs3.tomcat.installed.path}" arg2=""/>
      </not>
    </and>
  </condition>
  <pathconvert targetos="unix" property="tomcat.dir.unix">
    <path path="${tomcat.dir}"/><!-- creates an absolute path-->
  </pathconvert>

  <property name="solr.context.default" value="solr"/>
  <condition property="solr.context" value="${gs3.solr.context}" else="${solr.context.default}">
    <and>
      <isset property="gs3.solr.context"/>
      <not>
        <equals arg1="${gs3.solr.context}" arg2=""/>
      </not>
    </and>
  </condition>
  <property name="tomcat.lib.dir" value="${tomcat.dir}/lib"/>
  <property name="tomcat.context.dir" value="${tomcat.dir}/conf/Catalina/localhost"/>

  <condition property="compile.javac.flags" value="${env.JAVACFLAGS}" else="">
    <isset property="env.JAVACFLAGS"/>
  </condition>
		
  <path id="compile.classpath">
    <!-- gs3 jar files we need -->
    <pathelement location="${web.libdir}/gsdl3.jar"/>
    <pathelement location="${web.libdir}/gutil.jar"/>
    <pathelement location="${web.libdir}/LuceneWrapper4.jar"/>
    <pathelement location="${web.libdir}/gson-2.8.6.jar"/>
    <!-- local jar files -->
    <fileset dir="lib/java">
      <include name="*.jar"/>
    </fileset>
    <pathelement location="lib/servlet-api-3.0.jar"/>
  </path>


  <!-- FILE LISTINGS.-->
  <!-- Created as property elements rather than as filelist elements, since
	they can then be reused for the add-extension and del-extension targets. -->
  <property name="jars" 
		value="solr-core-4.7.2.jar
		solr-solrj-4.7.2.jar
		lucene-core-4.7.2.jar
		lucene-analyzers-common-4.7.2.jar
		lucene-analyzers-kuromoji-4.7.2.jar
		lucene-analyzers-phonetic-4.7.2.jar
		lucene-grouping-4.7.2.jar
		lucene-highlighter-4.7.2.jar
		lucene-queries-4.7.2.jar
		lucene-queryparser-4.7.2.jar
		lucene-memory-4.7.2.jar
		lucene-misc-4.7.2.jar
		lucene-spatial-4.7.2.jar
		lucene-suggest-4.7.2.jar
		httpclient-4.3.1.jar
		httpcore-4.3.jar
		httpmime-4.3.1.jar
		commons-fileupload-1.2.1.jar
		commons-io-2.1.jar
		commons-lang-2.6.jar
		velocity-1.6.1.jar
		log4j-over-slf4j-1.6.6.jar
		slf4j-api-1.6.6.jar
		slf4j-jdk14-1.6.6.jar
		spatial4j-0.4.1.jar
		zookeeper-3.4.5.jar
		guava-14.0.1.jar
		noggit-0.5.jar"/>
<!-- 		jcl-over-slf4j-1.6.6.jar
		jul-to-slf4j-1.6.6.jar
		log4j-1.2.16.jar
		wstx-asl-3.2.7.jar
-->
<!-- 
   lucene-spellchecker-3.3.0.jar replaced with lucene-suggest-4.7.2.jar, since both contain packages org/.../spell and org/.../suggest 
   
   commons-fileupload-1.2.1.jar - in solr war file
   velocity-1.6.1.jar
   log4j-over-slf4j-1.6.1.jar - have to download slf4j jar file for version 1.6.6 
   slf4j-jdk14-1.6.1.jar - have to download slf4j jar file for version 1.6.6 
  -->

  <property name="shared-xalan-jars"
	    value="xalan.jar
		   xercesImpl.jar
		   xml-apis.jar
		   xsltc.jar
		   serializer.jar"/>
  <property name="russian-morph-jars"
	    value="jmorphy2-lucene-0.2.0.jar
                   jmorphy2-nlp-0.2.0.jar
                   jmorphy2-core-0.2.0.jar
                   dawg-0.2.0.jar
                   jmorphy2-dicts-uk-0.2.0.jar
                   guava-18.0.jar
                   commons-codec-1.10.jar
                   noggit-0.7.jar
                   jmorphy2-dicts-ru-0.2.0.jar"/>


  <property name="java-service-files" 
    value="SolrSearch.java		   	
    GS2SolrSearch.java
    GS2SolrRetrieve.java"/>  
		   
  <property name="java-util-files" 
    value="SolrFacetWrapper.java
    SolrQueryWrapper.java			
    SolrQueryResult.java"/>  		   
  
<!-- <property name="property-files" 
    value="GS2SolrSearch.properties"/> --> 


  <!-- TARGETS -->
  <target name="usage" description="Print a help message">
    <echo message="  Execute 'ant -projecthelp' for a list of targets."/>
    <echo message="  Execute 'ant -help' for Ant help."/>
    <echo>To install the Solr extension for Greenstone3, run 'ant add-extension'.
	To remove the files and folders installed by add-extension, run 'ant del-extension'.
    </echo>
  </target>			
  
  <target name="compile" description="Compile up the solr java classes, jar them up, and install gs3-solr.jar into Greenstone's web/WEB-INF/lib dir">
    <mkdir dir="${build.home}"/>
    <javac srcdir="${src.home}" destdir="${build.home}" debug="true"
	   includeantruntime="false">
      <compilerarg line="${compile.javac.flags}"/>
      <classpath>
	<path refid="compile.classpath"/>
      </classpath>
    </javac>
    <jar destfile="${build.home}/gs3-solr.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/gsdl3/**"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <copy file="${build.home}/gs3-solr.jar" todir="${web.libdir}"/>

    <!-- customisations to the http solr server -->
    <jar destfile="${build.home}/gs3-solrserver.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/solrserver/**"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="greenstone3" />
      </manifest>
    </jar>
    <!-- update the war file -->
    <copy file="webapps/solr.war" tofile="webapps/gs3-solr.war" overwrite="true"/>
    <echo>Updating xalan related jar files, morphology and gs3-solrserver jars from ${web.libdir} in gs3-solr.war</echo>
    <war destfile="webapps/gs3-solr.war" update="true" >
      <zipfileset dir="lib/russianmorphology" includes="*.jar" prefix="WEB-INF/lib"/>
      <zipfileset dir="lib/ext" includes="*.jar" prefix="WEB-INF/lib"/>
      <zipfileset dir="${web.libdir}" includes="${shared-xalan-jars}" prefix="WEB-INF/lib"/>
      <zipfileset dir="${build.home}" includes="gs3-solrserver.jar" prefix="WEB-INF/lib"/>
    </war>    

    <!-- If you are testing, call ant compile-gs3-solrserver, which will copy gs3-solrserver.jar directly into unpacked solr webapp in tomcat. 
      -->
  </target>

  <target name="clean" description="Delete the solr compiled classes (build folder)">
    <delete dir="${build.home}"/>
  </target>
  
  <target name="copy-files" description="Helper-target: copy files across for add-extension target">
  	
    <echo/>
<!--	<echo>Adding to gsdl3 properties area: properties/${property-files}</echo>
	<copy todir="${web.classesdir}">
          <filelist id="prop-files" dir="properties" files="${property-files}"/>
	</copy>
	<echo/>-->
	<echo>Adding to gsdl3 web jar lib directory: ${basedir}/lib/java's ${jars}</echo>
	<copy todir="${web.libdir}">
          <filelist id="jar-files" dir="lib/java" files="${jars}"/>
	</copy>	
	
	<echo/>
	<echo>Creating web extension directory: ${web.extdir}</echo>
	<mkdir dir="${web.extdir}"/>
	
	<echo/>
	<echo>Adding to gsdl3 web solr ext directory: solr.xml.in</echo>
	<copy file="solr.xml.in" tofile="${web.extdir}/solr.xml.in"/>
	<pathconvert targetos="unix" property="src.gsdl3.home.unix">
	  <path path="../../web"/>
	</pathconvert>
	
	<echo/>
	<echo>Adding example solr-jdbm-demo collection to ${localsite.collectdir}</echo>
	<copy todir="${localsite.collectdir}/solr-jdbm-demo"
	      preservelastmodified="true"
	      failonerror="true" >  
	  <fileset dir="${basedir}/collect/solr-jdbm-demo" includes="**"/>  
	</copy>
    <echo>Unzipping pre-built index</echo>
    <unzip dest="${localsite.collectdir}/solr-jdbm-demo" src="${localsite.collectdir}/solr-jdbm-demo/index.zip" />
  </target>

  <!-- Setting up solr to work with tomcat server instead of jetty server -->
  <target name="install-solr-for-tomcat" description="Helper-target: install solr war and context file into tomcat">
    
    <!-- slf4j and commons logging bridge needed to avoid exception about incompatibility in tomcat log files-->
    <echo>Copying ${basedir}/lib/ext/jcl-over-slf4j-1.6.6.jar again to ${web.libdir}</echo>
    <copy todir="${web.libdir}">
      <filelist id="logging-bridge" dir="lib/ext" files="jcl-over-slf4j-1.6.6.jar"/>
    </copy>
	
    
    <echo>Copying ${basedir}/webapps/gs3-solr.war to ${tomcat.dir}/webapps/${solr.context}.war</echo>
    <copy file="webapps/gs3-solr.war" tofile="${tomcat.dir}/webapps/${solr.context}.war" />
	
    <echo>Generating solr context file in ${tomcat.context.dir}</echo>
    
    <!-- we want unix paths (forward slashes) in the tomcat context file -->
    <filter token="gsdl3webwritablehome" value="${web.writablehome.unix}"/>
    <filter token="tomcathome" value="${tomcat.dir.unix}"/>
    <filter token="solr.context" value="${solr.context}"/>
    <filter token="IPv4" value="${ipv4.val}"/>
    <copy file="solr-tomcat-context.xml.in" tofile="${tomcat.context.dir}/${solr.context}.xml" filtering="true" overwrite="true"/>

    <if>
      <bool><not><equals arg1="${solr.context}" arg2="${solr.context.default}"/></not></bool>
      <echo>Installed ${solr.context}.war and ${solr.context}.xml context files - Using non-standard names - do you need to delete other versions from the ${tomcat.dir}/webapps and ${tomcat.context.dir}/directory?</echo>
    </if>

  </target>  
  
  <!-- copy the content of the web folder (avoiding the top-level .svn directory) -->
  <target name="copy-solr-web" if="ext.web.exists">
    <echo/>
    <echo>Copy to gsdl3 web: the content of the ${basedir}/web folder (excluding .svn)</echo>
    <copy todir="${web.extdir}">
      <dirset dir="${basedir}/web">
	<exclude name=".svn"/>
      </dirset> 
    </copy>
  </target>


  <target name="add-extension" depends="copy-solr-web,copy-files,compile,install-solr-for-tomcat" description="Run this target to setup the Solr extension for Greenstone3" />

  <target name="accept-del-extension" unless="delextension.accepted">
    <input addproperty="delextension.ok" validargs="y,n">This will recursively delete all the files in:
    ${web.extdir}/solr
Do you want to continue [y/n]
    </input>
    <condition property="do.abort-delextension">
      <equals arg1="n" arg2="${delextension.ok}"/>
    </condition>
    <fail if="do.abort-delextension">... Exiting</fail>
  </target>


  <target name="configure-extension" description="Re-configure the solr extension for Greenstone">

    <filter token="gsdl3webhome" value="${web.home.unix}"/>
    <copy file="${web.extdir}/solr.xml.in" tofile="${web.extdir}/solr.xml" filtering="true" overwrite="true"/>

    <antcall target="install-solr-for-tomcat"/>
  </target>

  <target name="del-extension" depends="accept-del-extension,deactivate"
  description="Run this target to remove the Solr extension for Greenstone3" />

  <!-- Called by DEACTIVATE.SH, no user prompt -->
  <target name="deactivate" depends="del-files,del-solr-for-tomcat"
          description="Run this target to QUIETLY remove the Solr extension for Greenstone3" />
  
  <target name="del-files" description="Helper-target to delete files for deactivate target">
	<!-- failonerror is set to false in case some files don't exist 
	and can't be deleted therefore -->

	<echo/>
<!--	<echo>Removing from gsdl3 properties area: properties/${property-files}</echo>
	<delete failonerror="false">
          <filelist dir="${web.classesdir}" files="${property-files}"/>
	</delete>

	<echo/>-->
	<echo>Removing from gsdl3 web jar lib directory: ${basedir}/lib/java's ${jars} gs3-solr.jar</echo>
	<delete failonerror="false">
          <filelist dir="${web.libdir}" files="${jars} gs3-solr.jar"/>
	</delete>	
	
	<echo/>
	<echo>Removing web solr extension directory: ${web.extdir}</echo>
	<delete failonerror="false" includeEmptyDirs="true" dir="${web.extdir}"/>	

	<echo/>
	<echo>Removing solr-jdbm-demo collection from: ${localsite.collectdir}</echo>
	<delete failonerror="false" includeEmptyDirs="true" dir="${localsite.collectcdir}/solr-jdbm-demo"/>

  </target>
  
  <target name="del-solr-for-tomcat" description="Helper-target to remove files for getting solr to work with tomcat">

	<echo/>
	<echo>Removing solr modifications to tomcat: context file, solr.war and deployed version, jar files</echo>

	<!-- Actually need to ensure tomcat is not running at this point
	     But if solr.war is deleted first, it can't be re-deployed when deleting the solr folder subsequently
	  -->
	<delete failonerror="false" file="${tomcat.dir}/webapps/${solr.context}.war"/>
	<delete failonerror="false" includeEmptyDirs="true" dir="${tomcat.dir}/webapps/${solr.context}"/>
        <!-- just in case we have been using a non-standard context, lets make sure the 
         default context war file is not also there -->

	<delete failonerror="false" file="${tomcat.dir}/webapps/${solr.context.default}.war"/>
	<delete failonerror="false" includeEmptyDirs="true" dir="${tomcat.dir}/webapps/${solr.context.default}"/>

	<delete failonerror="false" file="${tomcat.context.dir}/${solr.context}.xml"/>
	<delete failonerror="false" file="${tomcat.context.dir}/${solr.context.default}.xml"/>

	<!-- delete all the jar files in tomcat/lib that were copied from ext/solr/lib/ext
	     which are all the jar files that are present in both tomcat/lib and ext/solr/lib/ext
	     https://ant.apache.org/manual/Types/selectors.html#presentselect -->
	<delete failonerror="false">	  
	  <fileset dir="${tomcat.lib.dir}" includes="**/*.jar">	    
	    <present present="both" targetdir="lib/ext"/>
	  </fileset>
	</delete>

	<!-- remove logging bridge jar file that was added into greenstone 3 web lib area -->
	<delete failonerror="false" file="${web.libdir}/jcl-over-slf4j-1.6.6.jar"/>

	<!--http://stackoverflow.com/questions/2140637/how-do-i-build-a-list-of-file-names-->
  </target>
  
  <target name="compile-gs3-solrserver" description="TEST Target to compile the solr server side gs3-solserver.jar for testing">
    <delete dir="build/org/greenstone/solrserver" />
    <delete file="build/gs3-solrserver.jar" />
    <antcall target="compile" />
    <copy file="${build.home}/gs3-solrserver.jar" todir="${tomcat.dir}/webapps/solr/WEB-INF/lib" />
  </target>

</project>
