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

  <property name="solr.version" value="solr-9.9.0-slim"/>
  <property name="solr.dirname" value="solr9"/>
  <property name="solr.srcdir" location="${basedir}/solr"/>

  <property name="os.linux" value="Linux"/> 
  <property name="os.mac" value="Mac OS X"/>
  <property name="os.solaris" value="SunOS"/>
  <property name="os.unix" value="${os.linux},${os.mac},${os.solaris}"/>
  <!-- ============ classpath for ant classes =================== -->
  <path id="project.classpath">
    <fileset dir="../../lib/java">
      <include name="**/*.jar"/>
    </fileset>
  </path>
  
  <!-- ============ self defined ant tasks =================== -->
  <taskdef name="if" classname="ise.antelope.tasks.IfTask" classpathref="project.classpath"/>
  
  <property environment="env"/>

  <!-- create build.properties if it has not been created yet -->
  <if>
    <bool><not><available file="build.properties"/></not></bool>
    <copy file="build.properties.svn" tofile="build.properties"/>
    <property name="first.run" value="true"/>
  </if>
  
  <property file="build.properties"/>
  
  <!-- DIRECTORY LOCATIONS -->
  <!-- local ext paths -->
  <loadproperties prefix="gs3." srcFile="${basedir}/../../build.properties"/> 
  <property name="java.src.home" location="${basedir}/src/java"/>
  <property name="build.home" location="${basedir}/build"/>

  <!-- greenstone3 paths -->
  <property name="gsdl3.writablebasedir" location="${basedir}/../.."/>
  <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>

  <!-- where solr9-jdbm-demo goes -->
  <property name="localsite.collectdir" location="${web.home}/sites/localsite/collect"/>
  <!-- where greenstone 3 jar files live -->
  <property name="web.libdir" location="${web.writablehome}/WEB-INF/lib"/>  

  <!-- writabel???-->
  <property name="data.home" location="${basedir}/../../data"/>
  <!-- solr's data home = SOLR_HOME -->
  <property name="solr.datadir" location="${data.home}/ext/${solr.dirname}"/>
  <property name="sites.dir" location="${web.writablehome}/sites"/>


  <condition property="compile.javac.flags" value="${env.JAVACFLAGS}" else="-Xlint:deprecation">
    <isset property="env.JAVACFLAGS"/>
  </condition>

  <!-- shuld we split this into client and plugin classpath? client - org.greenstone.gsdl3 - should only be using solrj. plugins - org.greenstone.solrserver - might use solr core -->
  <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"/>
    <pathelement location="${web.libdir}/log4j-1.2.16.jar"/>
    <pathelement location="${basedir}/solr/server/solr-webapp/webapp/WEB-INF/lib/solr-solrj-9.9.0.jar"/>
    <pathelement location="${basedir}/solr/server/solr-webapp/webapp/WEB-INF/lib/solr-core-9.9.0.jar"/>
  </path>

  <!-- I haven't done anything yet about this -->
  <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"/>


  <!-- 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="svnupdate" unless="nosvn.mode">
    <echo>svn updating solr</echo>
    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="update"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>
  </target>
  
  <target name="prepare-solr" description="Unpack the solr tgz file if nececssary">
    <echo>Checking for existence of: solr/.flagfile</echo>
    <if>
      <bool>
        <not><available file="solr/.flagfile"/></not>
      </bool>
      <untar src="${solr.version}.tgz" dest="." compression="gzip"/>
      <move todir="solr">
        <fileset dir="${solr.version}"/>
      </move>
      <chmod perm="ug+x" dir="${basedir}/solr/bin"
            includes="*.sh,solr,post,postlogs"/>

      <echo file="solr/.flagfile">
        the timestamp of this file is the time that solr was extracted from the zip files.
        it is used to figure out whether the files need to be refreshed or not in `ant prepare-solr`
      </echo>

      <else>
        <echo>SOLR has been prepared, will not prepare</echo>
        <echo>Delete ${basedir}/solr/.flagfile to force refresh</echo>
      </else>

    </if>
  </target> 
    
  <target name="compile" description="Compile up the solr java classes, jar them up, and install gs3-solr9.jar into Greenstone's web/WEB-INF/lib dir">
    <mkdir dir="${build.home}"/>
    <javac srcdir="${java.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-solr9.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-solr9.jar" todir="${web.libdir}"/>

    <!-- customisations to the http solr server -->
    <!-- dowe still need this? if so, it needs to be copied into solr area, not tomcat area -->
 <!--   <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 web jar lib directory: solr-solrj-9.9.0.jar, slf4j-api-2.0.13.jar</echo>
	<copy todir="${web.libdir}" flatten="true">
          <resources>
            <file file="${basedir}/solr/server/solr-webapp/webapp/WEB-INF/lib/solr-solrj-9.9.0.jar"/>
            <file file="${basedir}/solr/server/lib/ext/slf4j-api-2.0.13.jar"/>
            <!-- what else?-->
          </resources>
	</copy>	
	
	<echo/>
        <!-- should this part go into configure?? -->
	<echo>Creating solr data directory: ${solr.datadir} and subfolders</echo>
	<mkdir dir="${solr.datadir}"/>
	<mkdir dir="${solr.datadir}/cores"/>
        <mkdir dir="${solr.datadir}/cores/localsite"/>
	<echo/>
	<echo>Adding to solr data directory: solr.xml</echo>
	<copy file="solr.xml-gs3" tofile="${solr.datadir}/solr.xml"/>
	
	<echo/>
        <!-- TODO - don't copy this again if its already there -->
	<echo>Adding example solr9-jdbm-demo collection to ${localsite.collectdir}</echo>
	<copy todir="${localsite.collectdir}/solr9-jdbm-demo"
	      preservelastmodified="true"
	      failonerror="true" >  
	  <fileset dir="${basedir}/collect/solr9-jdbm-demo" includes="**"/>  
	</copy>
        <echo/>
        <echo>Adding solr9-jdbm-demo cores to solr data directory: ${solr.datadir}/cores/localsite/solr9-jdbm-demo</echo>
        <filter token="collectdir" value="${localsite.collectdir}"/>
        <copy todir="${solr.datadir}/cores/localsite"
              preservelastmodified="true"
	      failonerror="true"
              filtering="true">
	  <fileset dir="${basedir}/demo-cores" includes="**"/>  
	</copy>
        <echo/>
        <echo>Unzipping pre-built archives and index</echo>
        <unzip dest="${localsite.collectdir}/solr9-jdbm-demo" src="${localsite.collectdir}/solr9-jdbm-demo/index-and-archives.zip" />
        <echo>Copying conf from built collection into core area</echo>
        <copy todir="${solr.datadir}/cores/localsite/solr9-jdbm-demo/localsite-solr9-jdbm-demo-didx"
              preservelastmodified="true"
              failonerror="true">
          <fileset dir="${localsite.collectdir}/solr9-jdbm-demo/index/didx" includes="conf/**"/>
        </copy>
        <copy todir="${solr.datadir}/cores/localsite/solr9-jdbm-demo/localsite-solr9-jdbm-demo-sidx"
              preservelastmodified="true"
              failonerror="true">
          <fileset dir="${localsite.collectdir}/solr9-jdbm-demo/index/sidx" includes="conf/**"/>
        </copy>
  </target>

  <target name="status-solr">
    <condition property="solr.isrunning" value="true" else="false">
      <http url="http://${localhost.http}:${solr.port}"/>
    </condition>
    <echo>Solr is running: ${solr.isrunning}</echo>
  </target>
    
  <target name="start-solr" description="Start the SOLR server">
    <exec executable="${solr.srcdir}/bin/solr" os="${os.unix}" spawn="false">
      <arg value="start"/>
    </exec>
    <exec executable="${solr.srcdir}/bin/solr.cmd" osfamily="windows" spawn="false">
      <arg value="start"/>
    </exec>
  </target>
  <target name="stop-solr" description="Stop the SOLR server">
    <exec executable="${solr.srcdir}/bin/solr" os="${os.unix}" spawn="false">
      <arg value="stop"/>
    </exec>
    <exec executable="${solr.srcdir}/bin/solr.cmd" osfamily="windows" spawn="false">
      <arg value="stop"/>
    </exec>
  </target>

 <!-- add compile back to the end of this list once java src is there -->
  <target name="add-extension" depends="prepare-solr,copy-files,compile" 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 ${solr.datadir}, remove solr jar files from ${web.libdir} and remove the solr9-jdbm-demo collection from localsite's collect folder.
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">
   <!-- copy our modified solr.in.sh/cmd into solr/bin -->
    <!-- we modify it to set SOLR_HOME -->
    <filter token="solrdatadir" value="${solr.datadir}"/>
    <filter token="solrport" value="${solr.port}"/>
    <filter token="sitesdir" value="${sites.dir}"/>
    <copy file="solr.in.sh-gs3" tofile="solr/bin/solr.in.sh" filtering="true" overwrite="true"/>
    <copy file="solr.in.cmd-gs3" tofile="solr/bin/solr.in.cmd" filtering="true" overwrite="true"/>
    <!-- currently not needed   
<filter token="gsdl3webhome" value="${web.home.unix}"/>
    <copy file="${solr.datadir}/solr.xml.in" tofile="${solr.datadir}/solr.xml" filtering="true" overwrite="true"/>-->

  </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"
          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 ${web.libdir}: ${runtime-jars} gs3-solr9.jar</echo>
	<delete failonerror="false">
          <filelist dir="${web.libdir}" files="${runtime-jars} gs3-solr9.jar"/>
	</delete>	
	
	<echo/>
	<echo>Removing web solr extension directory: ${solr.datadir}</echo>
	<delete failonerror="false" includeEmptyDirs="true" dir="${solr.datadir}"/>	

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

  </target>

  <target name="get-solr-admin-url">
    <echo>http://${localhost.http}:${solr.port}/solr</echo>
  </target>

  <!-- solr.lock.type - needs to be single if we have read-only location-->
  <!-- is this still correct and where? how? -->

  <target name="reset-logs" description="Clean out the solr logs folder">
    <if><bool><available file="${solr.datadir}/logs" type="dir"/></bool>
    <delete>
      <fileset dir="${solr.datadir}/logs" includes="**/*"/>
    </delete>
    </if>
  </target>
  <!-- do we need to touch the files? what is=f the server is currently running? -->
</project>
