<?xml version="1.0"?>
<!-- ====================================================================== 
     Greenstone3 build and install script
                  
     kjdon, anu, davidb
     ====================================================================== -->

<project name="greenstone3" default="usage" basedir=".">
  <echo>OS    : ${os.name}</echo>
  <!--
      <echo>os.arch: ${os.arch}</echo>
      <echo>os.version: ${os.version}</echo>
  -->
  
  <antversion property="ant.version.running"/>
  <property name="ant.version.minimum" value="1.8.2"/>
  <fail message="Minimum of Ant v${ant.version.minimum} required to operate Greenstone3 (detected version ${ant.version.running})">
    <condition>      
      <not><antversion atleast="${ant.version.minimum}"/></not>
    </condition>
  </fail>


  <!-- ============ classpath =================== -->
  <path id="project.classpath">
    <fileset dir="lib/java">
      <include name="**/*.jar"/>
    </fileset>
  </path>
  
  <!-- ============ self defined tasks =================== -->
  <taskdef name="mysetproxy" classname="org.greenstone.anttasks.MySetProxy" classpathref="project.classpath"/>
  <taskdef name="getuserandpassword" classname="org.greenstone.anttasks.MyGetUserAndPassword" classpathref="project.classpath"/>
  <taskdef name="rsr" classname="org.greenstone.anttasks.RegexSearchReplace" classpathref="project.classpath"/>
  <!--<taskdef name="svn" classname="org.tigris.subversion.svnant.SvnTask" classpathref="project.classpath"/>-->
  <taskdef name="if" classname="ise.antelope.tasks.IfTask" classpathref="project.classpath"/>
  <taskdef name="for" classname="net.sf.antcontrib.logic.ForTask" classpathref="project.classpath"/>
  <taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" classpathref="project.classpath"/>
  <taskdef name="stringutil" classname="ise.antelope.tasks.StringUtilTask" classpathref="project.classpath"/>

  <!-- ===================== Property Definitions =========================== -->
  
  <!--

  Each of the following properties are used in the build script.
  Values for these properties are set by the first place they are
  defined, from the following list:

  * Definitions on the "ant" command line (ant -Dfoo=bar compile).

  * Definitions from a "build.properties" file in the top level
  source directory of this application.

  * Definitions from a "build.properties" file in the user's
  home directory.

  * Default definitions in this build.xml file.

  You will note below that property values can be composed based on the
  contents of previously defined properties.  This is a powerful technique
  that helps you minimize the number of changes required when your development
  environment is modified.  Note that property composition is allowed within
  "build.properties" files as well as in the "build.xml" script.

  -->

  <!-- Developer property. Set value=true if you want test https certificates issued
       (as there's a limit on how many real certificates you can be issued per week).
       If not testing can either uncomment this property or set value=false.
       BEWARE: iff test.mode is true (so https.testing gets set to minus-minus-staging)
       AND the default protocol is https (server.protocols contains https first), the 
       check-tomcat-running target and any other targets running an ant http condition
       to test the default GS DL URL (which will be over https) won't work.
       This is because the testing certificate is not trusted by clients, in this case
       the ant http condition. (Clients that are browsers can be set to temporarily
       trust the testing certificate, but don't know how to make ant do that, maybe it
       needs to be done at Java level.)
       Ant socket conditions to the URL will work because socket tests don't use
       the protocol, just the host and port.
  -->
  <property name="https.test.mode" value="false"/>
  <property name="sudo.or.not" value="/bin/bash"/><!-- by default, we don't run even special commands with sudo -->

  <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}"/>
  <property name="os.windows" value="Windows 95,Windows 98,Windows 2000,Windows 2003,Windows XP,Windows NT,Windows ME,Windows Vista,Windows 7,Windows Server 2008,Windows Server 2008 R2,Windows 8,Windows 10,Windows 11"/> <!-- check this!!!-->

  <!-- this is true for linux and macs -->
  <condition property="current.os.isunix">
    <os family="unix"/>
  </condition>
  
  <condition property="current.os.isunixnotmac">
    <and>
      <os family="unix"/>
      <not>
	<os family="mac"/>
      </not>
    </and>
  </condition>
  
  <condition property="current.os.ismac">
    <os family="mac"/>
  </condition>
  
  <condition property="current.os.iswindows">
    <os family="windows"/>
  </condition>

  <!-- 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>
  <!-- create servlet.properties if it hasn't been created yet -->
<if>
    <bool><not><available file="servlet.properties"/></not></bool>
    <copy file="servlet.properties.svn" tofile="servlet.properties"/>
  </if>
  
  
  <!-- create the packages dir if it has not been created yet -->
  <mkdir dir="packages"/>
  
  <!--the first three properties have to be put on the top to be used by build.properties-->
  <property name="gs2build.home" value="${basedir}${file.separator}gs2build"/>
  <property name="src.packages.home" value="${basedir}/src/packages"/>

  <property name="solr-ext.home"     value="${basedir}/ext/solr9"/>
  <property name="webswing-ext.home" value="${basedir}/ext/webswing"/>

  <if>
    <bool><available file="${webswing-ext.home}"/></bool>    
    <property name="webswing-ext.exists" value="true"/>
  </if>

  <property file="build.properties"/>
  <if>
    <bool><available file="${user.home}/build.properties"/></bool>
    <property file="${user.home}/build.properties"/>
  </if>

  <!-- on windows, we have downloaded wget into bin/windows, but its not on the path, so specify full path when calling it -->
  <if>
    <bool><isset property="current.os.iswindows"/></bool>
    <property name="wget" value="${basedir}/bin/windows/wget.exe"/>
    <else>
      <property name="wget" value="wget"/>
    </else>
  </if>
  
  <!-- Check for whether tomcat is to allow symlinks or not. This should always be false
       for Windows. And ideally also for any other OS with a case insensitive filesystem, but
       we can only detect Windows and override user assigned true value for Windows to false. -->
  <condition property="tomcat.allowLinking" value="false" else="${tomcat.user.allowLinking}">
    <and>
      <istrue value="${tomcat.user.allowLinking}"/>
      <isset property="current.os.iswindows"/>
    </and>
  </condition>  
  <if>
    <bool>
      <and>
	<isset property="tomcat.user.allowLinking"/>
	<istrue value="${tomcat.user.allowLinking}"/>
      </and>
    </bool>
    <if><bool><isfalse value="${tomcat.allowLinking}"/></bool>
    <echo>NOTE:
      Although the tomcat.user.allowLinking property was set to true,
      overriding this to use false instead for security reasons,
      since the Windows OS has a case insensitive filesystem.
      </echo>    
      <else>
	<echo>WARNING:
	tomcat.user.allowLinking property was set to true.
	The tomcat server will be set to use this.
	But if you're on an operating system where the filesystem is case INsensitive,
	then ensure tomcat.user.allowLinking's value is set to false for security reasons.
	</echo>
      </else>
    </if>
  </if>
  
  <if>
    <bool><not><matches string="${server.protocols}" pattern="^\s*(https?|http\s*,\s*https|https\s*,\s*http)\s*$"/></not></bool>
    <fail>@@@@
      In file build.properties: invalid value for server.protocols.
      It must contain http or https, or both (in order of preference) separated by commas.
    </fail>
  </if>

  <property name="server.default.servlet" value="${greenstone.default.servlet}"/>
  <!-- 
       * "valid ports range from 1024–49151" is probably about user assignable ports.
       But 80 is a valid port for running web servers including GS
       and in future the range may become greater, so don't restrict the port range to 1024-49151.
       https://stackoverflow.com/questions/113224/what-is-the-largest-tcp-ip-network-port-number-allowable-for-ipv4
  -->
    <if>
      <bool>
	<not><matches string="${localhost.port.http}" pattern="^\d{2,}\s*$"/></not>
      </bool>
      <fail>...
	********* ERROR: localhost.port.http in file build.properties is set to an invalid port number.
	If port 80 is not possible, user assignable ports range from 1024–49151.
	But don't choose any port already in use by another application.
	Try setting to localhost.port.http=8383
      </fail>
    </if>

    <!-- if server.protocols contains https, ensure its port number is valid -->
    <if>
      <bool>
	  <and>
	    <matches string="${server.protocols}" pattern="https"/>
	    <not><matches string="${tomcat.port.https}" pattern="^\d{2,}\s*$"/></not>
	  </and>
      </bool>
      <fail>...
	********* ERROR: in file build.properties, server.protocols includes https but
	tomcat.port.https is set to an invalid port number.
	If port 443 is not possible, user assignable ports range from 1024–49151.
	But don't choose any port already in use by another application.
	Try setting tomcat.port.https=8443
      </fail>
    </if>

    <!-- 
	 Bail if https is enabled but the keystore password (keystore.pass property) is not set.
	 However, keystore.pass has no default value and is therefore not set as a rule.
	 So don't bail when 'ant' is run for the first time to create build.props from build.props.svn.
	 But do bail if running ant.prepare and https enabled and password not set.
	 (Maybe put this entire section before the first target: so we only bail after all non-targets
	 are executed so that any other first ever initialisation is completed?)
    -->
    <if>
      <bool>
	<and>
	  <matches string="${server.protocols}" pattern="https"/>
	  <or>
	    <not><isset property="keystore.pass"/></not>
	    <matches string="${keystore.pass}" pattern="^\s*$"/>
	  </or>
	</and>
      </bool>
      <if>
	<bool><isset property="first.run"/></bool>
	<echo>IMPORTANT: In file build.properties, when property server.protocols includes https,
	as now, the keystore.pass property must be set to a non-empty value.
	Either set server.protocols to http by itself, if you don't want support for https,
	or set keystore.pass if you do want https support.</echo>
	<else>
	  <fail>...
	  ********* ERROR: server.protocols contains https in file build.properties, but the keystore.pass
	  property required for obtaining/renewing the https certificate is not set or is set to the empty
	  string. Choose a password for keystore.pass and set it in build.properties before proceeding.
	  </fail>
	</else>
      </if>
    </if>

    <!-- Set the keystore file name for linux versus windows. Ultimately unused/inactive if HTTPS
	 isn't enabled and no certificate obtained. We don't yet have https certification on mac -->
    <condition property="keystore.file" value="fullchain_and_prvtkey.pfx" else="fullchain_and_prvtkey.p12">
      <istrue value="${current.os.iswindows}"/>
    </condition>	

    <!-- Originally, https redirectPort when using regular http port 8383
	 was always fixed at 8443. Now we use redirectPort=tomcat.port.https.
    -->
    <property name="https.redirect.port" value="${tomcat.port.https}"/>

    <!-- The default protocol to be used is the FIRST in the comma separated list for server.protocols 
    The default public tomcat port depends on the default server protocol
    -->
    <if>
      <bool><matches string="${server.protocols}" pattern="^https"/></bool>
      <property name="default.server.protocol" value="https"/>
      <property name="default.tomcat.port" value="${tomcat.port.https}"/>      
      <else>
	<property name="default.server.protocol" value="http"/>
	<property name="default.tomcat.port" value="${localhost.port.http}"/>	
      </else>
    </if>
    

    <!-- For setting filter tokens.
	 Used to set up server.xml when configuring tomcat -->
    <property name="comment.start" value="&lt;!--" />
    <property name="comment.end" value="--&gt;" />

    <!-- 
	 If https is enabled, regardless of whether it is the default protocol,
	 there's some more work to do:
	 -
	 - if https is not enabled, comment out its Connecter element in server.xml
    -->    
    <if>
      <bool><matches string="${server.protocols}" pattern="https"/></bool>      
      <property name="https.comment.out.start" value=""/>
      <property name="https.comment.out.end" value=""/>

      <else>	
	<property name="https.comment.out.start" value="${comment.start}"/>
	<property name="https.comment.out.end" value="${comment.end}"/>
      </else>
    </if>

    <!-- if server.protocols doesn't contain http, then http is only to be locally available,
	 e.g. for solr server on 127.0.0.1 
	 In that case, see https://serverfault.com/questions/218666/how-to-configure-tomcat-to-only-listen-to-127-0-0-1
    -->
    <condition property="restrict.http.to.local" value="false" else="true">
      <matches string="${server.protocols}" pattern="http($|\s+|,)"/>
    </condition>
    
    <condition property="http.address.restriction" value="address=&quot;127.0.0.1&quot;" else="">
      <istrue value="${restrict.http.to.local}"/>
    </condition>

    <!-- No certificates for localhost: https://letsencrypt.org/docs/certificates-for-localhost/ 
    But 'localhost' (or actually, 127.0.0.1) needed for solr: solr server not accessible to outside world 
    -->
    <condition property="local.http.url" value="http://${localhost.server.http}" else="http://${localhost.server.http}:${localhost.port.http}">
      <equals arg1="${localhost.port.http}" arg2="80" trim="true"/>
    </condition>

    <condition property="local.ws.url" value="ws://${localhost.server.http}" else="ws://${localhost.server.http}:${localhost.port.http}">
      <equals arg1="${localhost.port.http}" arg2="80" trim="true"/>
    </condition>

    <!-- Set global property for internal.servlet-url -->
    <property name="internal.servlet-url" value="${local.http.url}/${greenstone.context}${server.default.servlet}"/>
      
    <!-- Set global property for external.servlet-url -->
    <if>
      <bool><isset property="revproxy.domain"/></bool>
      
      <if>
	<bool><isset property="revproxy.opt_port"/></bool>
	<property name="revproxy.colon_opt_port" value=":${revproxy.opt_port}"/>
	<else>
	  <property name="revproxy.colon_opt_port" value=""/>
	</else>
      </if>
      <if>
	<bool><isset property="revproxy.context"/></bool>
	<property name="revproxy.app.path" value="/${revproxy.context}"/>
	<else>
	  <property name="revproxy.app.path" value="/${greenstone.context}"/>
	</else>
      </if>

      <property name="external.context-url" value="${revproxy.protocol}://${revproxy.domain}${revproxy.colon_opt_port}${revproxy.app.path}"/>
      <property name="external.servlet-url" value="${revproxy.protocol}://${revproxy.domain}${revproxy.colon_opt_port}${revproxy.app.path}${server.default.servlet}"/>
      <property name="external.webswing-url" value="${revproxy.protocol}://${revproxy.domain}${revproxy.colon_opt_port}/${webswing.context}"/>
      
      <else>
	<property name="external.context-url" value="${default.server.protocol}://${tomcat.server}:${default.tomcat.port}/${greenstone.context}"/>
	<property name="external.servlet-url" value="${default.server.protocol}://${tomcat.server}:${default.tomcat.port}/${greenstone.context}${server.default.servlet}"/>
	<property name="external.webswing-url" value="${default.server.protocol}://${tomcat.server}:${default.tomcat.port}/${webswing.context}"/>
      </else>
    </if>
    





	<!-- On linux, if testing https certification, pass in minus-minus-staging. If not testing on linux, nothing extra to pass in.
	     On windows or mac, if testing https certification, nothing extra to pass in. If not testing on windows or mac, pass in minus-minus-live.
	-->
	<if><bool><istrue value="${current.os.isunixnotmac}"/></bool>	
		<condition property="https.testing" value="" else="--staging">
			<isfalse value="${https.test.mode}"/>
		</condition>
	</if>
	<if><bool><or><istrue value="${current.os.iswindows}"/><istrue value="${current.os.ismac}"/></or></bool>
		<condition property="https.testing" value="--live" else="">
			<isfalse value="${https.test.mode}"/>
		</condition>
		<!-- Set bitness of windows OS, so we can use it for the zeroSSL executable.
			The Java system property os.arch will return x86 for 32 bit and AMD64 (or x64) for 64 bit OS.
			See https://stackoverflow.com/questions/20856694/how-to-find-the-os-bit-type -->
		<condition property="os.bitness" value="64" else="32">
			<matches string="${os.arch}" pattern="64"/>
		</condition>		 
	</if>

	<condition property="googlesignin.enabled" value="true" else="false">
	  <isset property="tomcat.googlesigninJDBCRealm.clientid"/>
	</condition>

  <!-- now we've read in properties, apply defaults - these will be used if there was no value yet -->
  <property name="disable.collection.building" value="false"/>
  <property name="oai.servlets" value="oaiserver"/> <!-- in case user has old version of build.properties-->
  <property name="greenstone.default.servlet" value="/library"/>
  <property name="servlet.properties.filenames" value="servlet.properties"/>
  
  <!-- get the filesets defining components and executables -->
  <import file="resources/xml/components.xml"/>
  <import file="resources/xml/executables.xml"/>
    
  <!-- version properties for external packages -->

  <property name="tomcat.version" value="apache-tomcat-9.0.108"/>
  <property name="tomcat.version.major" value="9"/>

  <!-- for new tomcat 10
 <property name="tomcat.version" value="apache-tomcat-10.1.57"/>
 <property name="tomcat.version.major" value="10"/>
 -->

  
  <!-- external access to the GS3 pages or not 
       https://tomcat.apache.org/tomcat-7.0-doc/config/valve.html -->
  <condition property="allowed.IPs" 
	     value=".*" 
	     else="(127\.0\.0\.1|::1|0:0:0:0:0:0:0:1)"> 
    <matches pattern="^(1|true|yes)$" string="${server.external.access}"/>
  </condition>

  

  <!-- these properties used for unzip-windows-packages -->
  <property name="sqlite.targz.version" value="sqlite-autoconf-3070602.tar.gz"/>
  <property name="expat.targz.version" value="expat-1.95.8.tar.gz"/>
  <property name="jdbm.targz.version" value="gs-jdbm-1.0.tar.gz"/>

  <property name="build.home" value="${basedir}/build"/>
  <property name="java.src.home" value="${basedir}/src/java"/>
  <property name="jarsupport.home" value="${basedir}/src/jar-support"/>

  <!-- this may be set in build.properties, e.g. if you move the web dir to 
       tomcats webapps directory -->
  <property name="web.home" value="${basedir}/web"/>
  <property name="gsdl3.writablebasedir" value="${basedir}"/>
  <property name="web.writablehome" value="${gsdl3.writablebasedir}/web"/>
  <property name="gsdl3home.isreadonly" value="false"/>
  <property name="data.home" value="${basedir}/data"/>
  <property name="data.writablehome" value="${gsdl3.writablebasedir}/data"/>
  <property name="ext.data.home" value="${data.home}/ext"/>
  <property name="ext.data.writablehome" value="${data.writablehome}/ext"/>
  
  <if><bool><istrue value="${gsdl3home.isreadonly}"/></bool>
    <property name="readonly-packages.home" value="${basedir}/packages"/>
    <property name="packages.home" value="${gsdl3.writablebasedir}/packages"/>
  <else>
    <property name="packages.home" value="${basedir}/packages"/>
  </else>
  </if>


  <!-- 
       The derby.system.home property can be used to set the same as JAVA_OPT
       when launching the derby networked server, so that the usersDB can be set to
       a shorter path in greenstone.xml and accessed with that shorter path through
       clients like Java code using a Client class, Squirreldb connecting and more.
       
       https://db.apache.org/derby/docs/10.3/tuning/rtunproper32066.html
       https://db.apache.org/derby/docs/10.13/devguide/cdevdvlp40350.html
       https://db.apache.org/derby/docs/10.0/manuals/tuning/perf91.html
       https://stackoverflow.com/questions/26624031/how-to-manually-specify-the-derby-system-home-property-to-start-a-derby-server
       but: https://zetcode.com/db/apachederbytutorial/tomcat/
       https://db.apache.org/derby/docs/10.4/adminguide/tadmincbdjhhfd.html
  -->
  <property name="derby.system.home" value="${web.writablehome}/etc"/>
  
  <!-- The following are useful in Java-based config files (.properties,.json,.xml)
       where the 'Unix style' (really URL style of slash, even in a file://...) is used -->
  <pathconvert targetos="unix" property="web.home.unix">
    <path path="${web.home}"/>
  </pathconvert>
  <pathconvert targetos="unix" property="web.writablehome.unix">
    <path path="${web.writablehome}"/>
  </pathconvert>
  <pathconvert targetos="unix" property="gsdl3.writablebasedir.unix">
    <path path="${gsdl3.writablebasedir}"/>
  </pathconvert>
 <pathconvert targetos="unix" property="data.writablehome.unix">           
    <path path="${data.writablehome}"/>                                     
  </pathconvert>                                                                
  
  <!-- If using a dispersed GS3 web folder, then the user web would not contain everything in the
       default GS3 web (it won't contain a CGI or lib folder inside WEB-INF, for instance) -->
  <!-- full.web.dir refers to the main web.dir, not web.writable - stuff gets
       copied from full.web.dir into web.writablehome -->
  <if>
    <bool><available file="${web.home}/WEB-INF/cgi" type="dir"/></bool>
    <property name="full.web.dir" value="${web.home}"/>
    <else>
      <property name="full.web.dir" value="${basedir}/web"/>
    </else>
  </if>  

  <!-- jar files needed by applets go here -->
  <property name="web.applet" value="${web.writablehome}/applet"/>
  
  <!-- jar files needed by the servlet (and extra ones) go here -->
  <property name="web.lib" value="${web.writablehome}/WEB-INF/lib"/>
  <!-- other files needed by the servlet go here -->
  <property name="web.classes" value="${web.writablehome}/WEB-INF/classes"/>

  <if>
    <bool><istrue value="${gsdl3home.isreadonly}"/></bool>
    <echo>Greenstone3 home directory is read-only</echo>
    <echo>  => Writable area is: ${gsdl3.writablebasedir}</echo>
    <echo>  => Web writable home: ${web.writablehome}</echo>
    <condition property="gsdl3.writablebasedir.already-exists">
      <available file="${gsdl3.writablebasedir}" type="dir"/>
    </condition>

    <if>
      <bool><istrue value="${gsdl3.writablebasedir.already-exists}"/></bool>
      <echo>Greenstone home writable area (${gsdl3.writablebasedir}) already exists</echo>
      <echo>  => Not setting it up again </echo>
      <echo>  => Delete the folder and run 'ant' if you would like it reset to default</echo>
      <else>

      <!-- set up writable area -->
      <echo>No previous Greenstone home writable area detected</echo>
      <echo>  => Setting up area</echo>
      <mkdir dir="${gsdl3.writablebasedir}"/>
      <mkdir dir="${gsdl3.writablebasedir}/web"/>
      <mkdir dir="${gsdl3.writablebasedir}/packages"/> 
      <mkdir dir="${gsdl3.writablebasedir}/web/logs"/>
      <mkdir dir="${gsdl3.writablebasedir}/web/ext"/>

      <chmod perm="a+rwx" dir="${gsdl3.writablebasedir}"/>
      <chmod perm="a+rwx" dir="${gsdl3.writablebasedir}/web"/>
      <chmod perm="a+rwx" dir="${gsdl3.writablebasedir}/packages"/>
      <chmod perm="a+rwx" dir="${gsdl3.writablebasedir}/web/logs"/> 
      <chmod perm="a+rwx" dir="${gsdl3.writablebasedir}/web/ext"/> 

      <!-- copy over packages tomcat folder -->

      <if>
	<bool><istrue value="${current.os.iswindows}"/></bool>
	<copy todir="${gsdl3.writablebasedir}/packages/tomcat" 
	      preservelastmodified="true"
	      failonerror="true" >  
	  <fileset dir="${readonly-packages.home}/tomcat" includes="**"/>  
	</copy>
      
	<else>
	  <!-- else assume Unix -->
	  <!-- Can't go through the OS-independent <copy> task as it fails to preserve exec permissions -->
	  <echo>Copying to ${gsdl3.writablebasedir}/packages/tomcat</echo>
	  <exec executable="cp" output="/dev/null" spawn="false">
            <arg value="-r"/>
            <arg value="${readonly-packages.home}/tomcat"/>
            <arg value="${gsdl3.writablebasedir}/packages/."/>
	  </exec>

	  <!-- the packages folder in tmp may only have read permissions at this stage, lets make sure it all has write permission -->
	  <chmod perm="a+w">
            <dirset dir="${gsdl3.writablebasedir}/packages" includes="**" />
            <fileset dir="${gsdl3.writablebasedir}/packages" includes="**" />
          </chmod>
	</else>
      </if>
      
      <echo>  => Copying Greenstone's web/WEB-INF to writable area</echo>
      <copy todir="${web.writablehome}/WEB-INF" 
	    preservelastmodified="true"
	    failonerror="true" >  
	<fileset dir="${full.web.dir}/WEB-INF" includes="**"/>  
      </copy>
      <echo>  => Copying Greenstone's web/ext to writable area</echo>
      <copy todir="${web.writablehome}/ext" 
	    preservelastmodified="true"
	    failonerror="true" >  
	<fileset dir="${full.web.dir}/ext" includes="**"/>  
      </copy>
      <!-- need to think about data folder wrt writablebasedir -->
      <!-- should it all go there, or just some? -->
      <echo>  => Copying Greenstone's data folder to writable area</echo>
      <copy todir="${gsdl3.writablebasedir}/data"
            preservelastmodified="true"
            failonerror="true" >
        <fileset dir="${basedir}/data" includes="**"/>
      </copy>
      

      <copy todir="${web.writablehome}" 
	    preservelastmodified="true"
	    failonerror="true" >  
	<fileset dir="${web.home}" includes="index.html"/>  
      </copy>
    </else>
    </if>
  </if>

  <!-- jni libraries and java wrappers go here -->
  <property name="lib.jni" value="${basedir}/lib/jni"/>

  <!-- other jar files needed for installation (but not runtime) go here -->
  <property name="lib.java" value="${basedir}/lib/java"/>
  
  <property name="javadocs" value="${basedir}/docs/javadoc"/>

  <property name="app.name"      value="greenstone3"/>
  <property name="app.path"      value="/${greenstone.context}"/>

   <property name="admin.dir"      value="${basedir}/admin"/>

  <!-- defaults - set these on the command line or in build.properties or 
       they will take these default values-->
  <property name="app.version" value="trunk"/>
  <property name="branch.path" value="trunk"/>
  <property name="branch.revision" value="HEAD"/>

  <!--constants -->
  <property name="svn.root" value="https://svn.greenstone.org"/>
  <!-- release kits need to set this to - -non-interactive - -trust-server-cert"
       in order to bypass having to manually accept the new SSL certificate
       every three months -->
  <property name="global-svn-args" value=""/>

  <property name="gsorg.root" value="https://www.greenstone.org"/>
  <!--<property name="gsorg.root" value="http://community.nzdl.org"/>-->

  <!-- catalina home is set to tomcat basedir if already installed, otherwise
       use greenstone's tomcat -->
  <condition property="catalina.home" value="${tomcat.installed.path}" else="${packages.home}/tomcat"> 
    <and>
      <isset property="tomcat.installed.path"/>
      <not>
	<equals arg1="" arg2="${tomcat.installed.path}"/>
      </not>
    </and>
  </condition>
 

  <!-- is there a better way to do this?? what about solaris?? -->
  <condition property="os.bin.dir" value="${cross.os}">
      <istrue value="${compile.cross}"/>
  </condition>
  <condition property="os.bin.dir" value="windows">
    <os family="windows"/>
  </condition>
  <condition property="os.bin.dir" value="darwin">
    <os family="mac"/>
  </condition>
  <condition property="os.bin.dir" value="linux">
    <and>
      <os family="unix"/>
      <not>
	<os family="mac"/>
      </not>
    </and>
  </condition>

  
  <condition property="collection.building.disabled">
    <and>
      <isset property="disable.collection.building"/>
      <istrue value="${disable.collection.building}"/>
    </and>
  </condition>

  <condition property="collection.building.enabled">
    <not>
      <istrue value="${disable.collection.building}"/>
    </not>
  </condition>
  
  <condition property="collection.building.enabled.windows">
    <and>
      <istrue value="${collection.building.enabled}"/>
      <isset property="current.os.iswindows"/>
    </and>
  </condition>
  
  <condition property="collection.building.enabled.unix">
    <and>
      <istrue value="${collection.building.enabled}"/>
      <isset property="current.os.isunix"/>
    </and>
  </condition>

  <condition property="jni.enabled">
    <istrue value="${with.jni}"/>
  </condition>
  <condition property="webswing.enabled">
    <istrue value="${with.webswing}"/>
  </condition>
  <condition property="solr.enabled">
    <istrue value="${with.solr}"/>
  </condition>

  
  <condition property="static.arg" value="LDFLAGS=-static" else=" ">
    <isset property="compile.static"/>
  </condition>

  <condition property="opt.cross.build" 
	     value="--build=${cross.build}" else=" ">
    <isset property="cross.build"/>
  </condition>


  <condition property="cross.configure.args" 
	     value="--host=${cross.host} ${opt.cross.build} CPP=${cross.host}-cpp CC=${cross.host}-gcc CXX=${cross.host}-g++ LD=${cross.host}-ld AR=${cross.host}-ar RANLIB=${cross.host}-ranlib STRIP=${cross.host}-strip ${cross.configure.extraargs} crossOS=${cross.os}" else=" ">
    <istrue value="${compile.cross}"/>
  </condition>

  <!-- if we're told to work with gnome-lib, or if there's a gnome-lib-minimal that the user
       has placed in gs2build/ext, then compile gs2build after sourcing gnome-lib environment.
       But don't do anything with gnomelib on windows.
  -->
  <condition property="opt.gnomelibext.arg" 
	     value="--enable-gnome-lib-ext" else=" ">
    <and>
      <istrue value="${current.os.isunix}"/>
      <or>
	<available file="${gs2build.home}/ext/gnome-lib-minimal" type="dir"/>
	<istrue value="${use.gnomelib.ext}"/>
	<istrue value="${checkout.gnomelib.ext}"/>
      </or>
    </and>    
   </condition>

   <condition property="opt.wvware.arg" value=" " else="--disable-wvware ">
     <istrue value="${with.wvware}"/>
   </condition>

  <condition property="gs2.opt.args" value="${opt.gnomelibext.arg} ${opt.wvware.arg}" else="${opt.gnomelibext.arg} ${opt.wvware.arg} --disable-mg --disable-mgpp --disable-accentfold --disable-gdbm --disable-sqlite">
    <istrue value="${with.jni}"/>
  </condition>
  <condition property="gs2.compile.target" value="with-jni" else="without-jni">
    <istrue value="${with.jni}"/>
  </condition>
  <condition property="gs2.install.target" value="install-with-jni" else="install-without-jni">
    <istrue value="${with.jni}"/>
  </condition>
  <condition property="gs2.windows.enablejni" value="1" else="0">
    <istrue value="${with.jni}"/>
  </condition>
  <condition property="gs2.windows.enablemg" value="1" else="0">
    <istrue value="${with.jni}"/>
  </condition>
  <condition property="gs2.windows.enablemgpp" value="1" else="0">
    <istrue value="${with.jni}"/>
  </condition>
  <!-- Should accent folding not also be set here ?? -->
  <condition property="gs2.windows.usegdbm" value="1" else="0">
    <istrue value="${with.jni}"/>
  </condition>
  <condition property="gs2.windows.usesqlite" value="1" else="0">
    <istrue value="${with.jni}"/>
  </condition>
  
  <!-- where is search4j tool -->
  <condition property="search4j.exec" value="bin/search4j.exe" else="bin/search4j"> 
    <isset property="current.os.iswindows"/>
  </condition>

  
  <!-- ============= Base dirs for each package and component ============ -->
  <property name="src.gsdl3.home" value="${java.src.home}/org/greenstone/gsdl3"/>
  <property name="anttasks.home" value="${java.src.home}/org/greenstone/anttasks"/>
  <property name="gli.home" value="${basedir}/gli"/>
  <property name="javagdbm.home" value="${src.packages.home}/javagdbm"/>

  <condition property="common.src.home" value="${basedir}/common-src" else="${gs2build.home}${file.separator}common-src">
    <istrue value="${disable.collection.building}"/>
  </condition>
  <condition property="common.src.binhome" value="${basedir}" else="${gs2build.home}">
    <istrue value="${disable.collection.building}"/>
  </condition>

  <property name="build.src.home" value="${gs2build.home}/build-src"/>
  <property name="gdbm.home" value="${common.src.home}/packages/gdbm"/>
  <property name="mg.home" value="${common.src.home}/indexers/mg"/>
  <property name="mgpp.home" value="${common.src.home}/indexers/mgpp"/>
  <property name="lucene.home" value="${common.src.home}/indexers/lucene-gs"/>

  <!--  ==================== Compilation Control Options ==================== -->

  <!--

  These properties control option settings on the Javac compiler when it
  is invoked using the <javac> task.

  compile.debug        Should compilation include the debug option?

  compile.deprecation  Should compilation include the deprecation option?

  compile.optimize     Should compilation include the optimize option?

  -->

  <property name="compile.debug"       value="true"/>
  <property name="compile.deprecation" value="true"/>
  <property name="compile.optimize"    value="true"/>
  <property name="compile.encoding"    value="UTF8"/>
  <property name="compile.includeantruntime" value="false"/> <!-- to get rid of annoying 'ant' warning -->
  
  <!--

  Rather than relying on the CLASSPATH environment variable, Ant includes
  features that makes it easy to dynamically construct the classpath you
  need for each compilation.  The example below constructs the compile
  classpath to include the servlet.jar file, as well as the other components
  that Tomcat makes available to web applications automatically, plus anything
  that you explicitly added.

  -->
  
  <!-- All elements that Tomcat exposes to applications -->
  <path id="tomcat-jars">
    <fileset dir="${catalina.home}/lib">
      <include name="*.jar"/>
    </fileset>
  </path>

  
  <path id="compile.classpath">
    <!-- Include all jar files and libraries in our jni lib directory -->
    <pathelement location="${lib.jni}"/>
    <fileset dir="${lib.jni}">
      <include name="*.jar"/>
    </fileset>
    <!-- Include all jar files in our web lib directory -->
    <pathelement location="${web.lib}"/>
    <fileset dir="${web.lib}">
      <include name="*.jar"/>
    </fileset>
    
    <pathelement location="${lib.java}"/>
    <fileset dir="${lib.java}">
      <include name="*.jar"/>
    </fileset>
    
    <!-- include the jar files from the source packages -->
    <!-- mg and mgpp get installed into lib/jni but they may not be there yet
    so we add them in by name -->
    <pathelement location="${lib.jni}/mg.jar"/>
    <pathelement location="${lib.jni}/mgpp.jar"/>

    <pathelement location="${basedir}/ext/webswing/web/api/webswing-api.jar"/>      
    <!-- Include all elements that Tomcat exposes to applications -->
    <path refid="tomcat-jars"/>
    
  </path>

  <path id="running.classpath">
    <path refid="compile.classpath"/>
    <pathelement location="${web.classes}"/>
  </path>
  
  <path id="derby.server.classpath">
    <pathelement location="${web.lib}/derbynet.jar"/>
    <pathelement location="${web.lib}/derby.jar"/>
  </path>


  <target name="check-resources-timestamps">
    <!-- top level properties files -->
    <for list="build.properties.svn,servlet.properties.svn" param="svnfile">
      <sequential>
        <local name="livefile"/>
        <local name="isUpToDate"/>
        <pathconvert property="livefile">
          <path>
            <pathelement location="@{svnfile}"/>
          </path>
          <mapper type="glob" from="*.svn" to="*"/>
        </pathconvert>
        <if><bool><available file="${livefile}"/></bool>
        <echo>Checking ${livefile} against @{svnfile}</echo>
        <uptodate property="isUpToDate" targetfile="${livefile}" srcfile="@{svnfile}"/>
        <fail message="ERROR: @{svnfile} is newer than ${livefile}. Please check for changes, then edit ${livefile}, or 'touch' it to set its timestamp.">
        <condition>
          <not>
            <isset property="isUpToDate"/>
          </not>
        </condition>
        </fail>
        <else>
          <echo>${livefile} not found, generating it from @{svnfile}</echo>
          <copy file="@{svnfile}" tofile="${livefile}"/>
        </else>
        </if>
      </sequential>
    </for>
        <!-- resources .svn files -->
    <for param="svnfile">
      <path>
        <fileset dir="${basedir}" includes="resources/**/*.svn"/>
      </path>

      <sequential>
        <!-- define these properties to be locally scoped, 
             otherwise they won't change once set -->
        <local name="infile"/>
        <local name="isUpToDate"/>
        <!-- Compute corresponding .svn filename -->
        <pathconvert property="infile">
          <path>
            <pathelement location="@{svnfile}"/>
          </path>
          <mapper>
            <globmapper from="*.svn" to="*.in"/>
          </mapper>
        </pathconvert>
        <!-- Check if .in is up-to-date compared to .svn -->
        <!-- also check if there actually is an svn file -->
        <if><bool><available file="${infile}"/></bool>
        <echo>Checking ${infile} against @{svnfile}</echo>
          <uptodate property="isUpToDate" targetfile="${infile}" srcfile="@{svnfile}"/>
          <fail message="ERROR: @{svnfile} is newer than ${infile}. Please check for changes, then edit the .in file, or 'touch' it to set its timestamp.">
            <condition><not>
              <isset property="isUpToDate"/>
            </not>
            </condition>
          </fail>
          <else>
            <echo>${infile} not found, generating it from @{svnfile}</echo>
          <copy file="@{svnfile}" tofile="${infile}"/>
          </else>
        </if>
        <!-- cleanup -->
        <!--<unset property="svnfile"/>
        <unset property="isUpToDate"/>-->
      </sequential>
    </for>

    <echo message="✅ All .in files in resources are up to date with their .svn counterparts."/>
  </target>

  <target name="check-java-home">
    <echo>using JAVA_HOME : ${env.JAVA_HOME}</echo>

    <condition property="java.home.set">
        <isset property="env.JAVA_HOME"/>
    </condition>
    <fail unless="java.home.set" message="JAVA_HOME was not set. Maybe you would like to get a self contained jdk by cd'ing to the  ext-cli folder and running get-selfcontained-jdk.sh/bat. Otherwise, please make sure your JAVA_HOME is set to the installation of a JDK 1.8 or later. You could set this in local/gs3-setup.sh/bat to avoid having to set it on the command line each time."/>
  </target>

  <target name="test-setup" depends="needs-gs3-setup, check-java-home">
    
    <echo>ant.version   = ${ant.version}</echo>
    <echo>java.version  = ${java.specification.version} (specification version)</echo>
    <echo></echo>
    <echo>is unix       : ${current.os.isunix}</echo>
    <echo>is mac        : ${current.os.ismac}</echo>
    <echo>is unixnotmac : ${current.os.isunixnotmac}</echo>
    <echo>is windows    : ${current.os.iswindows}</echo>
    <echo>os.unix       : ${os.unix}</echo>
    <echo>env.PATH     : ${env.PATH}</echo>
    <!-- Windows can be case sensitive about env.PATH, preferring env.Path. See https://ant.apache.org/manual/Tasks/property.html  -->
    <echo>env.Path     : ${env.Path}</echo> 
    <echo>env.GSDLOS   : ${env.GSDLOS}</echo>
    
  </target>

  <target name="needs-gs3-setup">

    <exec os="${os.unix}" executable="bash"  dir="${basedir}" failonerror="true" output="${gsdl3.writablebasedir}/gs3-setupenv.properties">
      <arg value="-c" />
      <arg value=". ./gs3-setup.sh >/dev/null 2> ${gsdl3.writablebasedir}/gs3-setupenv.errors ; env" />
    </exec>
    <exec osfamily="windows" executable="cmd" dir="${basedir}" failonerror="true" output="${gsdl3.writablebasedir}/gs3-setupenv.properties">
      <arg value="/c" />
      <arg value=".\gs3-setup.bat >nul 2> ${gsdl3.writablebasedir}/gs3-setupenv.errors &amp;&amp; set"/>
    </exec>          

    <!-- Need to escape any backslashes (typically happens on Windows with file/dir paths) so can be parsed in correctly by 
	 property file read, otherwise can trigger errors such as Malformed \uXXXX -->
    <rsr verbosity="1" file="${gsdl3.writablebasedir}/gs3-setupenv.properties" pattern="\\" replacement="\\\\" />

    <!-- acts as a replacement for <property environment="env"/> -->
    <property file="${gsdl3.writablebasedir}/gs3-setupenv.properties" prefix="env"/> 
    
    <!-- Delete the error output file if it is empty -->
    <delete failonerror="false">
      <fileset dir="${gsdl3.writablebasedir}" includes="gs3-setupenv.errors">
	<size value="0" when="equal"/>
      </fileset>
    </delete>

    <!-- has the gs3-setup been sourced successfully -->
    <condition property="gs3-setup-not-done">
      <not>
	<isset property="env.GSDL3HOME"/>
      </not>
    </condition>

    <fail if="gs3-setup-not-done" message="Failed to run 'gs3-setup' (Windows) or '. ./gs3-setup.sh' (Unix) before starting the Greenstone server."/>    
  </target>

  <target name="needs-gs3-devel">
    <if>
      <!-- only need to source gs3-devel if not already done so -->
      <bool><not><isset property="gs3-devel-done"/></not></bool>
      
      <exec os="${os.unix}" executable="bash"   dir="${basedir}" failonerror="true" output="${gsdl3.writablebasedir}/gs3-develenv.properties">
	<arg value="-c" />
	<arg value=". ./gs3-devel.sh >/dev/null 2> ${gsdl3.writablebasedir}/gs3-develenv.errors ; env" />
      </exec>
      <exec osfamily="windows" executable="cmd" dir="${basedir}" failonerror="true" output="${gsdl3.writablebasedir}\gs3-develenv.properties">
	<arg value="/c" />
	<arg value="gs3-devel.bat >nul 2>${gsdl3.writablebasedir}\gs3-develenv.errors &amp;&amp; set" />
      </exec>          

      <!-- Need to escape any backslashes (typically happens on Windows with file/dir paths) so can be parsed in correctly by 
	   property file read, otherwise can trigger errors such as Malformed \uXXXX -->
      <rsr verbosity="1" file="${gsdl3.writablebasedir}/gs3-develenv.properties" pattern="\\" replacement="\\\\" />

      <!-- acts as a replacement for <property environment="env"/> -->      
      <property file="${gsdl3.writablebasedir}/gs3-develenv.properties" prefix="env"/>
      <!-- Delete the error output file if it is empty -->
      <delete failonerror="false">
	<fileset dir="${gsdl3.writablebasedir}" includes="gs3-develenv.errors">
	  <size value="0" when="equal"/>
	</fileset>
      </delete>

      <!-- has the gs3-setup been sourced successfully -->
      <condition property="gs3-devel-done">
	<isset property="env.GSDL3HOME"/>
      </condition>

      <if>
	<bool><isset property="env.JAVACFLAGS"/></bool>
	<property name="compile.javac.flags" value="${env.JAVACFLAGS}"/>
	<else>
	  <property name="compile.javac.flags" value=""/>
	</else>	
      </if>
    </if>
    
  </target>

  <target name="get-ipv4">
      <!-- Work out the IPv4 address for this machine -->
      <exec executable="${basedir}/bin/script/IPv4.sh" os="${os.unix}" failonerror="false" outputproperty="ipv4.val">
        <arg value="-format-for-tomcat-context"/>
      </exec>
      <exec executable="${basedir}/bin/script/IPv4.bat" osfamily="windows" failonerror="false" outputproperty="ipv4.val">
        <arg value="-format-for-tomcat-context"/>
      </exec>
  </target>
  
  <target name="envbased-devel-props" depends="needs-gs3-devel" if="current.os.iswindows">
    <if>
      <bool><isset property="env.VisualStudioVersion"/></bool>
      <property name="visualstudio.majorversion" value="${env.VisualStudioVersion}"/>
      <else>
	<fail message="Please setup your Visual Studio environment by editing local\gs3-devel.bat and setting the correct paths. Note, if you are using a version of Visual Studio prior to VS14, you may need to set the VisualStudioVersion environment variable in that file too"/>
      </else>
    </if>
  </target>

  <target name="envbased-localtomcat-path" depends="needs-gs3-setup">
	<if><bool><isset property="env.PATH"/></bool>
	  <path id="local.tomcat.path">
		<pathelement location="${basedir}/bin/script"/>
		<pathelement location="${basedir}/bin"/>
		<pathelement location="${lib.jni}"/>	
		<pathelement path="${env.PATH}"/>
		<pathelement path="${wn.home}/bin"/>
	  </path>
	  <!-- Windows can be case sensitive about env.PATH, preferring env.Path. See https://ant.apache.org/manual/Tasks/property.html  -->
	  <else><if><bool><isset property="env.Path"/></bool>
		<path id="local.tomcat.path">
		  <pathelement location="${basedir}/bin/script"/>
		  <pathelement location="${basedir}/bin"/>
		  <pathelement location="${lib.jni}"/>
		  <pathelement path="${env.Path}"/>
		  <pathelement path="${wn.home}/bin"/>
		</path>
	  <!-- else print error about no path set -->
	  <else>
		<echo>No env.PATH (or env.Path) set. Unable to set local.tomcat.path property</echo>
	  </else></if></else>	
	</if>

	<path id="local.tomcat.classpath">
	  <!-- explicitly include the jni java wrappers in the classpath -->
	  <pathelement location="${lib.jni}"/>
	  <fileset dir="${lib.jni}">
	    <include name="*.jar"/>
	  </fileset>
	  
	  <pathelement location="${web.lib}"/>
	  <fileset dir="${web.lib}">
	    <include name="derbyclient.jar"/> <!--<include name="derby.jar"/>-->
	  </fileset>
	</path>
	
  </target>
  
  <target name="envbased-gnomelib-allargs-prop" depends="needs-gs3-devel">

    <!-- If building a release then we want to adjust environment variables so that the support library can be seen during compilation -->
    <if>
        <bool><isset property="use.gnomelib.ext"/></bool>
	<property name="gnome-lib-dir" value="${basedir}/ext/gnome-lib-minimal/${os.bin.dir}"/>

	<if><bool><isset property="env.CFLAGS"/></bool>
		<property name="cflags.arg" value="CFLAGS=&quot;-I${gnome-lib-dir}/include -I${gnome-lib-dir}/include/libxml2 ${env.CFLAGS}&quot;"/>
	<else>
		<property name="cflags.arg" value="CFLAGS=&quot;-I${gnome-lib-dir}/include -I${gnome-lib-dir}/include/libxml2&quot;"/>
	</else>
	</if>

	<if><bool><isset property="env.CPPFLAGS"/></bool>
		<property name="cppflags.arg" value="CPPFLAGS=&quot;-I${gnome-lib-dir}/include -I${gnome-lib-dir}/include/libxml2 ${env.CPPFLAGS}&quot;"/>
	<else>
		<property name="cppflags.arg" value="CPPFLAGS=&quot;-I${gnome-lib-dir}/include -I${gnome-lib-dir}/include/libxml2&quot;"/>
	</else>
	</if>

	<if><bool><isset property="env.CXXFLAGS"/></bool>
		<property name="cxxflags.arg" value="CXXFLAGS=&quot;-I${gnome-lib-dir}/include -I${gnome-lib-dir}/include/libxml2 ${env.CXXFLAGS}&quot;"/>
	<else>
        	<property name="cxxflags.arg" value="CXXFLAGS=&quot;-I${gnome-lib-dir}/include -I${gnome-lib-dir}/include/libxml2&quot;"/>
	</else>
	</if>

	<!--https://groups.google.com/forum/#!topic/openkinect/m-hfXeYwKtU
	  compiling up libwv on ElCapitan needs -framework CoreFoundation -->
	<if><bool><isset property="env.LDFLAGS"/></bool>
		<property name="ldflags.arg" value="LDFLAGS=&quot;-L${gnome-lib-dir}/lib ${env.LDFLAGS}&quot;"/>
	<else>
        	<property name="ldflags.arg" value="LDFLAGS=&quot;-L${gnome-lib-dir}/lib&quot;"/>
	</else>
	</if>

	<if><bool><isset property="env.PATH"/></bool>
		<property name="path.arg" value="PATH=&quot;${gnome-lib-dir}/bin:${env.PATH}&quot;"/>
	<else>
        	<property name="path.arg" value="PATH=&quot;${gnome-lib-dir}/bin&quot;"/>
	</else>
	</if>

	<if><bool><isset property="env.PKG_CONFIG_PATH"/></bool>
		<property name="pcpath.arg" value="PKG_CONFIG_PATH=&quot;${gnome-lib-dir}/lib/pkgconfig:${env.PKG_CONFIG_PATH}&quot;"/>
	<else>
        	<property name="pcpath.arg" value="PKG_CONFIG_PATH=&quot;${gnome-lib-dir}/lib/pkgconfig&quot;"/>
	</else>
	</if>

	<if><bool><equals arg1="${os.bin.dir}" arg2="darwin"/></bool>
	  <!--<if><bool><isset property="env.DYLD_LIBRARY_PATH"/></bool>
	      <property name="ldlpath.arg" value="DYLD_LIBRARY_PATH=&quot;${gnome-lib-dir}/lib:${env.DYLD_LIBRARY_PATH}&quot;"/>
	      <else>
		<property name="ldlpath.arg" value="DYLD_LIBRARY_PATH=&quot;${gnome-lib-dir}/lib&quot;"/>
              </else>
	      </if>
            -->
	  <property name="ldlpath.arg" value="DYLD_LIBRARY_PATH=&quot;${env.DYLD_LIBRARY_PATH}&quot;"/>
	<else>
		<if><bool><isset property="env.LD_LIBRARY_PATH"/></bool>
			<property name="ldlpath.arg" value="LD_LIBRARY_PATH=&quot;${gnome-lib-dir}/lib:${env.LD_LIBRARY_PATH}&quot;"/>
		<else>
			<property name="ldlpath.arg" value="LD_LIBRARY_PATH=&quot;${gnome-lib-dir}/lib&quot;"/>
		</else>
		</if>
	</else>
	</if>


	<else>
		<if><bool><isset property="env.CFLAGS"/></bool>
			<property name="cflags.arg" value="CFLAGS=&quot;${env.CFLAGS}&quot;"/>
        	<else>
			<property name="cflags.arg" value=" "/>
		</else>
		</if>

		<if><bool><isset property="env.CPPFLAGS"/></bool>
        		<property name="cppflags.arg" value="CPPFLAGS=&quot;${env.CPPFLAGS}&quot;"/>
        	<else>
			<property name="cppflags.arg" value=" "/>
		</else>
		</if>

		<if><bool><isset property="env.CXXFLAGS"/></bool>
			<property name="cxxflags.arg" value="CXXFLAGS=&quot;${env.CXXFLAGS}&quot;"/>
		<else>
			<property name="cxxflags.arg" value=" "/>
		</else>
		</if>

		<if><bool><isset property="env.LDFLAGS"/></bool>
			<property name="ldflags.arg" value="LDFLAGS=&quot;${env.LDFLAGS}&quot;"/>
		<else>
			<property name="ldflags.arg" value=" "/>
		</else>
		</if>

		<if><bool><isset property="env.PATH"/></bool>
			<property name="path.arg" value="PATH=&quot;${env.PATH}&quot;"/>
		<else>
			<property name="path.arg" value=" "/>
		</else>
		</if>

		<if><bool><isset property="env.PKG_CONFIG_PATH"/></bool>
			<property name="pcpath.arg" value="PKG_CONFIG_PATH=&quot;${env.PKG_CONFIG_PATH}&quot;"/>
		<else>
			<property name="pcpath.arg" value=" "/>
		</else>
		</if>

		<if><bool><equals arg1="${os.bin.dir}" arg2="darwin"/></bool>
			<if><bool><isset property="env.DYLD_LIBRARY_PATH"/></bool>
				<property name="ldlpath.arg" value="DYLD_LIBRARY_PATH=&quot;${env.DYLD_LIBRARY_PATH}&quot;"/>
			<else>
				<property name="ldlpath.arg" value=" "/>
			</else>
			</if>
		<else>
			<if><bool><isset property="env.LD_LIBRARY_PATH"/></bool>
				<property name="ldlpath.arg" value="LD_LIBRARY_PATH=&quot;${env.LD_LIBRARY_PATH}&quot;"/>
			<else>
				<property name="ldlpath.arg" value=" "/>
			</else>
			</if>
		</else>	 
		</if>
	</else>
    </if>
    <property name="allargs" value="${cflags.arg} ${cxxflags.arg} ${cppflags.arg} ${ldflags.arg} ${path.arg} ${ldlpath.arg} ${pcpath.arg}"/>
  </target>
  
  <!-- Appends the current env to the file environment.txt. For debugging env vars used by the release-kit. -->
  <target name="write-env" description="Writes out the environment that this build.xml is executed in to file environment.txt. For debugging.">
    <echo message="*****************ENVIRONMENT OUTPUT:****************${line.separator}" file="environment.txt" append="true" />

    <if><bool><istrue value="${current.os.iswindows}"/></bool>
      <exec executable="cmd" dir="${basedir}" failonerror="false" output="environment.txt" append="true">      
	<arg value="/c" />
	<arg value="set" />
      </exec>
      <else>
	<exec executable="env" dir="${basedir}" failonerror="false" output="environment.txt" append="true" />
      </else>
    </if>

    <echo message="${line.separator}" file="environment.txt" append="true" />
    <echo>Check file environment.txt for environment settings written out for debugging.</echo>
  </target>

  
  <!-- ========= Target Group: Primary and Global Targets ======================== -->

  <target name="prepare" depends="accept-properties,init,needs-gs3-devel,envbased-devel-props,copy-dot-svn-files,prepare-core,prepare-packages,prepare-wget-for-windows,prepare-common-src,prepare-collection-building,prepare-tomcat,prepare-web,prepare-collections,prepare-binaries"
    description="Use this when you first checkout the code: 'ant prepare install'. This will do some additional subversion checkouts and downloads, so you need to be online to run this.">

    <!-- make sure .sh files are executable -->
    <chmod dir="${basedir}" perm="ugo+rx" 
      includes="*.sh"/>
    <chmod dir="${basedir}/bin/script" perm="ugo+rx" 
	   includes="*.sh,*.pl"/>
    <echo>To compile up and install Greenstone3, enter:</echo>
    <echo>  ant install</echo>
  </target>

  <!-- install-common-src and install-collection-building are mutually exclusive and either one or the other will be done depending on whether collection building is enabled or not -->
  <!--before configuring build-src, make sure that gnome-lib is compiled up-->
  <target name="install" depends="init,needs-gs3-devel,envbased-devel-props,check-java-home,check-resources-timestamps,compile-imagemagick,compile-gnome-lib,install-common-src,install-collection-building,install-webswing-ext,install-runtime,install-solr-ext,install-gli-for-webswing,setup-for-eclipse,init-oaiconfigs"
    description="Install Greenstone 3. Use this when you first checkout the code: 'ant prepare install'.">
	<echo>After successful installation, you can turn on https support on Windows and Linux</echo>
	<echo>by editing build.properties and running 'ant setup-https-cert'</echo>
	<echo>(requires system admin rights)</echo>
  </target>


  <target name="update" depends="init,svnupdate,clean,install"
    description="Update (thru Subversion) all the source (including common-src or collection-building, and runtime), then clean, and re-install. To do this without any SVN updates, run it like 'ant -Dnosvn.mode=yes update'"/>



  <target name="svnupdate" depends="init,svnupdate-runtime,svnupdate-common-src,svnupdate-collection-building,svnupdate-web"
    description="Do a `svn update` for all sources. Doesn't recompile the code. You need to be online to run this."/>
  
  <target name="configure" depends="needs-gs3-setup,init,configure-tomcat,configure-servlet-xml-files,configure-web,configure-solr-ext,configure-webswing-ext,init-oaiconfigs,configure-oaiconfigs"
          description="Configure the installation (not the C++ code). Includes setting up config files. Should be re-run if you change the build.properties file, including if you change the port number.">

    <property name="url" value="${default.server.protocol}://${tomcat.server}:${default.tomcat.port}${app.path}/"/>
    
    <available file="${basedir}/resources/iiif/IIIFConfig.xml.in" property="iiifconfig.present"/>
    <antcall target="init-iiifconfig">
      <param name="url" value="${url}"/>
    </antcall>
  </target> 

  <target name="clean" depends="init,clean-runtime,clean-common-src,clean-collection-building,clean-imagemagick,clean-gnome-lib,clean-solr-ext"
    description="Remove all old compiled code. Includes runtime, extensions and collection-building if necessary"/>
  
  <target name="distclean" depends="init,distclean-runtime,distclean-common-src,distclean-collection-building,clean-solr-ext"
    description="Remove all compiled code and also any Makefiles etc generated during configure-c++. Includes runtime, extensions and collection-building as necessary"/>
 
  


  <target name="perl-for-building" depends="needs-gs3-setup,init">
    <condition property="os.bitness" value="64" else="32">
      <matches string="${os.arch}" pattern="64"/>
    </condition>
    <!-- uses perl.path if set in build.properties, otherwise try for
         environment variable PERLPATH, and failing that assumes 'perl' 
         is on environment PATH -->
    <!-- if, outside build.properties, we only set perl.path if we have gs2build/build-src (collection building), then we won't set perl.path in a binary, since it doesn't have build-src -->
    <if><bool><available file="${gs2build.home}"/></bool>

      <if><bool><not><isset property="perl.path"/></not></bool>

        <if>
          <bool>
            <and>
              <isset property="env.PERLPATH"/>
              <not><equals arg1="${env.PERLPATH}" arg2=""/></not>
            </and>
          </bool>
		  <!-- set perlpath to env.PERLPATH. 
		       This is the path to perl\bin that's found by findperl.bat/sh when the server is launched 
			through GLI instead of the console.
			This env.PERLPATH is the bin folder and needs a trailing slash, since it 
			will appear suffixed with "perl(.exe)" in the perl setting in packages\tomcat\config\web.xml. 
			This web.xml's path to perl is then used to find perl when running the perl scripts in the 
			cgi folder, so without a slash appended at this point it becomes "binperl" in web.xml, and 
			things will break when GLI launches the server and the online GS3 metadata editor 
			is used	to save user-edited metadata. 
			A trailing slash is needed on unix too now, since we will now be bundling a
			perl (perl-with-cpan specifically) for all supported OS. 
		  -->		  
		  <property name="perl.path" value="${env.PERLPATH}${file.separator}"/>
		  
          <else>
            <echo>
  Using PATH environment variable to locate Perl.
            </echo>
            <exec executable="which" os="${os.unix}" spawn="false" outputproperty="full.perl.path">
              <arg value="perl" />
            </exec>

	    <exec executable="${gs2build.home}/bin/windows/which" osfamily="windows" spawn="false" outputproperty="full.perl.path">
              <arg value="perl" />
            </exec>
            <stringutil string="${full.perl.path}" property="partial.perl.path">
                <replace regex="\/[^\/]*$" replacement="/" />
            </stringutil>
	    <stringutil string="${partial.perl.path}" property="perl.path">
                <replace regex="\\[^\\]*$" replacement="\\" />
            </stringutil>	    
            <if><bool><istrue value="${current.os.isunix}"/></bool>
	      <property name="perl.cpan.bitness" value="${os.bitness}"/>
	      <property name="bin.os.perl" value="${gs2build.home}/bin/${os.bin.dir}/perl/bin/"/>
	      <property name="ext.cli.perl" value="${basedir}/ext-cli/selfcontained-perl-with-cpan/gsperl5-with-cpan-${os.bin.dir}${perl.cpan.bitness}/bin/"/>
	    <else>
	      <property name="bin.os.perl" value="${gs2build.home}\bin\windows\strawberry-perl\perl\bin\"/>
	      <property name="ext.cli.perl" value="${basedir}\ext-cli\selfcontained-perl-with-cpan\gsperl5-with-cpan-windows32\perl\bin\"/>
	    </else>
	    </if>
            <if><bool><not><or>
	      <equals arg1="${perl.path}" arg2="${bin.os.perl}"/>
	      <equals arg1="${perl.path}" arg2="${ext.cli.perl}"/>
	    </or></not></bool>
            <echo>
	      *** Beware. Proceeding with the perl currently found: ${perl.path}
	      even though this is not the Greenstone-compatible perl-with-cpan
	      (did not match ${bin.os.perl}
	      nor ${ext.cli.perl})
	      
	      *** Note Greenstone is now designed to operate with a Perl that comes
	      pre-bundled with the CPAN packages that are necessary for collection building.
	      A convenient way for developers to obtain this perl-with-cpan is to
	      cd into your ext-cli folder and run: get-self-contained-perl-with-cpan.bat/.sh.
            </echo>
            </if>	    
          </else>
        </if>
      </if>

      <!-- full.perl.path is for pretty-printing only, e.g. used in target "start" -->
      <if><bool><isset property="perl.path"/></bool>
        <property name="full.perl.path" value="${perl.path}perl"/>
        <else>
          <property name="full.perl.path" value="perl (will use the enviroment variable PATH to find this executable)"/>
        </else>
      </if>

      <!-- gs2build not available, perl.path -->
      <else>
	<property name="perl.path" value=""/>
      </else>
    </if>
    <stringutil string="${perl.path}" property="escaped.perl.path">
      <replace regex="\\" replacement="\\\\" />
    </stringutil>

    <!--<echo>@@@ Final perl.path determined: ${perl.path}</echo>-->
  </target>
  
  <target name="get-revproxy-servlet-url">
    <!-- the following is now globally set earlier in file -->
    <!--
    <if><bool><isset property="revproxy.opt_port"/></bool>
    <property name="revproxy.colon_opt_port" value=":${revproxy.opt_port}"/>
    <else>
      <property name="revproxy.colon_opt_port" value=""/>
    </else>
    </if>
    <if><bool><isset property="revproxy.context"/></bool>
    <property name="revproxy.app.path" value="/${revproxy.context}"/>
    <else>
      <property name="revproxy.app.path" value="${app.path}"/>
    </else>
    </if>

    
    <echo>${revproxy.protocol}://${revproxy.domain}${revproxy.colon_opt_port}${revproxy.app.path}${server.default.servlet}</echo>
    -->
    <echo>${external.servlet-url}</echo>
  </target>

  <target name="get-default-servlet-url">
    <echo>${default.server.protocol}://${tomcat.server}:${default.tomcat.port}${app.path}${server.default.servlet}</echo>
  </target>

  
  <!-- returns the base local URL, something like HTTP://127.0.0.1:<HTTPport>
       or some sane equivalent for 127.0.0.1 -->
  <target name="get-local-base-http-url">
    <echo>${local.http.url}</echo>
  </target>
  <!-- Returns something like HTTP://127.0.0.1:<HTTPport>/greenstone3/library -->
  <target name="get-local-http-servlet-url">
    <echo>${local.http.url}${app.path}${server.default.servlet}</echo>
  </target>

  <target name="get-internal-servlet-url">
    <!-- The following is now resolved earlier on (top-level), and set as a property -->
    <!--
    <antcall target="get-local-http-servlet-url" />
    -->
    <echo>${internal.servlet-url}</echo>
  </target>

  <target name="get-external-servlet-url">
    <!-- The following is now resolved earlier on (top-level), and set as a property -->
    <!--
    <if><bool><isset property="revproxy.domain"/></bool>
      <antcall target="get-revproxy-servlet-url"/>
      <else>
	<antcall target="get-default-servlet-url"/>
      </else>
    </if>
    -->
    <echo>${external.servlet-url}</echo>
  </target>

  <!-- ======= Target Group: Starting and stopping (and checking running) targets ================ -->

  <!-- the main ones are start, stop, restart, status -->
  
  <target name="start" depends="needs-gs3-setup,init,check-resources-timestamps,perl-for-building,check-tomcat-started,check-derbyserver-started"
          description="Startup the Tomcat, Derby and SOLR (if enabled) servers." >
    <if><bool><istrue value="${tomcat.isstarted}"/></bool>
      <echo>**************************************</echo>
      <echo>A WEB SERVER IS ALREADY RUNNING ON ${default.server.protocol}://${tomcat.server}:${default.tomcat.port} (${local.http.url}). NOT STARTING.</echo>
      <echo>**************************************</echo>
      <else>
        <if><bool><istrue value="${derby.isstarted}"/></bool>
        <echo>**************************************</echo>
        <echo>Derby server ALREADY RUNNING on ${derby.server}:${derby.server.port}. NOT STARTING.</echo>
        <else>
          <!-- we only run the configure if we know we are going to restart tomcat, hence
               not in the depends list -->
          <antcall target="configure"/>

          <!-- now start up the servers -->
          <antcall target="start-derby"/>
          <antcall target="start-tomcat"/>
          <antcall target="start-solr"/>
          
          <!-- print some helpful info -->
          <echo>${app.name} (${app.version}) server running using Apache Tomcat and Java</echo> 
          <echo>Tomcat: ${catalina.home}</echo>
          <echo>Java  : ${java.home}</echo>
          <if><bool><available file="${build.src.home}"/></bool>
          <echo>Perl  : ${full.perl.path}</echo>
          </if>
          <echo>URL   : ${internal.servlet-url}</echo>
          <echo>Public URL  : ${external.servlet-url}</echo>

        </else>
        </if>
      </else>
    </if>
    
  </target>

  <target name="stop" depends="init,stop-tomcat,stop-derby,stop-solr" 
          description="Shutdown the Tomcat, Derby and Solr servers."/>
  
  <target name="restart" description="Shutdown and restart Tomcat/Derby/Solr"  depends="init,stop,start"/>

  <target name="status" depends="check-derbyserver-running,check-solr-running,check-tomcat-running">
  </target>



  <!-- ============ Target Group: Derbyserver Targets ===================== -->
  <!-- derby is used for the users DB -->
  <target name="check-derbyserver-running">
    <condition property="derby.isrunning" value="true" else="false">
      <!--<socket server="jdbc:derby://${derby.server}" port="${derby.server.port}"/>-->
      <socket server="${derby.server}" port="${derby.server.port}"/><!-- like telnet machine port -->
    </condition>
    <echo>Derby is running: ${derby.isrunning}</echo>
  </target>

  <!-- Need a copy of the check-derbyserver-running target with a distinct property, because ant restart runs 
       both stop and start, which stop and start derby respectively. Both  need check the derby socket.
       Because each property can be set only once during an invocation with ant, ant restart will need 
       two check-derbyserver properties, one for each derby check. -->
  <target name="check-derbyserver-started">
    <condition property="derby.isstarted" value="true" else="false">
      <socket server="${derby.server}" port="${derby.server.port}"/>
    </condition>
    <echo>Derby is started: ${derby.isstarted}</echo>
  </target>


  <!-- Unused -->
  <target name="start-derby-java" depends="check-derbyserver-running">
    <if><bool><not><istrue value="${derby.isrunning}"/></not></bool>
      <echo>Launching derby on ${derby.server}:${derby.server.port}...</echo>
      <java classname="org.apache.derby.drda.NetworkServerControl" fork="true" spawn="true" clonevm="true">      
	<arg value="start"/>
	<classpath refid="derby.server.classpath"/>
      </java>
      <else>
	<echo>Derby server ALREADY RUNNING on ${derby.server}:${derby.server.port}</echo>
      </else>
    </if>
  </target>
  
  <target name="start-derby" depends="check-derbyserver-started">
    <if><bool><not><istrue value="${derby.isstarted}"/></not></bool>
      <echo>About to launch derby on ${derby.server}:${derby.server.port}</echo>
      <antcall target="force-start-derby"/>
      <else>
	<echo>Derby networked server ALREADY RUNNING on ${derby.server}:${derby.server.port}</echo>
      </else>
    </if>
  </target>

  <!-- Using derby 10.1.2.1
       See db-derby-10.1.2.1-bin/docs/html/adminguide/index.html -->
  <target name="force-start-derby">
      <property name="derby.server.classpath.prop" refid="derby.server.classpath" />
      <!-- During the installer, launching java directly fails on systems that don't have java installed.
	   Forcing force-start-derby to always use packages/jre/bin/java is wrong too: as gs3-setup does
	   a lot of work of determining the correct JRE_HOME/JAVA_HOME (not necessarily the GS3 bundled JRE)
	   and we want to be consistent in using the same java throughout. Solution:
	   Refer to the JRE used in executing this ant file, which for installers will always be the bundled one.
	   https://stackoverflow.com/questions/17571595/env-java-home-not-found-ant
	   
	   We set the derby.system.home so it doesn't default to the dir derby was
	   launched from. And then we can set greenstone3.xml to use a shorter URL
	   to the usersDB instead of the full GS3 web home path.
	   
	   Setting JAVA OPTS through an ant exec task:
	   https://stackoverflow.com/questions/55706746/set-java-opts-in-ant-task
      -->
      <exec executable="${java.home}/bin/java" spawn="true"><!-- failonerror="true"-->
	<arg value="-Dderby.system.home=${derby.system.home}"/><!-- Don't put quot-entities around the derby.system.home property! -->
	<env key="CLASSPATH" path="${derby.server.classpath.prop}"/>
	<arg value="org.apache.derby.drda.NetworkServerControl"/>
	<arg value="start"/>
	<arg value="-p"/>
	<arg value="${derby.server.port}"/>
      </exec>

      <echo>Property derby.server.classpath.prop is set to: ${derby.server.classpath.prop}</echo>

      <if>
	<bool><not><available file="${web.lib}/derbynet.jar" type="file"/></not></bool>
	<echo>File needed on classpath to start derby server: ${web.lib}/derbynet.jar not available</echo>
      </if>
      <if>
	<bool><not><available file="${web.lib}/derby.jar" type="file"/></not></bool>
	<echo>File needed on classpath to start derby server: ${web.lib}/derby.jar not available</echo>
      </if>
  </target>

  <target name="force-stop-derby">
    <java classname="org.apache.derby.drda.NetworkServerControl">
      <arg value="shutdown"/>
      <arg value="-p"/>
      <arg value="${derby.server.port}"/>
      <classpath refid="derby.server.classpath"/>
    </java>
  </target>

  <target name="stop-derby" description="Shutdown derby server only if running" depends="check-derbyserver-running"><!-- if="${derby.isrunning}" checks if true or false only from ant 1.8 on -->
    <if><bool><istrue value="${derby.isrunning}"/></bool>
      <antcall target="force-stop-derby"/>
      <else>
	<echo>Derby is not running</echo>
      </else>
    </if>
  </target>

<!-- ================= Target Group: Miscellaneous Configuration Targets ================== -->  
  <available file="${basedir}/resources/oai/OAIConfig.xml.in" property="oaiconfig.present"/>


  <target name="configure-oaiconfigs" if="oaiconfig.present">
    <echo>Generating live oaiconfig.xml files for servlets ${oai.servlets} </echo>
    <foreach list="${oai.servlets}" delimiter="," target="configure-oaiconfig" param="servlet_name"/>
  </target>


  <!-- This copies the OAIConfig-servletname.xml.in to the live file in web.classes. requires param of servlet_name eg oaiserver -->
  <target name="configure-oaiconfig">
    <echo>Copying ${basedir}/resources/oai/OAIConfig-${servlet_name}.xml.in to ${web.classes}/OAIConfig-${servlet_name}.xml</echo>
    <copy file="${basedir}/resources/oai/OAIConfig-${servlet_name}.xml.in" tofile="${web.classes}/OAIConfig-${servlet_name}.xml"/>
<!--    <rsr verbosity="1" file="${web.classes}/OAIConfig-${servlet_name}.xml" pattern="&lt;baseURL&gt;.*&lt;/baseURL&gt;" replacement="&lt;baseURL&gt;${external.context-url}/${servlet_name}&lt;/baseURL&gt;" />   -->
  </target>
    
  <target name="init-iiifconfig" if="iiifconfig.present">
    <echo>Writing out baseURL ${url}gs-cantaloupe to ${web.classes}/IIIFConfig.xml</echo>
    <copy file="${basedir}/resources/iiif/IIIFConfig.xml.in" tofile="${web.classes}/IIIFConfig.xml"/>
    <rsr verbosity="1" file="${web.classes}/IIIFConfig.xml" pattern="&lt;baseURL&gt;.*&lt;/baseURL&gt;" replacement="&lt;baseURL&gt;${url}gs-cantaloupe&lt;/baseURL&gt;" />   
  </target>
  

  <!-- =========== Target Group: Help targets ===================================  -->
  
  <property name="install-command" value="ant [options] prepare install"/>
  
  <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 Greenstone3, run '${install-command}'. 
    There are properties defined in build.properties. The install 
      process will ask you if these properties are set correctly. 
      To avoid this prompt, use the '-Dproperties.accepted=yes' 
      option.
    To log the output, use the '-logfile build.log' option.
  The README.txt file has more information about the ant targets
    and install process.
    </echo>
  </target>
  
  <target name="help" depends="usage" description="Print a help message"/>
  
  <target name="debug" depends="init" description="Display all the currently used properties">
    <echoproperties/>
  </target>

  <!-- ====== Target Group: initialization and setup targets ================== -->

  <target name="accept-properties" unless="properties.accepted">
    <input addproperty="properties.ok" validargs="y,n">The following properties (among others) are being used from a build.properties file found in this directory:
      server.protocols=${server.protocols}      
      tomcat.server=${tomcat.server}
      tomcat.port.https=${tomcat.port.https}
      localhost.port.http=${localhost.port.http}

         default.server.protocol=${default.server.protocol}
         default.tomcat.port=${default.tomcat.port}
         Local base HTTP URL used by Greenstone: ${local.http.url}

      tomcat.installed.path=${tomcat.installed.path} (this is the location of Tomcat's base dir if it is already installed)
      proxy.host=${proxy.host}
      proxy.port=${proxy.port}
      disable.collection.building=${disable.collection.building}
      If these are not acceptable, please change them and rerun this target. Continue [y/n]?
    </input>
    <condition property="do.abort"> 
      <equals arg1="n" arg2="${properties.ok}"/>
    </condition>
    <fail if="do.abort">Build aborted by user. Please change your properties settings and re-run the target</fail>
  </target>

  <!-- this sets up some initial properties -->
  <target name="init">

    <condition property="java.too.old">
      <or>
        <equals arg1="1.1" arg2="${java.specification.version}"/>
        <equals arg1="1.2" arg2="${java.specification.version}"/>
        <equals arg1="1.3" arg2="${java.specification.version}"/>
        <equals arg1="1.4" arg2="${java.specification.version}"/>
        <equals arg1="1.5" arg2="${java.specification.version}"/>
        <equals arg1="1.6" arg2="${java.specification.version}"/>
        <equals arg1="1.7" arg2="${java.specification.version}"/>
      </or>
    </condition>
    <fail if="java.too.old" message="You need Java 1.8 or greater to run Greenstone 3"/>
    
    <available file="${basedir}/gli" property="gli.present"/>
    <available file="${basedir}/common-src" property="common.src.present"/>
    <available file="${basedir}/gs2build" property="gs2build.present"/>
    <available file="${gnome-lib-dir}" property="gnome-lib.present"/>

    <condition property="tomcat.islocal">
      <or>
        <not><isset property="tomcat.installed.path"/></not>
        <equals arg1="" arg2="${tomcat.installed.path}"/>
      </or>
    </condition>

    <condition property="proxy.present">
      <and>
        <isset property="proxy.host"/>
        <not><equals arg1="" arg2="${proxy.host}"/></not>
      </and>
    </condition>

  </target>
  
  <target name="setup-proxy" depends="init" if="proxy.present">
    <condition property="ask.user">
      <or>
        <equals arg1="" arg2="${proxy.user}"/>
        <equals arg1="" arg2="${proxy.password}"/>
      </or>
    </condition>
    
    <getuserandpassword message="Using proxy: ${proxy.host}:${proxy.port}" if="ask.user" username="${proxy.user}" userproperty="proxy.username" pwordproperty="proxy.password"/>
    <mysetproxy proxyhost="${proxy.host}" proxyport="${proxy.port}" proxyuser="${proxy.username}" proxypassword="${proxy.password}"/>
  </target>

  <target name="copy-dot-svn-files" depends="init"
          description="Copies all resources .svn files to .in versions - which are used during configure to generate the runtime versions." >

    <for param="svnfile">
      <path>
        <fileset dir="${basedir}" includes="resources/**/*.svn"/>
      </path>

      <sequential>
        <local name="infile"/>
        <!-- Compute corresponding .in filename -->
        <pathconvert property="infile">
          <path>
            <pathelement location="@{svnfile}"/>
          </path>
          <mapper>
            <globmapper from="*.svn" to="*.in"/>
          </mapper>
        </pathconvert>

        <if><bool><not><available file="${infile}"/></not></bool>
          <echo>generating ${infile}</echo>
          <copy file="@{svnfile}" tofile="${infile}"/>
        </if>
      </sequential>
    </for>

    <antcall target="check-resources-timestamps"/>
  </target>
  
    <target name="init-oaiconfigs" if="oaiconfig.present">
    <echo>Generating new oaiconfig .in files for servlets ${oai.servlets} </echo>
    <foreach list="${oai.servlets}" delimiter="," target="init-oaiconfig" param="servlet_name"/>
  </target>

  <!-- This sets up the OAIConfig-servletname.xml.in file. requires param of servlet_name, eg oaiserver -->
  <target name="init-oaiconfig">
    <if><bool><not><available file="${basedir}/resources/oai/OAIConfig-${servlet_name}.xml.in"/></not></bool>
    <echo>Copying file ${basedir}/resources/oai/OAIConfig.xml.in to ${basedir}/resources/oai/OAIConfig-${servlet_name}.xml.in</echo>
    <echo>This file (OAIConfig-${servlet_name}.xml.in) will not be regenerated again, so you may edit it to customise settings for this servlet. To have it regenerated, delete the file and re-run 'ant init-oaiconfigs'.</echo>
    <copy file="${basedir}/resources/oai/OAIConfig.xml.in" tofile="${basedir}/resources/oai/OAIConfig-${servlet_name}.xml.in"/>
    <else>
      <echo>The file (OAIConfig-${servlet_name}.xml.in) was already present, so not regenerating it. You may edit it to customise settings for this servlet. To have it regenerated, delete the file (from ${basedir}/resources/oai) and re-run 'ant init-oaiconfigs'.</echo>
    </else>
    </if>
  </target>

  <!-- ========== Target Group: Web app Targets ================================ -->
  
  <target name="prepare-web" depends="init">
    <mkdir dir="${web.writablehome}/applet"/>
    <mkdir dir="${data.writablehome}/logs"/>
    <mkdir dir="${data.writablehome}/logs/tmp"/>
  </target>

  <target name="prepare-collections" depends="init"
    description="Unpack all the collections from their svn zipped versions">
    <property name="collect.dir" value="${web.home}/sites/localsite/collect"/>
    <property name="index-and-archives.zip" value="index-and-archives.zip"/>

    <echo message="Installing collections ..."/>
    <!-- collections requiring jni -->
    <if>
      <bool>
	<istrue value="${with.jni}"/>
      </bool>
      <antcall target="gs2mgdemo-install"/>
    </if>
    <if>
      <bool>
	<istrue value="${with.jni}"/>
      </bool>
      <antcall target="gs2mgppdemo-install"/>
    </if>
    <!-- pure java ones -->
    <antcall target="gberg-install"/>
    <antcall target="lucene-jdbm-demo-install"/>
  </target>

  <target name="gs2mgdemo-prepare" if="collect.dir">
    <property name="gs2mgdemo.dir" value="${collect.dir}/gs2mgdemo"/>

    <condition property="gs2mgdemo.present">
      <and>
        <available file="${gs2mgdemo.dir}/${index-and-archives.zip}"/>
      </and>
    </condition>
  </target>

  <target name="gs2mgdemo-install" if="gs2mgdemo.present" depends="gs2mgdemo-prepare">
    <unzip dest="${gs2mgdemo.dir}" src="${gs2mgdemo.dir}/${index-and-archives.zip}" />
    <echo>collection gs2mgdemo installed</echo>
  </target>

  <target name="gs2mgppdemo-prepare" if="collect.dir">
    <property name="gs2mgppdemo.dir" value="${collect.dir}/gs2mgppdemo"/>
 
    <condition property="gs2mgppdemo.present">
      <and>
        <available file="${gs2mgppdemo.dir}/${index-and-archives.zip}"/>
      </and>
    </condition>
  </target>

  <target name="gs2mgppdemo-install" if="gs2mgppdemo.present" depends="gs2mgppdemo-prepare">
    <unzip dest="${gs2mgppdemo.dir}" src="${gs2mgppdemo.dir}/${index-and-archives.zip}" />
    <echo>collection gs2mgppdemo installed</echo>
  </target>    

  <target name="gberg-prepare" if="collect.dir">
    <property name="gberg.dir" value="${collect.dir}/gberg"/>
    <available file="${gberg.dir}/index/index.zip" property="gberg.present"/>
  </target>

  <target name="gberg-install" if="gberg.present" depends="gberg-prepare">
    <unzip dest="${gberg.dir}/index" src="${gberg.dir}/index/index.zip"/>
    <echo>collection gberg installed</echo>
  </target>

  <target name="lucene-jdbm-demo-prepare" if="collect.dir">
    <property name="lucene-jdbm-demo.dir" value="${collect.dir}/lucene-jdbm-demo"/>
    <available file="${lucene-jdbm-demo.dir}/${index-and-archives.zip}" property="lucene-jdbm-demo.present"/>
  </target>

  <target name="lucene-jdbm-demo-install" if="lucene-jdbm-demo.present" depends="lucene-jdbm-demo-prepare">
    <unzip dest="${lucene-jdbm-demo.dir}" src="${lucene-jdbm-demo.dir}/${index-and-archives.zip}"/>
    <echo>collection lucene-jdbm-demo installed</echo>
  </target>


  <!-- Until 64 bit Linux and Mac (Lion) machines can generate a working IsisGdl, 
       use the ones generated on a 32 bit Linux and Mac (Leopard), respectively
    -->
  <target name="get-isisgdl" if="${current.os.isunix}">
    <exec executable="uname" dir="${basedir}" failonerror="false"
	  outputproperty="uname.val">
      <arg value="-m"/>
    </exec>
    
    <if>
      <!-- as the syntax below copies the IsidGdl binary into the gs2build directory
           only want to run the commend if collection.building.enabled -->
      <bool>
        <and>
          <istrue value="${collection.building.enabled}" />
          <equals arg1="${uname.val}" arg2="x86_64"/>
	</and>
      </bool>
      <echo>Bitness: ${uname.val}</echo>
      <if><bool><contains string="${os.bin.dir}" substring="darwin" casesensitive="false"/></bool>
	<exec executable="uname" dir="${basedir}" failonerror="false"
	      outputproperty="darwin.kernel.version">
	  <arg value="-r"/>
	</exec>
	
	<echo>darwin kernel version: ${darwin.kernel.version}</echo>
	<!-- up to high sierra/kernel (version 17.x) us IsisGdl.macleopard
	     From Mojave onwards, we can use the Mojave generated IsisGdl.mac64mojave -->
	<if>
	  <bool><matches string="${darwin.kernel.version}" pattern="^([0-9]|1[0-7])\."/></bool>
	  <property name="mac.isisgdl.suffix" value="macleopard"/>
	  <else>
	    <property name="mac.isisgdl.suffix" value="mac64mojave"/>
	  </else>      
	</if>	


	<!--<get src="${gsorg.root}/caveat-emptor/IsisGdl.macleopard"
             dest="${basedir}/gs2build/bin/${os.bin.dir}/IsisGdl"/>-->
	<exec executable="wget" failonerror="true">
	  <arg value="-P"/>
	  <arg value="${basedir}/gs2build/bin/${os.bin.dir}"/>
	  <arg value="--no-check-certificate"/>
	  <arg value="${gsorg.root}/caveat-emptor/IsisGdl.${mac.isisgdl.suffix}"/>
	</exec>
	<move file="${basedir}/gs2build/bin/${os.bin.dir}/IsisGdl.${mac.isisgdl.suffix}"
	      tofile="${basedir}/gs2build/bin/${os.bin.dir}/IsisGdl"/>
      </if>
      <if><bool><contains string="${os.bin.dir}" substring="linux" casesensitive="false"/></bool>
	<!--<get src="${gsorg.root}/caveat-emptor/IsisGdl.bin32"
             dest="${basedir}/gs2build/bin/${os.bin.dir}/IsisGdl"/>-->
	<exec executable="wget" failonerror="true">
	  <arg value="-P"/>
	  <arg value="${basedir}/gs2build/bin/${os.bin.dir}"/>
	  <arg value="--no-check-certificate"/>
	  <arg value="${gsorg.root}/caveat-emptor/IsisGdl.bin32"/>
	</exec>
	<move file="${basedir}/gs2build/bin/${os.bin.dir}/IsisGdl.bin32"
	      tofile="${basedir}/gs2build/bin/${os.bin.dir}/IsisGdl"/>
      </if>
      <chmod file="${basedir}/gs2build/bin/${os.bin.dir}/IsisGdl" perm="755"/>
    </if>
  </target>

  <target name="set-perl-shebangs" depends="perl-for-building">
    <if>
      <bool>
        <and><isset property="perl.path"/>
          <not><equals arg1="${perl.path}" arg2=""/></not>
        </and>
      </bool>

      <if><bool><istrue value="${current.os.iswindows}"/></bool>
	<property name="perl.exec" value="${perl.path}perl.exe"/>
	<else>
	  <property name="perl.exec" value="${perl.path}perl"/>	  
	</else>
      </if>

      <!--<echo>**** PERLPATH: ${perl.path}</echo>-->
      <echo>Setting perl shebangs to Perl Exec: ${perl.exec}</echo>

      <!-- set the shebangs in the cgi files to point to the correct perlpath -->
      <if>
	<bool><not><equals arg1="${web.home}" arg2="${web.writablehome}"></equals></not></bool>
	<mkdir dir="${web.writablehome}"/>
	<copy file="${full.web.dir}/WEB-INF/cgi/gliserver.pl"       tofile="${web.writablehome}/WEB-INF/cgi/gliserver.pl"       overwrite="true"/>
	<copy file="${full.web.dir}/WEB-INF/cgi/metadata-server.pl" tofile="${web.writablehome}/WEB-INF/cgi/metadata-server.pl" overwrite="true"/>
	<copy file="${full.web.dir}/WEB-INF/cgi/checksum.pl"        tofile="${web.writablehome}/WEB-INF/cgi/checksum.pl"        overwrite="true"/>
      </if>

      <rsr verbosity="1" file="${web.writablehome}/WEB-INF/cgi/gliserver.pl" pattern="^#!.*" replacement="#!${perl.exec} -w" lines="1"/>
      <rsr verbosity="1" file="${web.writablehome}/WEB-INF/cgi/metadata-server.pl" pattern="^#!.*" replacement="#!${perl.exec} -w" lines="1"/> 
      <rsr verbosity="1" file="${web.writablehome}/WEB-INF/cgi/checksum.pl" pattern="^#!.*" replacement="#!${perl.exec} -w" lines="1"/> 	
      <else>
	<echo>WARNING: perl.path is empty. Unable to set the shebangs in the perl files in ${web.writablehome}/WEB-INF/cgi</echo>
      </else>
    </if>
  </target>

  <target name="configure-web" depends="init,perl-for-building"
    description="Configure only the web app config files">

    <antcall target="set-perl-shebangs" inheritAll="true" />
    
    <filter token="gsdlhome" value="${gs2build.home}"/>
    <filter token="gsdlhomequoted" value="&quot;${gs2build.home}&quot;"/>
    <filter token="gsdl3srchome" value="${basedir}"/>
    <filter token="gsdl3srchomequoted" value="&quot;${basedir}&quot;"/>
    <filter token="gsdl3home" value="${web.home.unix}"/>
    <filter token="gsdl3homequoted" value="&quot;${web.home.unix}&quot;"/>
    <filter token="gsdl3writablehome" value="${web.writablehome.unix}"/>
    <filter token="gsdl3datahome" value="${data.writablehome.unix}"/>
    <filter token="gsdl3version" value="${app.version}"/>
    <filter token="revproxy.protocol" value="${revproxy.protocol}"/>
    <filter token="revproxy.domain"   value="${revproxy.domain}"/>
    <filter token="revproxy.opt_port" value="${revproxy.opt_port}"/>
    <filter token="revproxy.context"  value="${revproxy.context}"/>
    <filter token="server.protocols" value="${server.protocols}"/>
    <filter token="default.server.protocol" value="${default.server.protocol}"/>
    <filter token="tomcat.server" value="${tomcat.server}"/>
    <filter token="default.tomcat.port" value="${default.tomcat.port}"/>
    <filter token="localhost.server.http" value="${localhost.server.http}"/>
    <filter token="localhost.port.http" value="${localhost.port.http}"/>
    <filter token="tomcat.port.https" value="${tomcat.port.https}"/>
    <filter token="restrict.http.to.local" value="${restrict.http.to.local}"/>
    <filter token="tomcat.server.IPregex" value="${tomcat.server.IPregex}"/>

    <filter token="greenstone.context" value="${greenstone.context}"/>
    <filter token="webswing.context" value="${webswing.context}"/>
     <filter token="derbyserver" value="${derby.server}"/>
    <filter token="derbyserver.port" value="${derby.server.port}"/>
    <filter token="perlpath" value="${escaped.perl.path}"/>
    <filter token="defaultservletpath" value="${greenstone.default.servlet}"/>
    <filter token="disable.collection.building" value="${disable.collection.building}"/>
    <copy file="${basedir}/resources/cgi/gsdl3site.cfg.in" tofile="${web.writablehome}/WEB-INF/cgi/gsdl3site.cfg" filtering="true" overwrite="true"/>
    <copy file="${basedir}/resources/web/global.properties.in" tofile="${web.classes}/global.properties" filtering="true" overwrite="true"/>
    <copy file="${basedir}/resources/web/log4j.properties.in" tofile="${web.classes}/log4j.properties" filtering="true" overwrite="true"/>
    <if>
      <bool><istrue value="${gsdl3home.isreadonly}"/></bool>
      <!-- uncomment the writablehome properties -->
      <rsr verbosity="1" file="${web.classes}/global.properties" pattern="^#gsdl3\.(writable{1})?home" replacement="gsdl3.$1home" />
    </if>
    <copy todir="${web.classes}">
      <fileset dir="${basedir}" includes="${servlet.properties.filenames}"/>
    </copy>
    <chmod perm="664">
      <fileset dir="${web.classes}" includes="${servlet.properties.filenames}"/>
    </chmod>
    <chmod file="${web.classes}/global.properties" perm="664"/>
    <chmod file="${web.classes}/log4j.properties" perm="664"/>

    <copy file="${basedir}/resources/iiif/cantaloupe.properties.in" tofile="${gsdl3.writablebasedir}/cantaloupe.properties" filtering="true" overwrite="true"/>
    <chmod file="${gsdl3.writablebasedir}/cantaloupe.properties" perm="664"/>
  </target>

    <target name="configure-servlet-xml-files" depends="init"> 
      <copy file="${basedir}/resources/web/web.xml.in" tofile="${web.writablehome}/WEB-INF/web.xml" overwrite="true"/>
      <copy file="${basedir}/resources/web/servlets.xml.in" tofile="${web.writablehome}/WEB-INF/servlets.xml" overwrite="true">
      <filterset>
	<filter token="googlesigninclientid" value="${tomcat.googlesigninJDBCRealm.clientid}"/>
	<!-- the following might be useful, but not currently included, as not currently used -->
	<!--
            <filter token="gsdl3webhome" value="${web.home}"/>
            <filter token="gsdl3webwritablehome" value="${web.writablehome}"/>
	    <filter token="tomcathome" value="${basedir}/packages/tomcat"/>
	-->
     </filterset>
    </copy>
  </target>
  <target name="compile-web" depends="needs-gs3-devel,init">
    <javac srcdir="${web.classes}"
      destdir="${web.classes}"
      includeantruntime="${compile.includeantruntime}"
      debug="${compile.debug}"
      deprecation="${compile.deprecation}"
      optimize="${compile.optimize}"
      encoding="${compile.encoding}">
      <compilerarg line="${compile.javac.flags}"/>
      <classpath><path refid="compile.classpath"/></classpath>
    </javac>
  </target>

  <target name="compile-classpath-jars" depends="needs-gs3-devel,init">
    <if><bool><available file="admin/cp.mf"/></bool>
      <jar destfile="admin/cp.jar" manifest="admin/cp.mf"/>
    </if>
    <if><bool><available file="${lib.java}/cp.mf"/></bool>
      <jar destfile="${lib.java}/cp.jar" manifest="${lib.java}/cp.mf"/>
    </if>
    <if><bool><and><istrue value="${with.jni}"/>
    <available file="${lib.jni}/cp.mf"/></and></bool>
    <jar destfile="${lib.jni}/cp.jar" manifest="${lib.jni}/cp.mf"/>
    </if>
    <if><bool><available file="${web.lib}/cp.mf"/></bool>
      <jar destfile="${web.lib}/cp.jar" manifest="${web.lib}/cp.mf"/>
    </if>
    <jar destfile="cp.jar">
      <manifest>
        <attribute name="Class-Path" value="server.jar admin/cp.jar lib/java/cp.jar lib/jni/cp.jar web/WEB-INF/lib/cp.jar"/>
      </manifest>
    </jar>
  </target>

  <target name="clean-classpath-jars" depends="init">
    <delete file="admin/cp.jar"/>
    <delete file="${lib.java}/cp.jar"/>
    <delete file="${lib.jni}/cp.jar"/>
    <delete file="${web.lib}/cp.jar"/>
    <delete file="cp.jar"/>
  </target>


  <target name="svnupdate-web" unless="nosvn.mode">
    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="update"/>
      <arg value="${web.writablehome}"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>
  </target>

  <target name="update-web" depends="init,svnupdate-web,configure-web"
    description="update only the web stuff (config files)"/>


  <!--
      Newer versions of perl need CGI.pm placed in web/WEB-INF/cgi.
      We have a CGIModule.tar.gz in common-src/cgi-bin, that we can use for linux (unix) systems.

      - from perl5.22, CGI.pm is not included, but GS3 needs it:
      https://stackoverflow.com/questions/45434990/why-does-this-code-have-issues-with-the-cgi-module
      - bash line to get perl version: https://www.unix.com/shell-programming-and-scripting/18267-version-perl-installed-my-system.html
      - run bash command using pipes with ant: https://stackoverflow.com/questions/1187402/ant-run-command-with-pipes
      - single quotes in XML use &apos; https://stackoverflow.com/questions/7388695/how-can-i-escape-double-quotes-in-ant
      - islessthan and isgreaterthan ant bool conditionals: https://stackoverflow.com/questions/7616159/apache-ant-less-than
  -->  
  <condition property="cgimodule.exists">
    <and>
      <available file="${web.home}/WEB-INF/cgi" type="dir"/> <!--Unavailable for custom web.home-->
      <available file="${web.home}/WEB-INF/cgi/CGI.pm" type="file"/>
      <available file="${web.home}/WEB-INF/cgi/Fh.pm" type="file"/>
      <available file="${web.home}/WEB-INF/cgi/CGI" type="dir"/>
    </and>
  </condition>
  
  <target name="unpack-cgimodule" if="current.os.isunix" unless="cgimodule.exists">
    <exec executable="bash" outputproperty="perl.major.version">
      <arg value="-c"/>
      <arg line='"perl -v | awk &apos;/This/ {print $4}&apos; | sed -e &apos;s/,//&apos;"'/>
    </exec>
    <exec executable="bash" outputproperty="perl.min.version">
      <arg value="-c"/>
      <arg line='"perl -v | awk &apos;/This/ {print $6}&apos; | sed -e &apos;s/,//&apos;"'/>
    </exec>

    <if>
      <bool>
	<or>
	  <isgreaterthan arg1="${perl.major.version}" arg2="5"/>
	  <and>
	    <equals arg1="${perl.major.version}" arg2="5" trim="true"/>
            <islessthan arg1="${perl.minor.version}" arg2="22"/>
          </and>
        </or>
      </bool>    
    <echo message="Unpacking the CGIModule needed for your perl version: ${perl.major.version}.${perl.min.version}" />

    <untar compression="gzip" 
	   src="${common.src.home}/cgi-bin/CGIModule.tar.gz"
	   dest="${web.home}/WEB-INF/cgi"/>
    <move todir="${web.home}/WEB-INF/cgi" failonerror="false">
      <!-- This fileset moves *contents* of directory CGIModule into destination dir web.home -->
      <fileset dir="${web.home}/WEB-INF/cgi/CGIModule"/>
    </move>
    <delete dir="${web.home}/WEB-INF/cgi/CGIModule"/>
    
    </if>
  </target>
  <!-- ========== Target Group: Tomcat Targets ========================== -->

  <!-- this target downloads and installs Tomcat -->
  <target name="prepare-tomcat" depends="init,setup-proxy" if="tomcat.islocal"
    description="unzips the Tomcat package and copies a couple of files into it ">
    <echo>Checking for existence of: ${packages.home}/tomcat/.flagfile</echo>
    <if>
      <bool>
        <not><available file="${packages.home}/tomcat/.flagfile"/></not>
      </bool>

      <if>
        <bool>
          <equals arg1="${tomcat.version.major}" arg2="8"/>
        </bool>
        <!-- if we are using old tomcat 8, then download it -->
        <if>
	<bool>
	  <os family="mac" />
        </bool>	    
	<exec executable="curl" failonerror="true">
	  <arg value="--insecure"/>
	  <arg value="-o"/>
	  <arg value="${packages.home}/${tomcat.version}.zip"/>
	  <arg value="${gsorg.root}/gs3files/${tomcat.version}.zip"/>
	</exec>	
	<else>
	  <exec executable="${wget}" failonerror="true">
	    <arg value="-P"/><!-- specifying output dir, also: minus-minus-directory-prefix -->
	    <arg value="${packages.home}"/>
	    <arg value="--no-check-certificate"/>
	    <arg value="${gsorg.root}/gs3files/${tomcat.version}.zip"/>
	  </exec>
	</else>
      </if>
      </if>
      <unzip src="${packages.home}/${tomcat.version}.zip" 
             dest="${packages.home}"/>
	  
      <!-- delete any existing tomcat -->
      <delete dir="${packages.home}/tomcat"/>
      <move todir="${packages.home}/tomcat">
        <fileset dir="${packages.home}/${tomcat.version}"/>
      </move>
	  
	  <!-- To avoid the CGI permissions error appearing continuously in the (localhost) log, 
		need privileged=true set in the root context.xml file too, not just greenstone context file.
		See http://stackoverflow.com/questions/9845936/tomcat-v7-0-load-exception-marking-servlet-ssi-as-unavailable/10305471#10305471 -->

	  <copy file="${basedir}/resources/tomcat/root_context.xml.in" tofile="${packages.home}/tomcat/conf/context.xml" overwrite="true" />		
	  
	  <!--
      <copy file="${basedir}/resources/tomcat/setclasspath.bat"
        tofile="${packages.home}/tomcat/bin/setclasspath.bat"
        overwrite="true"/>
      <copy file="${basedir}/resources/tomcat/setclasspath.sh"
        tofile="${packages.home}/tomcat/bin/setclasspath.sh"
        overwrite="true"/>
	  -->
      <!-- make sure we have execute permission for the .sh files -->
      <chmod dir="${packages.home}/tomcat/bin" perm="ugo+rx" 
        includes="*.sh"/>   

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

      <else>
        <echo>Tomcat has been prepared, will not prepare</echo>
        <echo>Delete ${packages.home}/tomcat/.flagfile to force refresh</echo>
      </else>

    </if>

  </target>  
  

  <target name="configure-tomcat" depends="init,configure-tomcat-local,configure-tomcat-external"/>

  <target name="configure-tomcat-local" depends="init,perl-for-building" if="tomcat.islocal">
    <!-- re-setup the server.xml file -->
    <copy file="${basedir}/resources/tomcat/server_tomcat${tomcat.version.major}.xml.in"
		tofile="${packages.home}/tomcat/conf/server.xml" overwrite="true">
      <filterset>
        <filter token="shutdown-port" value="${tomcat.shutdown.port}"/>
	<filter token="tomcat.ajp.port" value="${tomcat.ajp.port}"/>
	<filter token="https.redirect.port" value="${https.redirect.port}"/>
	<filter token="localhost.port.http" value="${localhost.port.http}"/>
	<filter token="tomcat.port.https" value="${tomcat.port.https}"/>
	<!-- Relative path preferred for keystore.file, in case tomcat is moved elsewhere -->
	<!--<filter token="keystore.file" value="conf/https_cert/${tomcat.server}.jks" />-->
	<!--ON UNIX: <filter token="keystore.file" value="conf/https_cert/fullchain_and_prvtkey.p12" />-->
	<!--ON WINDOWS: <filter token="keystore.file" value="conf/https_cert/fullchain_and_prvtkey.pfx" />-->
	<filter token="keystore.file" value="conf/https_cert/${keystore.file}" />
	<!-- tomcat Connector's keystoreType param defaults to JKS (Java keystore), see https://tomcat.apache.org/tomcat-7.0-doc/config/http.html
	We'll follow the instructions at https://community.letsencrypt.org/t/using-lets-encrypt-with-tomcat/41082,
	https://www.ssl.com/how-to/create-a-pfx-p12-certificate-file-using-openssl/
	and https://computingforgeeks.com/tomcat-7-with-letsencrypt-ssl-certificate/ 
	(minus the keytool step) and use the PKCS12 file generated by openssl directly,
	instead of an additional step to generate the java keystore file from that -->
	<filter token="keystore.type" value="PKCS12"/>
	<filter token="keystore.pass" value="${keystore.pass}"/>
	<!--
	    if server.protocols doesn't contain http, then http is only to be locally available (e.g. for solr servlet on 127.0.0.1)
	    In that case, see https://serverfault.com/questions/218666/how-to-configure-tomcat-to-only-listen-to-127-0-0-1
	-->
	<filter token="http.address.restriction" value="${http.address.restriction}"/>	
	<filter token="https.comment.out.start" value="${https.comment.out.start}"/>
	<filter token="https.comment.out.end" value="${https.comment.out.end}"/>
      </filterset>
    </copy>



    <!-- set up the greenstone3 context, it may have a custom name specified in build.properties -->
	<if><bool><not><equals arg1="greenstone3" arg2="${greenstone.context}"></equals></not></bool>
		<copy file="${basedir}/resources/tomcat/greenstone3.xml.in" tofile="${basedir}/resources/tomcat/${greenstone.context}.xml.in" overwrite="true"/>
	</if>
    <copy file="${basedir}/resources/tomcat/${greenstone.context}.xml.in" tofile="${packages.home}/tomcat/conf/Catalina/localhost/${greenstone.context}.xml" overwrite="true">
      <filterset>
        <filter token="gsdl3webhome" value="${web.home}"/>
        <filter token="gsdl3webwritablehome" value="${web.writablehome}"/>
<!--	<filter token="privilegedattribute" value="${privileged.attribute}"/>-->
	<filter token="allowlinking" value="${tomcat.allowLinking}"/>
	<filter token="allowedIPs" value="${allowed.IPs}"/>
	<filter token="derbyserver" value="${derby.server}"/>
	<filter token="derbyserverport" value="${derby.server.port}"/>
	<filter token="googlesigninclientid" value="${tomcat.googlesigninJDBCRealm.clientid}"/>
      </filterset>
    </copy>
    <if>
      <bool><and>
	<available file="${packages.home}/tomcat/conf/Catalina/localhost/greenstone3.xml"/>
	<not><equals arg1="greenstone3" arg2="${greenstone.context}"></equals></not>
      </and></bool>
      <delete file="${packages.home}/tomcat/conf/Catalina/localhost/greenstone3.xml"/>
    </if>
	
    <!-- set up the greenstone3 web.xml file -->
    <copy file="${basedir}/resources/tomcat/web${tomcat.version.major}.xml.in" tofile="${packages.home}/tomcat/conf/web.xml" overwrite="true">
      <filterset>
	<filter token="perlpath" value="${perl.path}"/>
      </filterset>
    </copy>
  </target>
 
  <target name="configure-tomcat-external" depends="init" unless="tomcat.islocal">
    <!-- re-setup the server.xml file -->
    <!-- need to edit the config file, or do we get the user to do this???-->
  </target>
  

  
  <!-- This target runs tomcat's "bin/catalina.bat(.sh) jpda start" 
       to allow debugging the running GS3 server in Eclipse. See the instructions at 
       http://www.wikijava.org/wiki/Debugging_a_servlet_with_tomcat_and_Eclipse_tutorial
       on how to use this with eclipse
    -->
  <target name="debug-start-tomcat" description="Startup Tomcat for debugger" depends="envbased-localtomcat-path,init,set-webswing-env" if="tomcat.islocal">
    <property name="tomcat.classpath" refid="local.tomcat.classpath"/>
    <property name="tomcat.path" refid="local.tomcat.path"/>

    <if><bool>
	<isset property="fedora.maxpermsize"/></bool>
      <property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=&quot;$GSDL3HOME&quot; -DGSDLOS=$GSDLOS -DPATH=&quot;$PATH&quot;  ${tomcat.java.opts} -Dcantaloupe.config=&quot;${basedir}/cantaloupe.properties&quot;  ${webswing.catalina.opts} ${fedora.maxpermsize}"/>
      <else>
	<property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=&quot;$GSDL3HOME&quot; -DGSDLOS=$GSDLOS -DPATH=&quot;$PATH&quot;  ${tomcat.java.opts} -Dcantaloupe.config=&quot;${basedir}/cantaloupe.properties&quot; ${webswing.catalina.opts} "/>
      </else>
    </if>

    <echo file="${catalina.home}/bin/setenv.bat">set CLASSPATH=${tomcat.classpath}</echo>
    <echo file="${catalina.home}/bin/setenv.sh">export CLASSPATH=${tomcat.classpath}</echo>
	
    <exec executable="${catalina.home}/bin/catalina.sh" os="${os.unix}" dir="${catalina.home}/bin" spawn="false">
      <env key="JPDA_ADDRESS" value="8000"/> <!-- for debugging Tomcat in Eclipse -->
      <env key="JPDA_TRANSPORT" value="dt_socket"/> <!-- for debugging Tomcat in Eclipse -->
      <!--<env key="GSDLOS" value="linux"/> do we need this?? -->
      <env key="PATH" path="${tomcat.path}"/>
      <!-- Based on:
	     https://jfrog.com/knowledge-base/tomcat-takes-forever-to-start-what-can-i-do/
	   consider controlling Tomcat to use the psuedo random number generator '/dev/urandom' 
	   raher than the default '/dev/random' which while cryptographically stronger is
	   also slower when demand is high

           -Djava.security.egd=file:/dev/urandom 
      -->
      <env key="CATALINA_OPTS" value="${catalina.opts}"/>
      <env key="CATALINA_HOME" value="${catalina.home}"/>
      <env key="CLASSPATH" path="${tomcat.classpath}"/>
      <env key="LD_LIBRARY_PATH" path="${env.LD_LIBRARY_PATH}:${lib.jni}"/>
      <env key="DYLD_LIBRARY_PATH" path="${env.DYLD_LIBRARY_PATH}:${lib.jni}:${gdbm.home}/lib"/> <!-- for mac os --> <!-- need gdbm here these days ??-->
      <env key="WNHOME" path="${wn.home}"/>
      <env key="FEDORA_HOME" path="${fedora.home}"/>

      <env key="_RUNJAVA" value="${env.CATALINA_START_RUNJAVA}"/>
      <arg value="jpda" />
      <arg value="start" />      
    </exec>
    <exec executable="${catalina.home}/bin/catalina.bat" osfamily="windows" dir="${catalina.home}/bin" spawn="true">
      <env key="JPDA_ADDRESS" value="8000"/> <!-- for debugging Tomcat in Eclipse -->
      <env key="JPDA_TRANSPORT" value="dt_socket"/> <!-- for debugging Tomcat in Eclipse -->
      <env key="GSDLOS" value="windows"/>
      <env key="GSDL3HOME" value="${web.home}"/>
      <env key="Path" path="${tomcat.path}"/>
      <env key="PATH" path="${tomcat.path}"/>
      <env key="CATALINA_OPTS" value="${catalina.opts}"/>
      <env key="CATALINA_HOME" value="${catalina.home}"/>
      <env key="CLASSPATH" path="${tomcat.classpath}"/>
      <env key="FEDORA_HOME" path="${fedora.home}"/>

      <arg value="jpda" />
      <arg value="start" />
    </exec>
    <!-- wait for the server to startup in case other targets need it running -->
    <waitfor maxwait="5" maxwaitunit="second">
      <and>
         <socket server="${tomcat.server}" port="${default.tomcat.port}"/>
	<!--<http url="${default.server.protocol}://${tomcat.server}:${default.tomcat.port}${app.path}/index.html"/>-->
	<http url="${local.http.url}${app.path}/index.html"/>
      </and>
    </waitfor>
  </target>  
  
  <target name="start-tomcat" description="Startup only Tomcat" depends="check-tomcat-started">
    <if><bool><istrue value="${tomcat.isstarted}"/></bool>
      <echo>**************************************</echo>
      <echo>A WEB SERVER IS ALREADY RUNNING ON ${default.server.protocol}://${tomcat.server}:${default.tomcat.port} (${local.http.url}). NOT STARTING.</echo>
      <echo>**************************************</echo>
      <else>
	<antcall target="force-start-tomcat"/>
      </else>
      </if>
  </target>

  <!-- Another way: http://ptrthomas.wordpress.com/2006/03/25/how-to-start-and-stop-tomcat-from-ant/ -->
  <target name="force-start-tomcat" description="Startup only Tomcat" depends="envbased-localtomcat-path,init,set-webswing-env" if="tomcat.islocal">

    <property name="tomcat.classpath" refid="local.tomcat.classpath"/>
    <property name="tomcat.path" refid="local.tomcat.path"/>

    <if><bool>
	<isset property="fedora.maxpermsize"/></bool>
      <property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=&quot;$GSDL3HOME&quot; -DGSDLOS=$GSDLOS -DPATH=&quot;$PATH&quot;  ${tomcat.java.opts} -Dcantaloupe.config=&quot;${gsdl3.writablebasedir}/cantaloupe.properties&quot;  ${webswing.catalina.opts} ${fedora.maxpermsize}"/>
      <else>
	<property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=&quot;$GSDL3HOME&quot; -DGSDLOS=$GSDLOS -DPATH=&quot;$PATH&quot; ${tomcat.java.opts} -Dcantaloupe.config=&quot;${gsdl3.writablebasedir}/cantaloupe.properties&quot; ${webswing.catalina.opts}"/>
      </else>
    </if>
	
    <echo file="${catalina.home}/bin/setenv.bat">set CLASSPATH=${tomcat.classpath}</echo>
    <echo file="${catalina.home}/bin/setenv.sh">export CLASSPATH=${tomcat.classpath}</echo>

    <!-- using osfamily instead of testing os against os.windows list of recognised windows versions
	 so that future windows versions are included. See http://simonharrer.wordpress.com/tag/osfamily/ 
	Can't use the osfamily test for linux-type machines as a group since osfamily=unix is separate from osfamily=mac,
	see http://ant-contrib.sourceforge.net/tasks/tasks/osfamily.html -->

    <exec executable="${sudo.or.not}" os="${os.unix}" dir="${catalina.home}/bin" spawn="false">
      <!--<env key="JAVA_TOOL_OPTIONS" value="-Dfile.encoding=UTF-8"/>--><!-- Was needed by Windows for on e.g. Chinese locales, but added to unix for symmetry-->
      <env key="CATALINA_OPTS" value="${catalina.opts}"/>
      <env key="CATALINA_HOME" value="${catalina.home}"/>
      <env key="CLASSPATH"     path="${tomcat.classpath}"/>
      <env key="PATH"          path="${tomcat.path}"/>

      <env key="LD_LIBRARY_PATH"   path="${env.LD_LIBRARY_PATH}:${lib.jni}"/>
      <env key="DYLD_LIBRARY_PATH" path="${env.DYLD_LIBRARY_PATH}:${lib.jni}:${gdbm.home}/lib"/> <!-- for mac os --> <!-- need gdbm here these days ??-->

      <!-- Webswing servlet needs the following environment variables to be set to be able to run GLI -->
      <env key="GSDL3SRCHOME" value="${env.GSDL3SRCHOME}"/>
      <!-- this is where the sites and interfaces should be -->
      <env key="GSDL3HOME"    value="${web.home}"/>     
      <env key="GSDLHOME"     value="${env.GSDLHOME}"/> <!-- i.e., gs2build.home -->
      <env key="GSDL3EXTS"    value="${env.GSDL3EXTS}"/>
      <env key="GSDLEXTS"     value="${env.GSDLEXTS}"/>
      <env key="SOLR_PORT"    value="${env.SOLR_PORT}"/>
      <env key="SOLR_HOST"    value="${env.SOLR_HOST}"/>
      <env key="GSDLOS"       value="${env.GSDLOS}"/>
      <env key="WNHOME" path="${wn.home}"/>
      <env key="FEDORA_HOME" path="${fedora.home}"/>

      <env key="_RUNJAVA" value="${env.CATALINA_START_RUNJAVA}"/>
      <arg value="${catalina.home}/bin/startup.sh"/>
    </exec>
    <exec executable="${catalina.home}/bin/startup.bat" osfamily="windows" dir="${catalina.home}/bin" spawn="true">
      <!--<env key="JAVA_TOOL_OPTIONS" value="-Dfile.encoding=UTF-8"/>-->
      <env key="CATALINA_OPTS" value="${catalina.opts}"/>
      <env key="CATALINA_HOME" value="${catalina.home}"/>
      <env key="CLASSPATH"     path="${tomcat.classpath}"/>

      <env key="PATH"          path="${tomcat.path}"/>
      <env key="Path"          path="${tomcat.path}"/>
      <!-- Should the equivalent LD_LIBRARY_PATH line above for Unix, be turned into an equivalent PATH update, as this is what Windows uses? -->

      <!-- Webswing servlet needs the following environment variables to be set to be able to run GLI -->
      <env key="GSDL3SRCHOME" value="${env.GSDL3SRCHOME}"/>       
      <env key="GSDL3HOME"    value="${web.home}"/>     
      <env key="GSDLHOME"     value="${env.GSDLHOME}"/> <!-- i.e., gs2build.home -->
      <env key="GSDL3EXTS"    value="${env.GSDL3EXTS}"/>
      <env key="GSDLEXTS"     value="${env.GSDLEXTS}"/>
      <env key="SOLR_PORT"    value="${env.SOLR_PORT}"/>
      <env key="SOLR_HOST"    value="${env.SOLR_HOST}"/>

      <env key="GSDLOS"       value="${env.GSDLOS}"/>   <!-- used to be hardwired to 'windows', but now gs3-setup.sh run, GSDLOS should be set -->

      <env key="WNHOME" path="${wn.home}"/>
      <env key="FEDORA_HOME" path="${fedora.home}"/>
    </exec>
    <!-- wait for the server to startup in case other targets need it running -->
    <waitfor maxwait="5" maxwaitunit="second">
      <and>
        <socket server="${tomcat.server}" port="${default.tomcat.port}"/>
	<!-- The following is time-consuming to wait for: uncomment for extra thoroughness
	     For now, chosing to have it commented out in svn checked out version of code
	     Also leaving the <and> element in, even though it is now just one test to
	     make it easier to return to the double test
	     Note: the 'debug-start-tomcat' target does still include this test
	-->
        <!-- <http url="${local.http.url}${app.path}/index.html"/> -->
      </and>
    </waitfor>

  </target>


  <!-- windows: do we want to launch a webrowser?? -->
  <!-- shouldn't this test whether anything is running first? 
       It's safer to always attempt to stop tomcat: that way we won't be dependent on the right time 
       to check whether the server is stopped or still running before attempting to start again.
       This target, which was recently called force-stop-tomcat for a while but is back to being 
       called stop-tomcat, now hides the Java exception output that appears whenever tomcat is already 
       stopped as happens when stop-tomcat is called consecutively. -->
  <target name="force-stop-tomcat" description="Shutdown only Tomcat" depends="init" if="tomcat.islocal">
    <!--<exec executable="${catalina.home}/bin/shutdown.sh" os="${os.unix}" dir="${catalina.home}/bin" spawn="false">-->
    <exec executable="${sudo.or.not}" os="${os.unix}" dir="${catalina.home}/bin" spawn="false">
      <!--<env key="JAVA_TOOL_OPTIONS" value="-Dfile.encoding=UTF-8"/>--><!-- Was needed by Windows for on e.g. Chinese locales, but added to unix for symmetry-->
      <env key="FEDORA_HOME" path="${fedora.home}"/>
      <env key="CATALINA_HOME" value="${catalina.home}"/>
      <arg value="${catalina.home}/bin/shutdown.sh"/>
      <!-- <arg line=">/dev/null"/> --> <!-- removed stderr redirection '2>&amp;1' as surely better to see error if they occur -->
    </exec>
    <exec executable="${catalina.home}/bin/shutdown.bat" osfamily="windows" dir="${catalina.home}/bin" spawn="false">
      <!--<env key="JAVA_TOOL_OPTIONS" value="-Dfile.encoding=UTF-8"/>--><!-- In case on stopping tomcat, GLI may read in some XML file during cleanup for exit? -->
      <env key="FEDORA_HOME" path="${fedora.home}"/>
      <env key="CATALINA_HOME" value="${catalina.home}"/>
      <!-- <arg line=">nul"/> --> <!-- removed stderr redirection '2>&amp;1' as surely better to see error if they occur -->
    </exec>    
  </target>

  <!-- Can also try the "socket" condition in place of the "http" condition 
       And also use a <waitfor> in place of <condition>, such as:
       <waitfor maxwait="5" maxwaitunit="second" timeoutproperty="tomcat.isstopped"><http url="..."/></waitfor>
       The http URL resolves to host:port/greenstone3 
       Condition uses <http/> rather than <socket/> for testing, since if the server was stopped, the socket 
       might still be in use for some moments. We test the URL with the http condition since it's likelier to
       fail sooner if the server has indeed been stopped. -->
  <target name="check-tomcat-running">
    <condition property="tomcat.isrunning" value="true" else="false">
      <http url="${local.http.url}"/>
    </condition>
    <echo>Tomcat is running: ${tomcat.isrunning}</echo>
  </target>
  
  
  <!-- Need a copy of the check-tomcat-running target with a distinct property, because ant restart runs 
       both stop and start, both of which need to do tomcat checks. Each property can be set only once during 
       an invocation with ant. So ant restart will need two properties to store each of the tomcat checks -->
  <target name="check-tomcat-started">
    <condition property="tomcat.isstarted" value="true" else="false">
      <http url="${local.http.url}"/>
    </condition>
    <echo>Tomcat is started: ${tomcat.isstarted}</echo>
  </target>



  <!-- stop-tomcat checks if the tomcat server is already running. If it appears to be running
       (regardless of whether tomcat was just starting to shut down), this target calls force-stop-tomcat
       to issue the shutdown command to tomcat. Then it waits for at most 15 seconds for the server to 
       actually stop by checking the socket at which tomcat listens every second, printing a warning
       at the end of the max wait time of 15 seconds if tomcat was still running. -->
  <target name="stop-tomcat" description="Shutdown only Tomcat if running" depends="check-tomcat-running"><!-- if="${tomcat.isrunning}" checks if true or false only from ant 1.8 on -->
    <if>
      <bool><istrue value="${tomcat.isrunning}"/></bool>
      <antcall target="force-stop-tomcat"/>
      <!-- <antcall target="force-stop-tomcat"/> --> <!-- If there is an issue with 'force-stop-tomcat' not working when called once, we should figure out what that is, rather than call twice -->

      <property name="wait.numchecks" value="15"/>
      <echo>Waiting for the server to shutdown... (${wait.numchecks} seconds max)</echo>
      <waitfor maxwait="${wait.numchecks}" maxwaitunit="second" checkevery="1" checkeveryunit="second" timeoutproperty="tomcat.timedout">
	<not><socket server="${tomcat.server}" port="${default.tomcat.port}"/></not>
      </waitfor>
      
      <if>
	<bool>
	  <isset property="${tomcat.timedout}"/>
	</bool>
	<echo>WARNING: Checked the socket ${wait.numchecks} times, but port ${default.tomcat.port} is still busy.</echo>
	<else>
	  <echo>Tomcat is stopped.</echo>
	</else>
      </if>      

      <else>
	<echo>Tomcat is not running</echo>
      </else>
    </if>
  </target>

  <target name="restart-tomcat" description="Shutdown and restart only Tomcat" depends="init,stop-tomcat,start-tomcat"/>

  <target name="setup-catalina-ant-tasks">
    <!-- Configure the custom Ant tasks for the Tomcat Manager application -->
    <taskdef name="deploy"    classname="org.apache.catalina.ant.DeployTask"
      classpath="${catalina.home}/server/lib/catalina-ant.jar"/>
    <taskdef name="list"      classname="org.apache.catalina.ant.ListTask"
      classpath="${catalina.home}/server/lib/catalina-ant.jar"/>
    <taskdef name="reload"    classname="org.apache.catalina.ant.ReloadTask"
      classpath="${catalina.home}/server/lib/catalina-ant.jar"/>
    <taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask"
      classpath="${catalina.home}/server/lib/catalina-ant.jar"/>
    <taskdef name="roles"     classname="org.apache.catalina.ant.RolesTask"
      classpath="${catalina.home}/server/lib/catalina-ant.jar"/>
    <taskdef name="start"     classname="org.apache.catalina.ant.StartTask"
      classpath="${catalina.home}/server/lib/catalina-ant.jar"/>
    <taskdef name="stop"      classname="org.apache.catalina.ant.StopTask"
      classpath="${catalina.home}/server/lib/catalina-ant.jar"/>
    <taskdef name="undeploy"  classname="org.apache.catalina.ant.UndeployTask"
      classpath="${catalina.home}/server/lib/catalina-ant.jar"/>
  </target>


  <target name="clear-tomcat-sessions" description="Clear the Tomcat Session info." depends="init">
    <exec executable="cmd" osfamily="windows" dir="${catalina.home}/work/Catalina/localhost/greenstone3" spawn="false">
      <arg line="/c echo. > SESSIONS.ser"/>
    </exec>
    <exec executable="rm" os="${os.unix}" dir="${catalina.home}/work/Catalina/localhost/greenstone3" spawn="false">
      <arg value="-f"/>
      <arg value="SESSIONS.ser"/>
    </exec>

  </target>

  <!-- =========  Target Group: Admin Targets ============================ -->  

  <!-- This target won't work with Eclipse because the SecureInputHandler used below conflicts with it. 
    See http://www.dcepler.net/post.cfm/hiding-password-input-in-ant 
    But you can do: echo mypassword | ant config-admin -->
  <target name="config-admin" description="Reset admin password">
    <input addproperty="admin.password" defaultvalue="admin" message="New admin password (5-40 characters):&gt;">
      <handler classname="org.apache.tools.ant.input.SecureInputHandler" /> <!-- password won't be visible on screen -->
    </input>
    <!--<echo>PWD: ${admin.password}</echo>-->
    <antcall target="update-userdb">
      <param name="user.username" value="admin"/>
      <param name="user.password" value="${admin.password}"/>
      <param name="user.groups" value="administrator"/>
      <param name="user.status" value=""/>
      <param name="user.comment" value="Password updated."/>
      <param name="user.email" value=""/>
    </antcall>
  </target>

  <target name="config-user" description="Add or modify users" depends="get-user-data,update-userdb"/>
<!-- this is called bu config-user to ask for the new user details at the prompt -->
  <target name="get-user-data" >
    <input addproperty="user.username" message="Username:&gt;"/>
    <input addproperty="user.password" defaultvalue="" message="Password (5-40 characters):&gt;">
      <handler classname="org.apache.tools.ant.input.SecureInputHandler" /> <!-- password won't be visible on screen -->
    </input>
    <input addproperty="user.groups" defaultvalue="" message="Groups (comma-separated list, e.g. personal-collections-editor):&gt;"/>
    <input addproperty="user.status" defaultvalue="true" message="Enabled (true/false):&gt;"/>
    <input addproperty="user.comment" defaultvalue="" message="Comment:&gt;"/>
    <input addproperty="user.email" defaultvalue="" message="Email:&gt;"/>
  </target>

  <!-- This target won't work with Eclipse because the SecureInputHandler used below conflicts with it. 
       See http://www.dcepler.net/post.cfm/hiding-password-input-in-ant 
       But you can do: echo mypassword | ant config-admin -->
  <target name="update-userdb"  depends="start-derby">

    <!-- 
       We're now using derby networked server, so stopping and starting tomcat is not necessary.
       For embedded derby: stop tomcat if running, since derby db is embedded 
       and only allows connections from one jvm instance at a time
	 See http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
	 The ${tomcat.isrunning} property is set by the depends-target "check-tomcat-running" -->
   
    <!-- Need the derby networked server to be running in order to modify the usersDB.
	 The start-derby task will check if derby is already running (if not, ${derby.isstarted} will 
	 be false) and will only start up networked derby if it is not already running. 
	 The ${derby.isstarted} property is set by the depends-target "start-derby", since it won't
	 set the property if called with antcall (like launching in a subshell). Have to use 'depends'.
	 We'll check ${derby.isstarted} at the end to stop derby again if we had to start it up now.-->
    <!--<antcall target="start-derby"/>-->

    <!-- wait a max of 5 seconds for the derbyserver to have started up -->
    <waitfor maxwait="5" maxwaitunit="second">
      <socket server="${derby.server}" port="${derby.server.port}"/>
    </waitfor>

    <!--<echo>${admin.password}</echo>--> <!-- for testing -->
    <echo>gsdl3.writablebasedir: ${gsdl3.writablebasedir}</echo> <!-- for testing -->
    <echo>web.home: ${web.home}</echo> <!-- for testing -->
    <java classname="org.greenstone.gsdl3.util.ModifyUsersDB">
      <classpath refid="running.classpath"/> <!--for ${web.lib}/gsdl3.jar and supporting files-->
      <sysproperty key="gsdl3.writablehome" value="${web.writablehome}" /> <!-- passes -D<syspropKey=syspropVal> to java class ModifyUsersDB. Available in java code as System.getProperty("syspropKey") -->
      <arg value="usersDB"/><!--<arg file="${web.home}/etc/usersDB"/>-->
      <arg value="${user.username}"/>
      <arg value="password=${user.password}"/>
      <arg value="groups=${user.groups}"/>
      <arg value="status=${user.status}"/>
      <arg value="comment=${user.comment}"/>
      <arg value="email=${user.email}"/>
    </java>

    <!-- Need to stop networked derby again if we ran it for this target with the depends=start-derby call. 
	 The test here is for <not>derby.isstarted</not>, as the property would be the same as before 
	 derby was started, since properties are immutable within a single ant command. -->
    <if>
      <bool>
	<not><istrue value="${derby.isstarted}"/></not>
      </bool>
      <antcall target="force-stop-derby"/>
    </if>

  </target>

    <!--ant task http: http://www.jajakarta.org/ant/ant-1.6.1/docs/ja/manual/api/org/apache/tools/ant/taskdefs/condition/Http.html-->
  <target name="reconfigure" description="Reconfigure the message router">
    <waitfor maxwait="5" maxwaitunit="second">
      <!--<http url="${default.server.protocol}://${tomcat.server}:${default.tomcat.port}${app.path}${server.default.servlet}?a=s&amp;sa=c"/>-->
      <http url="${local.http.url}${app.path}${server.default.servlet}?a=s&amp;sa=c"/>
    </waitfor>
  </target>

  <!--Command-line args to Ant: http://www.jguru.com/faq/view.jsp?EID=471794-->
  <target name="reconfigure-collection" description="Reconfigure the collection">
    <waitfor maxwait="5" maxwaitunit="second">
      <!--<http url="${default.server.protocol}://${tomcat.server}:${default.tomcat.port}${app.path}${server.default.servlet}?a=s&amp;sa=c&amp;sc=${collection}"/>-->
      <http url="${local.http.url}${app.path}${server.default.servlet}?a=s&amp;sa=c&amp;sc=${collection}"/>
    </waitfor>
  </target>

    <!-- http://blog.andrewbeacock.com/2007/11/how-to-truncate-log-file-using-ubuntu.html 
    Doing "cat </dev/null > packages/tomcat/logs/catalina.out" doesn't work as an ant target.
    It seems to have a problem with cat or </dev/null, with or without the < sign. -->
  <target name="reset-logs" description="Empties catalina.out, greenstone.log, usage.log, server.log and contents of web/logs/tmp">
    <echo>Truncating ${catalina.home}/logs/catalina.out, ${catalina.home}/bin/logs/webswing.log, greenstone.log, usage.log and server.log, and emptying ${web.writablehome}/logs/tmp</echo>    
    <exec executable="rm" os="${os.unix}" dir="${catalina.home}/logs" spawn="false">
      <arg value="-f"/>
      <arg value="catalina.out"/>
    </exec>
    <if><bool><available file="${catalina.home}/bin/logs" type="dir"/></bool>
      <exec executable="rm" os="${os.unix}" dir="${catalina.home}/bin/logs" spawn="false">
	<arg value="-f"/>
	<arg value="webswing.log"/>
      </exec>
    </if>
    <exec executable="rm" os="${os.unix}" dir="${data.writablehome}/logs" spawn="false">
      <arg value="-f"/>
      <arg value="greenstone.log"/>
    </exec>
    <exec executable="rm" os="${os.unix}" dir="${data.writablehome}/logs" spawn="false">
      <arg value="-f"/>
      <arg value="usage.log"/>
    </exec>
    <exec executable="rm" os="${os.unix}" dir="${data.writablehome}/logs" spawn="false">
      <arg value="-f"/>
      <arg value="server.log"/>
    </exec>

    <exec executable="touch" os="${os.unix}" dir="${catalina.home}/logs"
    spawn="false">
      <arg value="catalina.out"/>
    </exec>
    <exec executable="touch" os="${os.unix}" dir="${data.writablehome}/logs"
      spawn="false">
      <arg value="greenstone.log"/>
    </exec>
    <exec executable="touch" os="${os.unix}" dir="${data.writablehome}/logs"
      spawn="false">
      <arg value="usage.log"/>
    </exec>
    <exec executable="touch" os="${os.unix}" dir="${data.writablehome}/logs"
      spawn="false">
      <arg value="server.log"/>
    </exec>

    <exec executable="cmd" osfamily="windows" dir="${catalina.home}/logs" spawn="false">
      <arg line="/c echo. > catalina.out"/>
    </exec>
	<if><bool><available file="${catalina.home}/bin/logs" type="dir"/></bool>
      <exec executable="cmd" osfamily="windows" dir="${catalina.home}/bin/logs" spawn="false">
		<arg line="/c echo. > webswing.log"/>
      </exec>
    </if>
    <exec executable="cmd" osfamily="windows" dir="${data.writablehome}/logs" spawn="false">
      <arg line="/c echo. > greenstone.log"/>
    </exec>
     <exec executable="cmd" osfamily="windows" dir="${data.writablehome}/logs" spawn="false">
      <arg line="/c echo. > usage.log"/>
    </exec>
    <exec executable="cmd" osfamily="windows" dir="${data.writablehome}/logs" spawn="false">
      <arg line="/c echo. > server.log"/>
    </exec>

    <!-- call reset-logs for solr -->
    <if><bool><istrue value="${with.solr}"/></bool>
    <ant dir="${solr-ext.home}" target="reset-logs" inheritAll="false"/>
    <!--
    <exec executable="ant" os="${os.unix}" dir="${solr-ext.home}" spawn="false" failonerror="false">
      <arg value="reset-logs"/>
      </exec>-->
    </if>
	
    <if>
      <bool><available file="${data.writablehome}/logs/tmp" type="dir"/></bool>
      <delete>
	<fileset dir="${data.writablehome}/logs/tmp" includes="**/*"/>
      </delete>
    </if>
  </target>

  <!-- ========== Target Group: Runtime wrapper targets ====================== -->
    <target name="install-runtime" depends="init,configure-packages,configure-core,compile-web,compile-packages,compile-core,compile-classpath-jars,unpack-cgimodule"
    description="Install (configure, compile, install) the runtime system. Needs either common-src or collection-building to have been installed first." />

    <target name="clean-runtime" depends="init,clean-packages,clean-core,clean-classpath-jars"
    description="Remove all old compiled code for the runtime system."/>
 <target name="distclean-runtime" depends="init,distclean-packages,clean-core,clean-classpath-jars"
    description="Remove all compiled code and also any Makefiles etc generated during configure-c++. Includes runtime."/>

 <target name="svnupdate-runtime" depends="init,svnupdate-packages,svnupdate-core"/>

  <!-- =========== Target Group: Core source targets ============================== -->
  <!-- core targets refer to the core gsdl3 java src -->
  
  <target name="prepare-core"/>
  
  <target name="configure-core"/>

  <target name="update-core" depends="init,svnupdate-core,clean-core,compile-core"
	  description="Update only the Greenstone core" />
  
  <target name="svnupdate-core" unless="nosvn.mode">
    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="update"/>
      <arg value="${basedir}"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>
  </target>
  
  <target name="clean-core"
	  description="Clean only the Greenstone core">
    <!-- should this delete the gsdl3.jar from web/WEB-INF?? -->
    <delete dir="${build.home}"/>
  </target>
  
  <target name="compile-core" depends="needs-gs3-devel,init" 
	  description="Compile only the Greenstone core">
<!--    <echo message="webswing.enabled = ${webswing.enabled}"/>
    <echo message="jni.enabled = ${jni.enabled}"/> -->
    <mkdir dir="${build.home}"/>
    
    <property name="gsprefix" value=""/>	  
    <javac srcdir="${java.src.home}"
	   destdir="${build.home}"
	   includeantruntime="${compile.includeantruntime}"
	   debug="${compile.debug}"
	   deprecation="${compile.deprecation}"
	   optimize="${compile.optimize}"
	   encoding="${compile.encoding}">
      <compilerarg line="${compile.javac.flags}"/>
      <classpath>
        <path refid="compile.classpath"/>
      </classpath>
      <exclude name="org/greenstone/gsdl3/service/GS2MGPPRetrieve.java" unless="jni.enabled"/>
      <exclude name="org/greenstone/gsdl3/service/GS2MGPPSearch.java" unless="jni.enabled"/>
      <exclude name="org/greenstone/gsdl3/service/GS2MGSearch.java" unless="jni.enabled"/>
      <exclude name="org/greenstone/gsdl3/service/GS2MGRetrieve.java" unless="jni.enabled"/>
      <exclude name="org/greenstone/gsdl3/service/GoogleNgramMGPPSearch.java" unless="jni.enabled"/>
      <exclude name="org/greenstone/gsdl3/service/PhindPhraseBrowse.java" unless="jni.enabled"/>
      <exclude name="org/greenstone/gsdl3/util/GDBMWrapper.java" unless="jni.enabled"/>
      <exclude name="org/greenstone/applet/phind/**" unless="webswing.enabled"/>
      <exclude name="org/greenstone/applet/GsdlCollageApplet/**" unless="webswing.enabled"/>
      <exclude name="org/greenstone/gsdl3/auth/oidc/**"/>	  
    </javac>
    
    <jar destfile="${build.home}/gsdl3.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/gsdl3/**"/>
        <exclude name="**/Test.class"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <copy file="${build.home}/gsdl3.jar" todir="${web.lib}"/>

    <antcall target="googlesignin-jar-with-dependencies" />
    <copy file="${build.home}/googlesignin-jdbcrealm.jar" todir="${catalina.home}/lib"/>

    <jar destfile="${build.home}/gsdl3-wordpress-jdbcrealm.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/gsdl3/auth/realm/**"/>
	<include name="org/greenstone/gsdl3/util/AuthenticationHelper.class"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <copy file="${build.home}/gsdl3-wordpress-jdbcrealm.jar" todir="${catalina.home}/lib"/>
    <copy file="web/WEB-INF/lib/jbcrypt-0.4.jar"             todir="${catalina.home}/lib"/>
    <copy file="web/WEB-INF/lib/commons-codec-1.21.0.jar"       todir="${catalina.home}/lib"/>
    <!--<copy file="web/WEB-INF/lib/commons-codec-1.7.jar"       todir="${catalina.home}/lib"/>-->
    
    <!--
    <copy file="web/WEB-INF/lib/mysql-connector-j-8.4.0.jar" todir="${catalina.home}/lib"/>
    -->
    
    <jar destfile="${build.home}/gutil.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/util/**"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <copy file="${build.home}/gutil.jar" todir="${web.lib}"/>


    <!-- Greenstone Administrator Interface -->
    <jar destfile="${build.home}/GAI.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/admin/**"/>
	<include name="org/greenstone/util/**"/>
	<include name="org/greenstone/gsdl3/util/**"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <copy file="${build.home}/GAI.jar" todir="${web.lib}"/>
    <copy file="${build.home}/GAI.jar" todir="${admin.dir}"/>

    <mkdir dir="${web.applet}"/>

    <!-- phind and collage don't work if webswing is not enabled -->
    <if><bool><istrue value="${with.webswing}"/></bool> 
    <jar destfile="${build.home}/phind.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/applet/phind/**"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <copy file="${build.home}/phind.jar" todir="${web.applet}"/>

    <jar destfile="${build.home}/GsdlCollageApplet.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/applet/GsdlCollageApplet/**"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <copy file="${build.home}/GsdlCollageApplet.jar" todir="${web.applet}"/>

    <!-- phind and GsdlCollage also need webswing-api.jar to be in web/applet -->
    <copy file="${ext.data.writablehome}/webswing/api/webswing-api.jar" todir="${web.applet}"/>

    <!-- phind also needs xercesImpl.jar and xml-apis.jar to be in web/applet -->        
   <if>
      <bool><istrue value="${tomcat.islocal}"/></bool>
      <if><bool><available file="${catalina.home}/lib/xercesImpl.jar"/></bool><!-- moved for solr -->
	<copy file="${catalina.home}/lib/xercesImpl.jar" todir="${web.applet}"/>
	<copy file="${catalina.home}/lib/xml-apis.jar" todir="${web.applet}"/>
	
	<else><!-- get from default GS3 web lib location-->
	  <copy file="${web.lib}/xercesImpl.jar" todir="${web.applet}"/>
	  <copy file="${web.lib}/xml-apis.jar" todir="${web.applet}"/>
	</else>
      </if>
    </if>
    </if>

    <!-- skip anttasks for now
    <jar destfile="${build.home}/anttasks.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/anttasks/**"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <copy file="${build.home}/anttasks.jar" todir="${basedir}/lib/java"/>-->
    <jar destfile="${build.home}/gsdl3test.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/gsdl3/**/*Test.class"/>
        <include name="org/greenstone/testing/**"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <jar destfile="${build.home}/server.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/server/**"/>
        <include name="org/greenstone/util/**"/>
      </fileset>
      <fileset file="${basedir}/resources/java/server.properties"/>
      <manifest>
        <attribute name="Built-By" value="${user.name}"/>
      </manifest>
    </jar>
    <copy file="${build.home}/server.jar" todir="${basedir}"/>
  </target>

  <target name="googlesignin-checkforchanges" >
    <uptodate property="googlesignin-nochanges" targetfile="${build.home}/googlesignin-jdbcrealm.jar">
      <srcfiles dir="${build.home}" includes="org/greenstone/gsdl3/GoogleSigninJDBCRealm.class"/>
    </uptodate>
  </target>

  <target name="googlesignin-jar-with-dependencies" depends="init,googlesignin-checkforchanges" unless="googlesignin-nochanges">
    <path id="googlesignin-jar-dependencies">
      <fileset dir="${web.lib}">
	<includesfile name="${jarsupport.home}/google-api-client-jars/jar-list.txt"/>
      </fileset>
    </path>
    
    <unzip dest="${web.lib}/unjarred">
      <patternset>
        <exclude name="META-INF/*.SF"/>
      </patternset>
      <!-- fileset to include ... -->
      <path refid="googlesignin-jar-dependencies"/>
    </unzip>

    <path id="googlesignin-unjarred">
      <fileset dir="${web.lib}/unjarred">
      </fileset>
    </path>

    <jar destfile="${build.home}/googlesignin-jdbcrealm.jar">
	<fileset dir="${build.home}">
          <include name="org/greenstone/gsdl3/GoogleSigninJDBCRealm.class"/>
	</fileset>
	<fileset dir="${java.src.home}">
          <include name="org/greenstone/gsdl3/mbeans-descriptor.xml"/>
	</fileset>
	<path refid="googlesignin-unjarred" />
	<manifest>
          <attribute name="Built-By" value="${user.name}" />
	</manifest>
    </jar>
    <delete dir="${web.lib}/unjarred" quiet="true"/>
	
  </target>

  <!-- === Eclipse targets == -->
  <target name="setup-for-eclipse">

    <filter token="gsdlhome" value="${gs2build.home}"/>
    <filter token="gsdl3srchome" value="${basedir}"/>
    <filter token="gsdl3home" value="${basedir}/web"/>

    <if>
      <bool><not><available file="${basedir}/TransformingLibrary.launch"/></not></bool>      	
      <copy file="${basedir}/TransformingLibrary.launch.in" tofile="${basedir}/TransformingLibrary.launch" filtering="true" overwrite="true"/>
    </if>
    <!--
	<if>
	<bool><not><available file="${basedir}/LibraryCommandline.launch"/></not></bool>      	
	<copy file="${basedir}/LibraryCommandline.launch.in" tofile="${basedir}/LibraryCommandline.launch" filtering="true" overwrite="true"/>
	</if>
    -->
  </target>
  
  <!-- ==========Target Group:  Packages targets ================================ -->
  <!-- these targets refer to the greenstone source packages - these need 
  updating less often, so are in  separate targets to the core -->

  <target name="prepare-wget-for-windows">
    <exec executable="svn" osfamily="windows" dir="${basedir}/bin/windows">
      <arg line="${global-svn-args}"/>
      <arg value="export"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
      <arg value="${svn.root}/main/${branch.path}/binaries/windows/bin/wget.exe"/> 
    </exec>
    <exec executable="svn" osfamily="windows" dir="${basedir}/bin/windows">
      <arg line="${global-svn-args}"/>
      <arg value="export"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
      <arg value="${svn.root}/main/${branch.path}/binaries/windows/bin/wgetrc"/>
      <arg value="wgetrc"/>
    </exec>

  </target>
  <target name="prepare-packages" depends="init"/>
  
  <target name="update-packages" depends="init,svnupdate-packages,configure-packages,clean-packages,compile-packages"
    description="Update only the source packages"/>

  <target name="svnupdate-packages" unless="nosvn.mode"> 
    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="update"/>
      <arg value="${src.packages.home}"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>
  </target>

  <target name="prepare-binaries" depends="prepare-bins-unix"/>

  <target name="prepare-bins-unix" depends="init" if="current.os.isunix">
    <condition property="bitness.suffix" value="64" else="32">
      <matches string="${os.arch}" pattern="64"/>
    </condition>
    <condition property="os.folder.name" value="mac" else="linux"> <!-- checkout from dir: mac or linux -->
      <equals arg1="${os.bin.dir}" arg2="darwin" trim="true"/>
    </condition>    
    <mkdir dir="${gs2build.home}/bin/${os.bin.dir}"/> <!-- mkdir darwin or linux -->
    <exec executable="svn" dir="${gs2build.home}/bin/${os.bin.dir}">
      <arg line="${global-svn-args}"/>
      <arg value="export"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
      <arg value="${svn.root}/main/${branch.path}/binaries/${os.folder.name}/yaz/yaz-client-x${bitness.suffix}"/> <!-- checkout from dir: mac or linux -->
      <arg value="yaz-client"/>
    </exec>
  </target>
  
  <target name="configure-packages" depends="init,configure-javagdbm" 
    description="Configure only the packages."/>
  
  <target name="configure-javagdbm" if="${with.jni}">
    <echo>
      Configuring JavaGDBM
    </echo>
    
    <exec executable="${javagdbm.home}/configure" os="${os.unix}" dir="${javagdbm.home}" failonerror="true">
      <arg value="--prefix=${basedir}"/>
      <arg value="--libdir=${lib.jni}"/>
      <arg value="--with-gdbm=${gdbm.home}"/>
      <arg line="${cross.configure.args}"/>
    </exec>    
  </target>

  <target name="clean-packages" depends="init,clean-javagdbm" description="Clean only the packages"/>

  <target name="clean-javagdbm" depends="init">
    <if><bool><available file="${javagdbm.home}/Makefile"/></bool>
    <exec executable="make" os="${os.unix}"
      dir="${javagdbm.home}" failonerror="true">
      <arg value="clean"/>
    </exec>
    </if>
  </target>
  
  <target name="distclean-packages" depends="init,distclean-javagdbm" description="Distclean only the packages"/>

  <target name="distclean-javagdbm" depends="init">
    <if><bool><available file="${javagdbm.home}/Makefile"/></bool>
    <exec executable="make" os="${os.unix}"
      dir="${javagdbm.home}" failonerror="true">
      <arg value="distclean"/>
    </exec>
    </if>
  </target>
  
  <target name="compile-packages" depends="init" description="Compile only the source packages">
    <!-- Search4j -->
    <antcall target="compile-search4j"/>
    <!-- javagdbm -->
    <antcall target="compile-javagdbm"/>
  </target>

  <target name="compile-javagdbm" description="Compile JavaGDBM" if="${with.jni}">
    
    <!-- unix: -->
    <echo>compile javagdbm</echo>
    <exec executable="make" os="${os.unix}" dir="${javagdbm.home}" failonerror="true">
      <arg value="JAVACOPTIONS=-encoding UTF8"/>
      <env key="JAVA_HOME" path="${env.JAVA_HOME}"/>
    </exec>
    <exec executable="make" os="${os.unix}" dir="${javagdbm.home}" failonerror="true">
      <arg value="install"/>
    </exec>

    <!-- windows: Calling without the "compile" argument first will run winMake.bat with 
         "all" which will then perform both the compile AND link targets in jni/win32.mak 
         (thereby also generating gdbmjava.dll). Then we run the same command with 
         the "install" argument to copy the gdbmjava.dll into the correct location. -->
    <echo>Windows: compile javagdbm</echo>
    <exec executable="${javagdbm.home}/winMake.bat" osfamily="windows" dir="${javagdbm.home}" failonerror="true">	  
      <env key="JAVA_HOME"    path="${env.JAVA_HOME}"/>
      <env key="GSDL3SRCHOME" path="${basedir}"/>
    </exec>
    <exec executable="${javagdbm.home}/winMake.bat" osfamily="windows" dir="${javagdbm.home}" failonerror="true">
      <env key="GSDL3SRCHOME" path="${basedir}"/>
      <arg value="install"/>
    </exec>

    <!-- install the jar file -->
    <echo>Install the javagdbm jar file ${javagdbm.home}/javagdbm.jar ${lib.jni}</echo>
    <copy file="${javagdbm.home}/javagdbm.jar" todir="${lib.jni}"/>
  </target>

  <target name="compile-search4j">

    <!-- windows -->
    <if><bool><istrue value="${current.os.iswindows}"/></bool>
      <exec executable="nmake" dir="${src.packages.home}/search4j" failonerror="true">
        <arg value="/f"/>
        <arg value="win32.mak"/>
        <arg value="BINDIR=${basedir}\bin"/>
        <arg value="LIBDIR=${basedir}\lib"/>
      </exec>
      <exec executable="nmake" dir="${src.packages.home}/search4j" failonerror="true">
        <arg value="/f"/>
        <arg value="win32.mak"/>
        <arg value="install"/>
        <arg value="BINDIR=${basedir}\bin"/>
        <arg value="LIBDIR=${basedir}\lib"/>
      </exec>

    <!-- unix -->
    <else><if><bool><istrue value="${current.os.isunix}"/></bool>
      <exec executable="${src.packages.home}/search4j/configure" dir="${src.packages.home}/search4j" failonerror="true">
        <arg value="--bindir=${basedir}/bin"/>
        <arg value="--libdir=${basedir}/lib"/>
        <arg value="${static.arg}"/>
        <arg line="${cross.configure.args}"/>
      </exec>
      <exec executable="make" dir="${src.packages.home}/search4j" failonerror="true"/>
      <exec executable="make" dir="${src.packages.home}/search4j" failonerror="true">
        <arg value="install"/>
      </exec>

    <!-- else warn -->
    <else>
      <fail>this target does not support the current os</fail>

    </else></if></else></if>

  </target>

  <target name="install-auxiliary-jar-files" depends="init">

    <if>
      <bool><available file="${mg.home}/mg.jar"/></bool>
      <copy file="${mg.home}/mg.jar" todir="${lib.jni}"/>
    </if>

    <if>
      <bool><available file="${mgpp.home}/mgpp.jar"/></bool>
      <copy file="${mgpp.home}/mgpp.jar" todir="${lib.jni}"/>
    </if>

    <copy file="${lucene.home}/LuceneWrapper4.jar" todir="${web.lib}"/>    
  </target>
  
  <target name="install-jni-files" depends="init" if="${with.jni}">
    <antcall target="install-jni-files-linux"/>
    <antcall target="install-jni-files-windows"/>
    <antcall target="install-jni-files-macos"/>
  </target>

  <target name="install-jni-files-linux" depends="init" if="current.os.isunixnotmac">

    <if>

      <bool><equals arg1="${os.bin.dir}" arg2="windows"/></bool>
      <!-- cross compiling to windows -->
      <copy file="${mg.home}/jni/mgretrievejni.dll" todir="${lib.jni}"/>
      <copy file="${mg.home}/jni/mgsearchjni.dll" todir="${lib.jni}"/>
      <copy file="${mg.home}/jni/mgpassjni.dll" todir="${lib.jni}"/>
      <copy file="${mgpp.home}/jni/mgppretrievejni.dll" todir="${lib.jni}"/>
      <copy file="${mgpp.home}/jni/mgppsearchjni.dll" todir="${lib.jni}"/>
      <copy file="${mgpp.home}/jni/mgpppassjni.dll" todir="${lib.jni}"/>
      
      <else>
	<!-- otherwise do the usual Unix copies -->
	<copy file="${mg.home}/jni/libmgretrievejni.so" todir="${lib.jni}"/>
	<copy file="${mg.home}/jni/libmgsearchjni.so" todir="${lib.jni}"/>
	<copy file="${mg.home}/jni/libmgpassjni.so" todir="${lib.jni}"/>
	<copy file="${mgpp.home}/jni/libmgppretrievejni.so" todir="${lib.jni}"/>
	<copy file="${mgpp.home}/jni/libmgppsearchjni.so" todir="${lib.jni}"/>
	<copy file="${mgpp.home}/jni/libmgpppassjni.so" todir="${lib.jni}"/>
      </else>
    </if>


  </target>
  <target name="install-jni-files-windows" depends="init" if="current.os.iswindows">
    <copy file="${mg.home}/jni/mgretrievejni.dll" todir="${lib.jni}"/>
    <copy file="${mg.home}/jni/mgsearchjni.dll" todir="${lib.jni}"/>
    <copy file="${mg.home}/jni/mgpassjni.dll" todir="${lib.jni}"/>
    <copy file="${mgpp.home}/jni/mgppretrievejni.dll" todir="${lib.jni}"/>
    <copy file="${mgpp.home}/jni/mgppsearchjni.dll" todir="${lib.jni}"/>
    <copy file="${mgpp.home}/jni/mgpppassjni.dll" todir="${lib.jni}"/>
  </target>
  <target name="install-jni-files-macos" depends="init" if="current.os.ismac">
    <copy file="${mg.home}/jni/libmgretrievejni.jnilib" todir="${lib.jni}"/>
    <copy file="${mg.home}/jni/libmgsearchjni.jnilib" todir="${lib.jni}"/>
    <copy file="${mg.home}/jni/libmgpassjni.jnilib" todir="${lib.jni}"/>
    <copy file="${mgpp.home}/jni/libmgppretrievejni.jnilib" todir="${lib.jni}"/>
    <copy file="${mgpp.home}/jni/libmgppsearchjni.jnilib" todir="${lib.jni}"/>
    <copy file="${mgpp.home}/jni/libmgpppassjni.jnilib" todir="${lib.jni}"/>
  </target>
  
  <!-- ======== Target Group: common-src targets =================================-->
  <!-- these are used to get common-src (for indexers, gdbm, sqlite etc) when
  collection building is not enabled -->

  <target name="install-common-src" depends="init" 
    description="Install (configure, compile, install) only the common-src package (shared code from Greenstone 2). This is needed only if collection building has been disabled" >
    <antcall target="configure-common-src">
      <param name="set.ld.path" value="true"/>
    </antcall>
    <antcall target="compile-common-src">
      <param name="set.ld.path" value="true"/>
    </antcall>
    <antcall target="install-auxiliary-jar-files"/>
    <antcall target="install-jni-files"/>
  </target>

  <target name="update-common-src" depends="init" if="collection.building.disabled">
  </target>

  <target name="svnupdate-common-src" depends="init" if="collection.building.disabled" unless="nosvn.mode">
    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="update"/>
      <arg value="${common.src.home}"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>
  </target>

  <target name="prepare-common-src" depends="init" if="collection.building.disabled" unless="common.src.present">
    <antcall target="checkout-common-src"/>
    <antcall target="unzip-windows-packages"/>
  </target>

  <target name="checkout-common-src" depends="init" if="collection.building.disabled" unless="nosvn.mode">
    <echo>checking out common-src</echo>
    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="checkout"/>
      <arg value="${svn.root}/main/${branch.path}/greenstone2/common-src"/>
			<arg value="common-src"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>
  </target>
    
  <target name="configure-common-src" depends="envbased-gnomelib-allargs-prop,init">
<!--
    <echo>cross configure args: ${cross.configure.args}</echo>
-->
    <exec executable="${common.src.home}/configure" os="${os.unix}"
      dir="${common.src.home}" failonerror="true">
      <arg value="--prefix=${common.src.binhome}"/> <!-- what value to use?? -->
      <arg value="--bindir=${common.src.binhome}/bin/${os.bin.dir}"/> <!-- what value to use?? -->
      <arg line="${gs2.opt.args}"/>
      <arg line="${static.arg}"/>
      <arg line="${cross.configure.args}"/>
      <arg line="${allargs}"/>
    </exec>
  </target>

  <target name="clean-common-src" depends="envbased-devel-props,init">
    <!-- unix:  -->
    <if><bool><available file="${common.src.home}/Makefile"/></bool>
    <exec executable="make" os="${os.unix}" dir="${common.src.home}" failonerror="true">
      <arg value="clean"/>
    </exec>
    </if>
    <!-- windows: -->    
    <exec executable="nmake" dir="${common.src.home}" osfamily="windows" failonerror="true">
      <arg value="/f"/>
      <arg value="win32.mak"/>	  
      <arg value="clean"/>
	  <arg value="VISUAL_STUDIO_MAJORVERSION=${visualstudio.majorversion}"/>
      <arg value="GSDLHOME=${common.src.binhome}"/>
    </exec>
  </target>

  <target name="distclean-common-src" depends="envbased-devel-props,init">
    <!-- unix:  -->
    <if><bool><available file="${common.src.home}/Makefile"/></bool>
    <exec executable="make" os="${os.unix}" dir="${common.src.home}" failonerror="true">
      <arg value="distclean"/>
    </exec>
    </if>
    <!-- windows: distclean = clean + remove configure products (remove makefiles). But on Windows there is no removing makefiles, so we just call clean -->    
    <exec executable="nmake" dir="${common.src.home}" osfamily="windows" failonerror="true">
      <arg value="/f"/>
      <arg value="win32.mak"/>
      <arg value="clean"/>
	  <arg value="VISUAL_STUDIO_MAJORVERSION=${visualstudio.majorversion}"/>
      <arg value="GSDLHOME=${common.src.binhome}"/>
    </exec>
  </target>
  <target name="compile-common-src" depends="needs-gs3-devel,envbased-devel-props,init">
    <!-- unix: -->
    <exec executable="make" os="${os.unix}" dir="${common.src.home}" failonerror="true">
      <arg value="${gs2.compile.target}"/>
      <arg value="JAVA_HOME=${env.JAVA_HOME}"/>      
    </exec>
    <!-- windows: -->
    <exec executable="nmake" dir="${common.src.home}" osfamily="windows" failonerror="true">
      <arg value="/f"/>
      <arg value="win32.mak"/>
      <arg value="VISUAL_STUDIO_MAJORVERSION=${visualstudio.majorversion}"/>
      <arg value="JAVA_HOME=${env.JAVA_HOME}"/>      

      <arg value="GSDLHOME=${common.src.binhome}"/>
      <arg value="ENABLE_JNI=${gs2.windows.enablejni}"/>
      <arg value="ENABLE_MG=${gs2.windows.enablemg}"/>
      <arg value="ENABLE_MGPP=${gs2.windows.enablemgpp}"/>
      <arg value="USE_GDBM=${gs2.windows.usegdbm}"/>
      <arg value="USE_SQLITE=${gs2.windows.usesqlite}"/>
    </exec>
  </target>
  
  <!-- ======= Target Group: collection-building targets ===========================-->

  <target name="install-collection-building" depends="init" if="collection.building.enabled"
    description="Install (configure, compile, install) the Greenstone 2 collection building package." >
    <antcall target="configure-collection-building"/>
    <antcall target="tweak-makefiles" />
    <antcall target="compile-collection-building"/>
  </target>
    
  <target name="update-collection-building" if="collection.building.enabled" 
    depends="init,svnupdate-collection-building,gs2build-edit-setup-bat,configure-common-src,clean-common-src,compile-common-src,clean-collection-building,configure-collection-building,compile-collection-building"
    description="Update (SVN update, configure, compile etc) only the collection building components"/>

  <target name="svnupdate-collection-building" if="collection.building.enabled" depends="init,svnupdate-gs2build,svnupdate-gli" unless="nosvn.mode"
    description="SVN update the collection building components">
  </target>

  <target name="prepare-collection-building" depends="init,prepare-gs2build,prepare-gli" if="collection.building.enabled">
  </target>

  <target name="configure-collection-building" depends="init,configure-build-src" if="collection.building.enabled"
    description="Configure the collection building components">
  </target>

  <target name="clean-collection-building" depends="init,clean-gli,clean-build-src" 
    description="Clean only the collection building components" 
    if="collection.building.enabled"/>

  <target name="distclean-collection-building" depends="init,clean-build-src,distclean-build-src" 
    description="Distclean only the collection building components" 
    if="collection.building.enabled"/>

  <target name="compile-collection-building" depends="needs-gs3-devel,envbased-devel-props,init,compile-build-src,compile-gli" if="collection.building.enabled"
    description="Compile only the collection building components">
    <!-- make install for common-src -->
    <!-- unix: -->
    <exec executable="make" os="${os.unix}" dir="${common.src.home}" failonerror="true">
      <arg value="${gs2.install.target}"/>
    </exec>
      
    <!-- windows: -->
    <exec executable="nmake" dir="${common.src.home}" osfamily="windows" failonerror="true">
      <arg value="/f"/>
      <arg value="win32.mak"/>
      <arg value="install"/>
      <arg value="VISUAL_STUDIO_MAJORVERSION=${visualstudio.majorversion}"/>
      <arg value="GSDLHOME=${gs2build.home}"/>
      <arg value="ENABLE_JNI=${gs2.windows.enablejni}"/>
      <arg value="ENABLE_MG=${gs2.windows.enablemg}"/>
      <arg value="ENABLE_MGPP=${gs2.windows.enablemgpp}"/>
      <arg value="USE_GDBM=${gs2.windows.usegdbm}"/>
      <arg value="USE_SQLITE=${gs2.windows.usesqlite}"/>
	  <!--
      <arg value="ENABLE_JNI=${gs2.windows.enablejni}"/>
      <arg value="USE_SQLITE=0"/>--> <!-- why is this not on by default? -->
    </exec>

    <!-- install gs2build indexers for windows -->
      <if>
	<bool><istrue value="${current.os.iswindows}"/></bool> 
	<copy todir="${gs2build.home}/bin/windows">
	  <fileset dir="${gs2build.home}/common-src/indexers/bin">
	    <include name="*.*"/>
	  </fileset>
	</copy>
      </if>

    <!-- LuceneWrapper jar file not installed by default -->
    <mkdir dir="${gs2build.home}/bin/java"/>
    <copy file="${lucene.home}/LuceneWrapper4.jar" todir="${gs2build.home}/bin/java"/>

  </target>

  <!-- ==========Target Group: GLI targets ================================= -->
  
  <target name="svnupdate-gli" if="collection.building.enabled" depends="init" unless="nosvn.mode">

    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="update"/>
      <arg value="${gli.home}"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>

  </target>

  <target name="prepare-gli" depends="init" if="collection.building.enabled" unless="gli.present">
    <!-- checkout -->
    <if><bool><and><not><istrue value="${nosvn.mode}"/></not><isset property="with.gli.and.gems"/></and></bool>

    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="checkout"/>
      <arg value="${svn.root}/main/${branch.path}/gli"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>

    </if>
  </target>
  
  <target name="clean-gli" depends="init" if="collection.building.enabled">
    <!-- gli -->
    <property name="gli.home" value="${basedir}/gli"/>
    <!-- linux -->
    <exec executable="clean.sh" os="${os.unix}" dir="${gli.home}"
      resolveExecutable="true" failonerror="true"/>
    <!-- windows -->
    <exec executable="clean.bat" osfamily="windows" dir="${gli.home}"
      resolveExecutable="true" failonerror="true"/>
  </target>
 
  <target name="compile-gli" depends="needs-gs3-devel,init" if="collection.building.enabled">
    <if><bool><isset property="with.gli.and.gems"/></bool>
      <!-- gli -->
      <property name="gli.home" value="${basedir}/gli"/>

      <!-- linux -->
      <exec executable="makegli.sh" os="${os.unix}" dir="${gli.home}" resolveExecutable="true" failonerror="true"/>
      <!--remote gli-->
      <exec executable="makejar.sh" os="${os.unix}" dir="${gli.home}"
	    resolveExecutable="true" failonerror="true"/>
      <!-- windows -->
      <exec executable="makegli.bat" osfamily="windows" dir="${gli.home}" resolveExecutable="true" failonerror="true"/>
      <!--remote gli-->
      <exec executable="makejar.bat" osfamily="windows" dir="${gli.home}"
	    resolveExecutable="true" failonerror="true"/> 
      <copy file="${gli.home}/GLIServer.jar" todir="${gs2build.home}/bin/java" />
      <if><bool><istrue value="${with.webswing}"/></bool>
      <copy file="${gli.home}/GLI.jar" todir="${ext.data.writablehome}/webswing/apps/gli" />
      </if>
    </if>
  </target>
  
  <target name="install-gli-for-webswing"  description="Install GLI.jar into webswing ext. If GLI needs compiling, run the compile-gli target first">
    <if><bool><istrue value="${with.webswing}"/></bool>
    <mkdir dir="${ext.data.writablehome}/webswing/apps/gli"/>
    <copy file="${gli.home}/GLI.jar" todir="${ext.data.writablehome}/webswing/apps/gli" />
    <copy file="${gli.home}/classes/images/gatherer.png" todir="${ext.data.writablehome}/webswing/apps/gli" />
    <copy todir="${ext.data.writablehome}/webswing/apps/gli/help" >  
      <fileset dir="${gli.home}/help" includes="**"/>  
    </copy> 
    <copy todir="${ext.data.writablehome}/webswing/apps/gli/metadata" >  
      <fileset dir="${gli.home}/metadata" includes="**"/>  
    </copy>
    </if>
  </target>
  
  <target name="gli" description="Run the Greenstone Librarian Interface" depends="init" if="collection.building.enabled">
    <exec executable="${basedir}/gli/gli.sh" os="${os.linux},${os.solaris}" dir="${basedir}/gli" spawn="true">
      <env key="gsdl3path" path="${basedir}"/>
      <env key="gsdlpath" path="${gs2build.home}"/>
    </exec>
    <exec executable="${basedir}/gli/gli.sh" os="${os.mac}" dir="${basedir}/gli" spawn="true">
      <env key="gsdl3path" path="${basedir}"/>
      <env key="gsdlpath" path="${gs2build.home}"/>
      <env key="DYLD_LIBRARY_PATH" path="${env.DYLD_LIBRARY_PATH}:${gdbm.home}/lib"/>
    </exec>
    <exec executable="${basedir}/gli/gli.bat" osfamily="windows" dir="${basedir}/gli" spawn="true">
      <env key="GSDL3PATH" path="${basedir}"/>
      <env key="GSDLPATH" path="${gs2build.home}"/>
    </exec>
    <echo>Running GLI from Ant means that you don't get to see any of the terminal output. If you have problems with GLI and want to see the output, please run the script gli.sh/bat from the greenstone3/gli directory.
    </echo>
  </target>

  <!-- ========= Target Group: gs2build targets =========================== -->

  <target name="svnupdate-gs2build" if="collection.building.enabled" depends="init" unless="nosvn.mode">
    <echo>svn updating gs2build</echo>
    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="update"/>
      <arg value="${gs2build.home}"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>
  </target>
    
  <target name="prepare-gs2build" depends="init" if="collection.building.enabled" unless="gs2build.present">
    <antcall target="checkout-gs2build"/>
    <antcall target="prepare-pdfbox"/>
    <antcall target="prepare-imagemagick"/> <!-- has to be done before calling prepare-gnome-lib -->
    <antcall target="prepare-gnome-lib"/>
    <antcall target="unzip-windows-packages"/>
    <antcall target="checkout-winbin"/>
    <antcall target="get-windows-binaries"/>
    <antcall target="delete-winbin"/>
  </target>

  <target name="checkout-gs2build" depends="init" if="collection.building.enabled" unless="nosvn.mode">
    <echo>checking out gs2build</echo>
    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="checkout"/>
      <arg value="${svn.root}/main/${branch.path}/gs2build"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>
  </target>

  <!-- Gets the PDFBox extension into gs2build/ext if checkout.pdfbox.ext is set to true in build.properties 
       (which it is by default) -->
  <target name="prepare-pdfbox" depends="init" if="collection.building.enabled">
    <if>
      <bool>
	<istrue value="${checkout.pdfbox.ext}"/>
      </bool>
      
      <property name="pdfbox.ext.dir" value="${gs2build.home}/ext/pdf-box"/>
      <condition property="pdfbox.ext.present">
	<available file="${pdfbox.ext.dir}" type="dir" />
      </condition>
      
      <!-- get the pdfbox tar.gz file if we don't already have it and extract it if there's no pdf-box directory in gs2build/ext-->
      <if>
	<bool>
	  <not><istrue value="${pdfbox.ext.present}"/></not>
	</bool>

	<if>
	  <bool>
	    <not><istrue value="${gs2build.home}/ext/pdf-box-java.tar.gz"/></not>
	  </bool>

	  <echo>Checking out the PDFBox extension into the GSDLHOME extension area</echo>
	  <exec executable="svn">
            <arg line="${global-svn-args}"/>
	    <arg value="export"/>
	    <arg value="${svn.root}/main/${branch.path}/gs2-extensions/pdf-box/pdf-box-java.tar.gz"/>
	    <arg value="${gs2build.home}/ext/pdf-box-java.tar.gz"/>
	  </exec>
	</if>	

	<echo>Extacting the PDFBox extension into the GSDLHOME extension area</echo>
	<untar compression="gzip" 
	       src="${gs2build.home}/ext/pdf-box-java.tar.gz"
	       dest="${gs2build.home}/ext"/>

	<delete file="${gs2build.home}/ext/pdf-box-java.tar.gz"/>

	<else>
	  <echo>The PDFBox extension already exists at ${pdfbox.ext.dir}</echo>
	</else>	
      </if>

      <else>
	<echo>**** Not preparing the PDFBox extension:</echo>
	<echo>The property checkout.pdfbox.ext in build.properties was not set or was set to false</echo>
      </else>
    </if>
  </target>

  <target name="prepare-imagemagick" depends="init" if="collection.building.enabled">
    <if>
      <bool>
	<istrue value="${checkout.imagemagick.ext}"/>
      </bool>

      <antcall target="checkout-imagemagick"/>
      <!--Compilation of imagemagick now happens during ant install, still before configuring the src code, as before -->

      <else>
	<echo>**** Not preparing imagemagick:</echo>
	<echo>property checkout.imagemagick.ext in build.properties was not set or was set to false</echo>
      </else>
    </if>
  </target>

  <target name="checkout-imagemagick" depends="init" if="collection.building.enabled" unless="nosvn.mode">
    
    <property name="imagemagick.src.dir" value="${gs2build.home}/ext/imagemagick"/>
    <condition property="imagemagick.src.present">
      <available file="${imagemagick.src.dir}" type="dir" />
    </condition>

    <if>
      <bool>
	<not><istrue value="${imagemagick.src.present}"/></not>
      </bool>
      
      <echo>checking out imagemagick source into the extension area</echo>

      <exec executable="svn">
        <arg line="${global-svn-args}"/>
	<arg value="checkout"/>
	<arg value="${svn.root}/main/${branch.path}/gs2-extensions/imagemagick/source"/>
	<arg value="${imagemagick.src.dir}"/>
      </exec>

      <else>
	<echo>imagemagick source code already exists at ${imagemagick.src.dir}</echo>
      </else>
    </if>
  </target>

 <!-- Compile up imagemagick src folder if: the checkout.imagemagick.ext flag is turned on, if the imagick source code exists and if hasn't already been compiled up, then compile it up. Later can check if a gs-specific binary version has been installed already, in which case compilation won't be necessary. -->
  <target name="compile-imagemagick" if="${checkout.imagemagick.ext}">
    
    <property name="imagemagick.src.dir" value="${gs2build.home}/ext/imagemagick"/>
    <property name="imagemagick.compiled.dir" value="${gs2build.home}/ext/imagemagick/${os.bin.dir}"/>

    <condition property="imagemagick.src.present.firstcheck">
      <available file="${imagemagick.src.dir}" type="dir" />
    </condition>
    <condition property="imagemagick.compiled.present">
      <available file="${imagemagick.compiled.dir}" type="dir"/>
    </condition>
    <!--<condition property="imagemagick.bin.present">
      <available file="${basedir}/wherever/the/imgmagick/binary/is/to/be/found" type="dir" />
    </condition>-->

    <!-- imagemagick will only be checked out if the user set the checkout.imagemagick.ext in build.properties -->
    <if>
      <bool>
	<isfalse value="${imagemagick.src.present.firstcheck}"/>
      </bool>
      <antcall target="checkout-imagemagick"/>
    </if>

    <!-- keep track of whether the imagemagick src is now indeed present. Need to know this for a subsequent test -->
    <condition property="imagemagick.src.present">
      <available file="${imagemagick.src.dir}" type="dir" />
    </condition>

    <if>
      <bool>
	<and>
	  <istrue value="${imagemagick.src.present}"/> <!-- imagemagick src code is present -->
	  <isfalse value="${imagemagick.compiled.present}"/> <!-- imagemagick src not compiled yet, so no imagemagick/os subfolder yet -->
	</and>
      </bool>
      <!-- then compile it. Only necessary for mac/linux, since windows has a stable working binary of imagemagick -->
      <exec executable="/bin/bash" dir="${imagemagick.src.dir}" failonerror="true">
        <arg value="CASCADE-MAKE.sh"/>
      </exec>  
    </if>
  </target>

  <target name="clean-imagemagick" if="${checkout.imagemagick.ext}">
    <property name="imagemagick.compiled.dir" value="${gs2build.home}/ext/imagemagick/${os.bin.dir}"/>
    <condition property="imagemagick.compiled.present">
      <available file="${imagemagick.compiled.dir}" type="dir"/>
    </condition>
    <if>
      <bool>
        <istrue value="${imagemagick.compiled.present}"/>
      </bool>
      <!-- then clean it. Only necessary for mac/linux, since windows has a stable working binary of imagemagick -->
      <!-- TODO, check that we don't try to call this on windows -->
      <exec executable="/bin/bash" dir="${imagemagick.src.dir}" failonerror="true">
      <arg value="CASCADE-MAKE.sh"/>
      <arg value="clean"/>
      </exec>
    </if>
  </target>


  <target name="prepare-gnome-lib" depends="init" if="collection.building.enabled" unless="gnome-lib.present">
    <if>
      <bool><istrue value="${checkout.gnomelib.ext}"/></bool>
      <if>
	<bool><istrue value="${current.os.iswindows}"/></bool>
	
	<echo>Skipping prepare-gnome-lib, because compilation is on Windows which checks out binaries that do not rely on gnomelib</echo>
	<else>
	  <!-- current.os.isunix -->

	  <antcall target="checkout-gnome-lib"/>
	  <!--Compilation of gnome-lib happens during ant install, just before configuring (common-src and) build-src-->
	  
	</else>
      </if>
      <else>
	<if><bool><istrue value="${current.os.isunix}"/></bool>
	  <!-- current.os.isunix and checkout.gnomelib.ext not turned on -->
	  <echo>**** Not preparing gnome-lib:</echo>
	  <echo>property checkout.gnomelib.ext in build.properties was not set or was set to false.</echo>
	</if>
      </else>      
    </if>
  </target>


  <target name="checkout-gnome-lib" depends="init" if="collection.building.enabled" unless="nosvn.mode">

    <property name="gnomelib.src.dir" value="${basedir}/gs2build/ext/gnome-lib"/>
    <condition property="gnome.src.present">
      <available file="${gnomelib.src.dir}" type="dir" />
    </condition>

    <if>
      <bool>
	<not><istrue value="${gnome.src.present}"/></not>
      </bool>

      <echo>checking out gnome-lib extension</echo>
      <exec executable="svn">
        <arg line="${global-svn-args}"/>
	<arg value="checkout"/>
	<arg value="${svn.root}/main/${branch.path}/gs2-extensions/gnome-lib/source"/>
	<arg value="${gs2build.home}/ext/gnome-lib"/>
      </exec>

      <else>
	<echo>gnomelib source code already exists at ${gnomelib.src.dir}</echo>
      </else>
    </if>

  </target>

    <!-- If not on windows, then compile up gnome-lib src folder if:
       checkout.gnomelib.ext is turned on,
       and if not using the binary version (gnome-lib-minimal), 
       and if the gnome-lib src folder is not already compiled up. -->
  <target name="compile-gnome-lib" if="${checkout.gnomelib.ext}" unless="current.os.iswindows">

    <!-- http://stackoverflow.com/questions/3290307/sourcing-a-shell-profile-in-an-ant-build-file 
	 TODO: CASCADE-MAKE already sources devel.bash, but we still want to source it more globally, 
	 so that the rest of the GS3 compilation process also has access to those env variables -->
    
    <property name="gnome.lib.src.dir" value="${basedir}/gs2build/ext/gnome-lib"/>
    <property name="gnome.lib.compiled.dir" value="${basedir}/gs2build/ext/gnome-lib/${os.bin.dir}"/>

    <condition property="gnome.src.lib.present.firstcheck" value="true" else="false">
      <available file="${gnome.lib.src.dir}" type="dir" />
    </condition>
    <condition property="gnome.compiled.lib.present" value="true" else="false">
      <available file="${gnome.lib.compiled.dir}" type="dir"/>
    </condition>
    <condition property="gnome.lib.min.present" value="true" else="false">
      <available file="${basedir}/gs2build/ext/gnome-lib-minimal" type="dir" />
    </condition>

    <!-- Make sure to checkout gnome-lib if it was not checked out at this stage 
	 since we're instructed to do so in th pre-condition to this target -->
    <if>
      <bool>
	<and>
	  <isfalse value="${gnome.lib.min.present}"/>
	  <isfalse value="${gnome.src.lib.present.firstcheck}"/>
	</and>
      </bool>
      <antcall target="checkout-gnome-lib"/>
    </if>

    <!-- Keep track of whether we have the gnome-lib src folder now. Need to know this for a subsequent test -->
    <condition property="gnome.src.lib.present" value="true" else="false">
      <available file="${gnome.lib.src.dir}" type="dir" />
    </condition>

    <!--<echo>MIN: ${gnome.lib.min.present}
      SRC LIB: ${gnome.src.lib.present}
      COMPILED: ${gnome.compiled.lib.present}</echo>-->

    <if>
      <bool>
	<and>
	  <isfalse value="${gnome.lib.min.present}"/> <!-- no gnome-lib-minimal binary present -->
	  <istrue value="${gnome.src.lib.present}"/> <!-- gnome-lib folder for compilation is present-->
	  <isfalse value="${gnome.compiled.lib.present}"/> <!-- gnome-lib not yet compiled, so no gnome-lib/os subfolder yet -->
	</and>
      </bool>

      <!-- then compile it. Only necessary for mac/linux, since windows doesn't need gnome lib -->
      <exec executable="/bin/bash" dir="${gnome.lib.src.dir}" failonerror="true">
        <arg value="CASCADE-MAKE.sh"/>
      </exec>  
    </if>
  </target>

  <target name="clean-gnome-lib" if="${checkout.gnomelib.ext}" unless="current.os.iswindows">
  </target>
  
  <target name="checkout-winbin" depends="init" if="current.os.iswindows" 
    unless="nosvn.mode">

    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="checkout"/>
      <arg value="${svn.root}/main/${branch.path}/binaries/windows"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
      <arg value="winbin"/>
    </exec>

  </target>

  <target name="update-winbin" depends="init" if="current.os.iswindows" unless="nosvn.mode">
    <exec executable="svn">
      <arg line="${global-svn-args}"/>
      <arg value="update"/>
      <arg value="winbin"/>
      <arg value="-r"/><arg value="${branch.revision}"/>
    </exec>

  </target>
  
  <target name="get-windows-binaries" depends="init" if="collection.building.enabled.windows">
    <move todir="${gs2build.home}/bin/windows" failonerror="false">
      <fileset dir="${basedir}/winbin/bin"/>
    </move>
  </target>

  <target name="delete-winbin" depends="init" if="collection.building.enabled.windows">
    <delete dir="${basedir}/winbin"/>
  </target>
  
  <target name="unzip-windows-packages" depends="envbased-devel-props,init" if="current.os.iswindows">  
    <unzip src="${common.src.home}/packages/windows/crypt/crypt.zip"
      dest="${common.src.home}/packages/windows/crypt"/>
    <untar compression="gzip" 
      src="${common.src.home}/packages/sqlite/${sqlite.targz.version}"
      dest="${common.src.home}/packages/sqlite"/>
    <untar compression="gzip"
	   src="${common.src.home}/packages/expat/${expat.targz.version}"
	   dest="${common.src.home}/packages/expat"/>
    <untar compression="gzip"
	   src="${common.src.home}/packages/jdbm/${jdbm.targz.version}"
	   dest="${common.src.home}/packages/jdbm"/>
    <if>
      <bool>
		<islessthan arg1="${visualstudio.majorversion}" arg2="14"/>
      </bool>
      <echo>Unzipping Pre-VS14.0 compatible version of iconv</echo>
      <unzip src="${common.src.home}/indexers/packages/windows/iconv/iconv-PRE-VS14.zip"
	         dest="${common.src.home}/indexers/packages/windows/iconv"/>
      <else>
		<echo>Unzipping VS14.0-Plus compatible version of iconv</echo>
		<unzip src="${common.src.home}/indexers/packages/windows/iconv/iconv-VS14-PLUS.zip"
	           dest="${common.src.home}/indexers/packages/windows/iconv"/>
      </else>
    </if>
   
  </target>
    
  <target name="gs2build-edit-setup-bat" if="collection.building.enabled.windows">
    <!-- we want a windows path in the setup.bat file -->
    <pathconvert targetos="windows" property="gs2build.home.windows">
      <path path="${gs2build.home}"/>
    </pathconvert>
    <move file="${gs2build.home}/setup.bat" tofile="${gs2build.home}/setup-tmp.bat">
      <filterset>
        <filter token="gsdlhome" value="${gs2build.home.windows}"/>
      </filterset>
    </move>  
    <move file="${gs2build.home}/setup-tmp.bat" tofile="${gs2build.home}/setup.bat" />
  </target>
  
 
  <target name="clean-build-src" depends="init" if="collection.building.enabled">
    <!-- unix:  -->
    <if><bool><available file="${build.src.home}/Makefile"/></bool>
    <exec executable="make" os="${os.unix}" dir="${build.src.home}" failonerror="true">
      <arg value="clean"/>
    </exec>
    </if>
    <!-- windows: -->    
    <exec executable="nmake" dir="${build.src.home}" osfamily="windows" failonerror="true">
      <arg value="/f"/>
      <arg value="win32.mak"/>
      <arg value="clean"/>
      <arg value="GSDLHOME=${gs2build.home}"/>
    </exec>
  </target>
  
  
  <target name="distclean-build-src" depends="init,clean-build-src" if="collection.building.enabled">
    <!-- unix:  -->
    <if><bool><available file="${build.src.home}/Makefile"/></bool>
    <exec executable="make" os="${os.unix}" dir="${build.src.home}" failonerror="true">
      <arg value="distclean"/>
    </exec>
    </if>
    <!-- windows: distclean = clean + remove configure products (remove makefiles). But on Windows there is no removing makefiles, so we just call clean -->    
    <exec executable="nmake" dir="${build.src.home}" osfamily="windows" failonerror="true">
      <arg value="/f"/>
      <arg value="win32.mak"/>
      <arg value="clean"/>
      <arg value="GSDLHOME=${gs2build.home}"/>
    </exec>
  </target>
 
  <target name="configure-build-src" depends="envbased-gnomelib-allargs-prop,init" if="collection.building.enabled"
    description="Configure the build-src component">
    <exec executable="${build.src.home}/configure" os="${os.unix}"
      dir="${build.src.home}" failonerror="true">
      <arg value="--prefix=${gs2build.home}"/>
      <arg line="${gs2.opt.args} ${static.arg} ${cross.configure.args} ${allargs}"/>
    </exec>
  </target>

  <!-- common-src is done separately and needs to be compiled first -->
  <target name="compile-build-src" depends="needs-gs3-devel,init" if="collection.building.enabled">
    
    <exec executable="make" dir="${build.src.home}" os="${os.unix}" failonerror="true">
    	<!-- <arg line="${ldlpath.arg}"/> -->
        <env key="LD_LIBRARY_PATH" value="${env.LD_LIBRARY_PATH}"/> <!--changed to specifying through 'env' rather than arg with ldlpath.arg -->
    </exec>
	
    <exec executable="make" dir="${build.src.home}" os="${os.unix}" failonerror="true">
	<env key="GSDLOS" value="${os.bin.dir}"/>
	<arg value="install"/>
    </exec>

    <!-- run the setup script -->
    <!-- <exec executable="${compile.windows.c++.setup}" osfamily="windows" failonerror="true"/>-->
      <!--Above does not work: even though vcvars.bat executes, the env changes it makes don't get saved. Need user to run vcvars.bat first before calling ant--> 
      <exec executable="nmake" dir="${build.src.home}" osfamily="windows" failonerror="true">
        <arg value="/f"/>
        <arg value="win32.mak"/>
	<arg value="GSDLHOME=${gs2build.home}"/>
      </exec>
      <exec executable="nmake" dir="${build.src.home}" osfamily="windows" failonerror="true">
        <arg value="/f"/>
        <arg value="win32.mak"/>
        <arg value="install"/>
	<arg value="GSDLHOME=${gs2build.home}"/>
      </exec>
  </target>
  
  <!-- ===========Target Group:  SOLR extension targets =================== -->

  <target name="configure-solr-ext" depends="needs-gs3-setup,init" description="Configure the solr9 extension (regenerates the config files)" if="solr.enabled">
    <ant dir="${solr-ext.home}" target="configure-extension" inheritAll="false">
        <property name="web.home" value="${web.home}"/>
        <property name="web.writablehome" value="${web.writablehome}"/>
        <property name="data.home" value="${data.writablehome}"/>
    </ant>
  </target>

    <!-- helper targets -->
  <target name="check-solr-running" if="solr.enabled">
    <ant dir="${solr-ext.home}" target="status-solr" inheritAll="false"/>
  </target>

  <target name="start-solr" description="Start the solr9 server" if="solr.enabled">
    <ant dir="${solr-ext.home}" target="start-solr" inheritAll="false"/>
  </target>
  
 <target name="stop-solr" description="Stop the solr9 server" if="solr.enabled">
    <ant dir="${solr-ext.home}" target="stop-solr" inheritAll="false">
      <property name="os.unix" value="${os.unix}"/>
    </ant>
  </target>

   <target name="svnupdate-solr-ext" depends="needs-gs3-setup,init" if="solr.enabled" unless="nosvn.mode"
	  description="Svn update of the solr9 extension">
    <ant dir="${solr-ext.home}" target="svnupdate" inheritAll="false">
      <property name="global-svn-args" value="${global-svn-args}"/>
      <property name="branch.revision" value="${branch.revision}"/>
    </ant>
  </target>

  <target name="install-solr-ext" depends="init" description="Install the solr9 extension" if="solr.enabled">
    <ant dir="${solr-ext.home}" target="add-extension" inheritAll="false">
      <property name="web.home" value="${web.home}"/>
      <property name="web.writablehome" value="${web.writablehome}"/>
      <property name="data.home" value="${data.writablehome}"/>
    </ant>
  </target>

  <target name="compile-solr-ext" depends="init" description="Compile the solr9 extension" if="solr.enabled">
    <ant dir="${solr-ext.home}" target="compile" inheritAll="false">
      <property name="web.home" value="${web.home}"/>
      <property name="web.writablehome" value="${web.writablehome}"/>
      <property name="data.home" value="${data.writablehome}"/>
    </ant>
  </target>
  
  <target name="clean-solr-ext" depends="init" if="solr.enabled">
    <ant dir="${solr-ext.home}" target="clean" inheritAll="false"/>
  </target>
  

  <!-- ========== Target Group: Webswing extension Targets ========================= -->
  <target name="configure-webswing-ext" depends="needs-gs3-setup,init" description="Configure the webswing extension (regenerates the config files and reinstalls the war file)">
        <if><bool><istrue value="${with.webswing}"/></bool>
	<property name="dollarsymbol" value="$"/>
	<property name="start_java_file" value="gs_webswing_start_java"/>
	
	<!--
		If we have spaces in the GS3 installation filepath, or particularly spaces in the java/jre filepath,
		then our version of the webswing software needs help in running the greenstone webswing applications:
		We set the jreExecutable webswing property to something other than the default ${java.home}/bin/java
		and we copy our helper script gs_webswing_start_java.bat/.sh into the /tmp or %TMP% location, so that
		this script helper script can be run by webswing from a path without spaces. The script itself will
		ensure the path to java is quoted. (The .bat script has an inactive line to convert to Win short file path)
		https://www.webswing.org/docs/25.1/configure/swing#variable-resolution
		Google Gemini's suggestion to use escaped double quotes in the property jreExecutable, or set jreExecutable
		to a ${java.home}/bin/java embedded as a single JSON array element, and the location Gemini suggested for
		its basic start_java bat file didn't quite help. The former 2 suggestions didn't work. The path to start_java
		bat can't be within a GS3 installation if the GS3 installation has spaces in its filepath.
	-->
	<if><bool><contains string="${java.home}" substring=" "/></bool>
		<echo>java/jre home ${java.home} is in a path with spaces, need to set up webswing to handle this.</echo>
		<echo>About to copy ${webswing-ext.home}/src/gs_webswing_start_java script to temp location</echo>
		<echo>and set the webswing jreExecutable to use this to determine the java path.</echo>
		<if><bool><istrue value="${current.os.iswindows}"/></bool>			
			<property name="jreExecutable.webswing.property" value="${dollarsymbol}{TMP}/${start_java_file}.bat"/>
			<copy file="${webswing-ext.home}/src/${start_java_file}.bat" todir="${env.TMP}" overwrite="true"/>
			<else>
				<property name="jreExecutable.webswing.property" value="/tmp/${start_java_file}.sh"/>
				<copy file="${webswing-ext.home}/src/${start_java_file}.sh" todir="/tmp/${start_java_file}.sh" overwrite="true"/>
			</else>
		</if>
		<else>
			<property name="jreExecutable.webswing.property" value="${dollarsymbol}{java.home}/bin/java"/>
		</else>
	</if>
	
    <if><bool><isset property="webswing-ext.exists"/></bool>
    <echo>Running ant configure-extension in ext.webswing</echo>
    <ant dir="${webswing-ext.home}" target="configure-extension" inheritAll="false">
      <property name="web.home" value="${web.home}"/>
      <property name="web.writablehome" value="${web.writablehome.unix}"/>
      <property name="gsdl3.writablebasedir" value="${gsdl3.writablebasedir.unix}"/>
      <property name="gs3.tomcat.installed.path" value="${catalina.home}"/>
      <property name="gs3.webswing.context" value="${webswing.context}"/>
      <property name="gsdlos" value="${env.GSDLOS}"/>
      <property name="scriptorjavapath" value="${jreExecutable.webswing.property}"/>
      <property name="internal.websocket-baseurl" value="${local.ws.url}"/>
      <property name="internal.servlet-url" value="${internal.servlet-url}"/>
      <property name="external.servlet-url" value="${external.servlet-url}"/>
      <property name="external.context-url" value="${external.context-url}"/>
      <property name="external.webswing-url" value="${external.webswing-url}"/>
      <property name="default.servlet-path" value="${server.default.servlet}"/>
    </ant>
    </if>
  </if>
  </target>


  <target name="set-webswing-env" description="Sets properties needed to run tomcat">
    <if>
      <bool><available file="${ext.data.writablehome}/webswing/etc/catalina-opts-extra.args"/></bool>
      <loadfile property="webswing.catalina.opts" srcfile="${ext.data.writablehome}/webswing/etc/catalina-opts-extra.args" failonerror="true"/>
      <else>
	<property name="webswing.catalina.opts" value=""/>
      </else>
    </if>
  </target>  

    <target name="install-webswing-ext" depends="init">
    <if><bool><istrue value="${with.webswing}"/></bool>
    <ant dir="${webswing-ext.home}" inheritAll="false" target="add-extension">
      <property name="web.home" value="${web.home}"/>
      <property name="web.writablehome" value="${web.writablehome}"/>
      <property name="gsdl3.writablebasedir" value="${gsdl3.writablebasedir}"/>
      <property name="gsdl3.datadir" value="${data.writablehome}"/>
      <property name="gs3.tomcat.installed.path" value="${catalina.home}"/>
      <property name="gs3.webswing.context" value="${webswing.context}"/>

    </ant>
    </if>
  </target>

  <!-- ======================== TESTING Targets ========================= -->
  
  <target name="test" description="Run the (incomplete) JUnit test suite "
    depends="init">
    <mkdir dir="${basedir}/test"/>
    <junit printsummary="withOutAndErr"
      errorproperty="test.failed"
      failureproperty="test.failed"
      fork="${junit.fork}">
      <formatter type="plain"/>
      <classpath>
        <pathelement location="${build.home}/gsdl3test.jar"/>
        <path refid="compile.classpath"/>
      </classpath>
      <test name="${testcase}" if="testcase"/>
      <batchtest todir="${basedir}/test" unless="testcase">
        <fileset dir="${build.home}" includes="**/*Test.class" />
      </batchtest>
    </junit>
    <echo>
      *********************************************
      Test output can be found in directory 'test'
      *********************************************
    </echo>
  </target>

  <target name="compile-javadocs">
    <javadoc packagenames="org.greenstone.*"
           sourcepath="src/java"
           defaultexcludes="yes"
           destdir="docs/javadoc"
           author="true"
           version="true"
           use="true"
           windowtitle="Greenstone3 API">
    <doctitle><![CDATA[<h1>Greenstone3 API</h1>]]></doctitle>
    </javadoc>
  </target>

<!-- ========== Some distribution targets ======================== -->
  <target name="remove-source">
    <if><bool><isset property="with.gli.and.gems"/></bool>
    <delete includeEmptyDirs="true">
      <fileset dir="." defaultexcludes="false">
        <patternset refid="greenstone3.source.component"/>
     </fileset>
    </delete>
    
    <else>
    <delete includeEmptyDirs="true">
      <fileset dir="." defaultexcludes="false">
        <patternset refid="greenstone3.source.no.gli.component"/>
     </fileset>
    </delete>
    </else>
    </if>
  </target>

  <target name="dist-tidy"
    description="'tidies-up' a greenstone3 installation for distribution."
    unless="${properties.keep.src}">

    <!-- delete unneeded things -->
    <delete dir="${packages.home}/axis"/>
    <delete><fileset dir="${packages.home}" includes="*.zip"/></delete>
    <delete file="README-SVN.txt"/>
    <delete file="build.properties.svn"/>

    <!-- delete source files -->
    <antcall target="remove-source"/>

    <!-- create empty directories -->
    <mkdir dir="${web.writablehome}/applet"/>
    <mkdir dir="${data.writablehome}/logs"/>
    <mkdir dir="${data.writablehome}/logs/tmp"/>

	<!-- Lines with ***** are commented out because these files are useful if we want hybrid installations -->
	
    <!-- os specific tidy-ups -->
    <!-- linux, mac -->
    <if><bool><istrue value="${current.os.isunix}"/></bool>
      <!--*****<delete><fileset dir="." includes="*.bat"/></delete>-->
      <if><bool><isset property="with.gli.and.gems"/></bool>
		<!--*****<delete><fileset dir="gli" includes="*.bat"/></delete>-->
      </if>
      <!--*****<delete><fileset dir="gs2build" includes="*.bat"/></delete>-->
      <!--*****<delete><fileset dir="bin/script" includes="*.bat"/></delete>-->
      <delete file="${basedir}/gs2build/win32cfg.h"/>
      <delete file="${basedir}/gs2build/win32.mak"/>
      <delete dir="${basedir}/winutil"/>
      <delete failonerror="false"><fileset dir="${lib.jni}" includes="*.dll"/></delete>

    <!-- windows -->
    <else><if><bool><istrue value="${current.os.iswindows}"/></bool>
      <!--*****<delete><fileset dir="." includes="*.sh,*.bash,*.csh"/></delete>-->
      <if><bool><isset property="with.gli.and.gems"/></bool>
	<!--*****<delete><fileset dir="gli" includes="*.sh,*.bash,*.csh"/></delete>-->
      </if>
      <!--*****<delete><fileset dir="gs2build" includes="*.sh,*.bash,*.csh"/></delete>-->
      <!--*****<delete><fileset dir="bin/script" includes="*.sh,*.bash,*.csh"/></delete>-->
    </if></else></if>

  </target>

  <!-- fix up executable permissions for binary release -->
  <target name="fix-execute-permissions">
    <echo>Setting binaries to executable</echo>
    <chmod perm="775">
      <fileset dir="."><patternset refid="greenstone3.executables"/></fileset>
    </chmod>
  </target>
  
  <!-- fix up executable permissions for source code release -->
  <target name="fix-execute-permissions-source">
    <chmod perm="775">
      <fileset dir="."><patternset refid="greenstone3.source.executables"/></fileset>
    </chmod>
  </target>

  <!-- for macs, set up the .app shortcuts to gsi, gli, client-gli and gems -->
  <target name="gen-mac-shortcuts">    
    <exec executable="uname" dir="${basedir}" failonerror="false"
	  outputproperty="darwin.kernel.version">
      <arg value="-r"/>
    </exec>

    <echo>darwin kernel version: ${darwin.kernel.version}</echo>

    <if>
      <bool><matches string="${darwin.kernel.version}" pattern="^([0-9]|1[0-8])\."/></bool>
      <property name="mac.app.bitness" value="32"/>
      <else>
	<property name="mac.app.bitness" value="64"/>
      </else>      
    </if>

    <echo>bitness for mac apps: ${mac.app.bitness}</echo>

    <!--(recursively) copy the .app files from the MacShortcutsXbit folder of the correct bitness 
      https://stackoverflow.com/questions/1685442/how-to-copy-a-directory-using-ant -->
    <copy todir="${basedir}" >  
      <fileset dir="${basedir}/MacShortcuts${mac.app.bitness}bit" includes="**"/>  
    </copy> 

    <if><bool><istrue value="${current.os.ismac}"/></bool>
      <filter token="gsdl3srchome" value="${basedir}"/>
      <copy file="${basedir}/gs3-server.app/Contents/document.wflow.in" tofile="${basedir}/gs3-server.app/Contents/document.wflow" filtering="true" overwrite="true"/>
      <copy file="${basedir}/gli.app/Contents/document.wflow.in" tofile="${basedir}/gli.app/Contents/document.wflow" filtering="true" overwrite="true"/>
      <copy file="${basedir}/client-gli.app/Contents/document.wflow.in" tofile="${basedir}/client-gli.app/Contents/document.wflow" filtering="true" overwrite="true"/>
      <copy file="${basedir}/gems.app/Contents/document.wflow.in" tofile="${basedir}/gems.app/Contents/document.wflow" filtering="true" overwrite="true"/>
    </if>
  </target>

  <!-- ============= tweaks for making compilation static ========== -->  
  <target name="tweak-makefiles" depends="init" if="compile.static">
    <antcall target="rtftohtml-add-static" />
  </target>
    
  <target name="rtftohtml-add-static" depends="init" if="collection.building.enabled">
    <rsr verbosity="1" file="${gs2build.home}/build-src/packages/rtftohtml/rtftohtml_src/Makefile" pattern="-o rtftohtml(.{2})EXEEXT(.{1})" replacement="-o rtftohtml$1EXEEXT$2 -static" />
  </target>
    
  <target name="run-collection-tests">
    <if><bool><not><available file="${basedir}/ext/testing" type="dir"/></not></bool>
    <fail>The testing extension is not available. This is required to perform the tests. It can be acquired from SVN by running the command "svn co https://svn.greenstone.org/main/trunk/gs3-extensions/testing/source testing" in the ext directory of your Greenstone 3 installation. </fail>
    </if>
    <for param="testjar">
      <path>
	<fileset dir="${basedir}" includes="web/sites/*/collect/*/tests/tests.jar"/>
      </path>
      <sequential>
	<echo>Testing @{testjar}</echo>
	<java classname="org.junit.runner.JUnitCore" fork="true">
	  <!--https://stackoverflow.com/questions/38676719/selenium-using-java-the-path-to-the-driver-executable-must-be-set-by-the-webdr-->
	  <sysproperty key="webdriver.gecko.driver" path="${basedir}/ext/testing/geckodriver"/>
	  <arg value="gstests.TestClass"/>
	  <jvmarg value="-DSERVERURL=${default.server.protocol}://${tomcat.server}:${default.tomcat.port}${app.path}${server.default.servlet} "/>
	  <classpath>
	    <fileset dir="${basedir}/ext/testing/lib/java">
	      <include name="*.jar"/>
	    </fileset>
	    <files includes="@{testjar}"/>
	  </classpath>
	</java>
      </sequential>
    </for>
  </target>
  
  <target name="build-collection-tests">
    <if><bool><not><available file="${basedir}/ext/testing" type="dir"/></not></bool>
    <fail>The testing extension is not available. This is required to perform the tests. It can be acquired from SVN by running the command "svn co https://svn.greenstone.org/main/trunk/gs3-extensions/testing/source testing" in the ext directory of your Greenstone 3 installation. </fail>
    </if>
    <for param="compiledir">
      <path>
	<dirset dir="${basedir}" includes="web/sites/*/collect/*/tests/src"/>
      </path>
      <sequential>
	<echo>Compiling @{compiledir}</echo>
	<if><bool><not><available file="@{compiledir}/../build" type="dir"/></not></bool>
	<mkdir dir="@{compiledir}/../build"/>
	</if>
	<javac srcdir="@{compiledir}" 
	       destdir="@{compiledir}/../build" 
	       includeantruntime="${compile.includeantruntime}"
	       debug="${compile.debug}" 
	       deprecation="${compile.deprecation}" 
	       optimize="${compile.optimize}"
				       encoding="${compile.encoding}">
	  <compilerarg line="${compile.javac.flags}"/>
	  <classpath>
	    <fileset dir="${basedir}/ext/testing/lib/java">
	      <include name="*.jar"/>
	    </fileset>
	  </classpath>
	  <include name="gstests/*.java"/>
	</javac>
	<jar destfile="@{compiledir}/../tests.jar">
	  <fileset dir="@{compiledir}/../build">
	    <include name="gstests/**"/>
	  </fileset>
	  <manifest>
	    <attribute name="Built-By" value="${user.name}" />
	  </manifest>
	</jar>
      </sequential>
    </for>
  </target>

  <!-- ============ Target Group: Targets concerned with https certification ================ -->

  <target name="setup-https-cert-info">
    <echo>
      *********************************************************************
               NOTE TO OBTAINING A TLS (SSL) CERTIFICATE FOR HTTPS
      *********************************************************************
      A signed certificate is needed for your GS server to serve pages over https.
      This target will attempt to obtain a certificate for you from the official and free Certificate Authority Lets Encrypt.
      However, a certificate can only be obtained if you have admin/sudo permissions on this machine that you are installing Greenstone on.

      Note that:
      * if you already have a certificate, then you probably do not want to be running this target but the 'ant renew-existing-https-cert' target instead, to renew your existing certificate.
      * if you run this target when you already have a generated certificate, the existing certificate will remain unchanged and the script will terminate with a message alerting you to this fact.
    </echo>
  </target>
  

  <target name="https-conditions-set">
    <input addproperty="https.conditions.ok" validargs="y,n">     
      To run this target, ensure you have:
      * (if on unix) sudo permissions. Enter the sudo password if prompted.
      * (if on windows) sufficient privileges to run the included tomcat on port 80.
      * port 80 open, and nothing running on it when you run this target
      * edited the build.properties file with
        - tomcat.server set to the/a domain name of your server
        - server.protocols comma-separated list set to contain at least 'https' if not also http (the first in this list will be the default protocol)
        - tomcat.port.https set to a valid port number not yet in use
        - keystore.pass set to a password for the certification process
	* read the Lets Encrypt Subscriber Agreement at https://letsencrypt.org/repository/
      If any of the above is not possible, quit this target. Continue [y/n]?
    </input>

    <condition property="quit.https.setup"> 
      <equals arg1="n" arg2="${https.conditions.ok}"/>
    </condition>

    <fail if="quit.https.setup">https certification step aborted by user. Please edit build.properties to set server.protocol=http and comment out tomcat.port.https.</fail>
  </target>
  

  <target name="setup-https-cert" depends="setup-https-cert-info,https-conditions-set"
          description="Use this target to obtain SSL certificates from LetsEncrypt and install them into Tomcat">
    <if><bool><not><matches string="${server.protocols}" pattern="https"/></not></bool>
      <fail>To setup https certification, the server.protocols property in file build.properties must contain 'https'</fail>
    </if>

    <input addproperty="https.cert.email">Enter an email that Let's Encrypt, the certification authority, can send any important notifications to</input>
    <input addproperty="https.other.domains">Besides tomcat.server=${tomcat.server}, enter a comma separated list of additional domains to support, if any</input>
    <input addproperty="https.cert.agree" validargs="y,n">You've read the Let's Encrypt Subscriber Agreement at https://letsencrypt.org/repository/ and agree</input>
    <if>
      <bool><equals arg1="y" arg2="${https.cert.agree}"/></bool>

      <condition property="https.cert.domains" value="${tomcat.server},${https.other.domains}" else="${tomcat.server}">
	<and>
	  <isset property="https.other.domains" />
	  <not><matches string="${https.other.domains}" pattern="^\s*$"/></not>
	</and>
      </condition>      

      <input addproperty="https.do.cert" validargs="y,n">
	You've agreed to the Let's Encrypt TOS with
	- email: ${https.cert.email}
	- domains: ${https.cert.domains}
	Looks okay? [y/n]
      </input>
    </if>

    <if><bool><equals arg1="n" arg2="${https.do.cert}"/></bool>
      <echo>Not proceeding with https certification for the Greenstone 3 web server</echo>
    <else>
        <echo>Proceeding...</echo>	  
		<if><bool><istrue value="${current.os.iswindows}"/></bool>
			<antcall target="setup-https-cert-windows">
				<param name="https.cert.renewal" value=""/><!-- for cert issuance, there are none of the additional parameters specific to cert renewal -->
			</antcall>
		</if>
		<if><bool><istrue value="${current.os.isunixnotmac}"/></bool>
                <antcall target="setup-https-cert-linux"/>
                <antcall target="install-https-cert-linux"/>
		</if>
		<if><bool><istrue value="${current.os.ismac}"/></bool>
			<antcall target="setup-https-cert-mac"/>
		</if>
	 </else>
  </if>
  
  </target>
  
  
  <target name="setup-https-cert-windows">
	
	<echo>********** The included tomcat will be stopped, then restarted on port 80 and stopped again</echo>
	
	<!-- create folder packages\tomcat\webapps\ROOT\.well-known\acme-challenge -->
	<mkdir dir="${packages.home}/tomcat/webapps/ROOT/.well-known/acme-challenge"/>
	<mkdir dir="${packages.home}/tomcat/conf/https_cert"/>
  
	<!--
		For Windows, Greenstone can generate the account and domain keys with the openSSL we compiled up ourselves
		and put on SVN (at GS3/bin/windows/openssl) rather than let ZeroSSL generate these keys for the user.
		Letting Greenstone generate the keys may be considered more trustworthy by the user than letting a 3rd
		party do so. See https://zerossl.com/usage.html#First_time_run_and_regular_use for OpenSSL commands
		If we don't generate the keys ourselves with our OpenSSL, then ZeroSSL will do so automatically in the
		call to le64/32.exe further below, as it's passed in the flag generate-missing.
	-->
	<!-- We generate the account key named "privkey.key" in ${packages.home}\tomcat\conf\https_cert -->
    <exec executable="cmd" osfamily="windows" dir="${basedir}/bin/${os.bin.dir}/openssl/bin" spawn="false">
      <arg value="/c" />
	  <arg value="openssl.exe" />
	  <arg value="genrsa" />
	  <arg value="-out" /><arg value="${packages.home}\tomcat\conf\https_cert\privkey.key" /><arg value="4096" />
	</exec>
  
	<!-- Also generate the domain key (for csr-key parameter to zeroSSL's le.exe)
		${packages.home}\tomcat\conf\https_cert\${tomcat.server}.key
		Using 2048 instead of 4096 bits for this. See https://zerossl.com/usage.html#First_time_run_and_regular_use
	-->
    <exec executable="cmd" osfamily="windows" dir="${basedir}/bin/${os.bin.dir}/openssl/bin" spawn="false">
      <arg value="/c" />
	  <arg value="openssl.exe" />
	  <arg value="genrsa" />
	  <arg value="-out" /><arg value="${packages.home}\tomcat\conf\https_cert\${tomcat.server}.key" /><arg value="2048" />
	</exec>
  
	<!-- stop the included tomcat (also stopping derby and solr) -->
	<antcall target="stop" />
	
	<!-- rerun tomcat on port 80
		See https://ant.apache.org/manual/Tasks/antcall.html -->
	<antcall target="start">
		<param name="localhost.port.http" value="80"/>
		<param name="default.tomcat.port" value="80"/>
		<param name="local.http.url" value="http://${localhost.server.http}"/><!-- For port 80 over http, leave out port number in URL -->
		<param name="http.address.restriction" value=""/><!-- don't prevent public access over http of port 80 -->
		<param name="https.comment.out.start" value="${comment.start}"/>
		<param name="https.comment.out.end" value="${comment.end}"/>
	</antcall>
 
	<!-- get the certificate: use zerossl for windows
		Download from https://github.com/do-know/Crypt-LE/releases,
		For licence see https://github.com/do-know/Crypt-LE/
		Usage instructions at https://zerossl.com/usage.html		
		
		le64 ==key "${packages.home}\tomcat\conf\https_cert\privkey.key" ==csr "${packages.home}\tomcat\conf\https_cert\${tomcat.server}.csr" ==csr-key "${packages.home}s\tomcat\conf\https_cert\${tomcat.server}.key" ==crt "${packages.home}\tomcat\conf\https_cert\${tomcat.server}.crt" ==domains "${https.cert.domains}" ==path "${packages.home}\tomcat\webapps\ROOT\.well-known\acme-challenge" ==generate-missing ==unlink ==live -export-pfx "${keystore.pass}"
		
		which generates a .pfx file with the same name as the PEM certificate (.crt param below)
		.pfx vs .p12: https://stackoverflow.com/questions/6819079/convert-pfx-format-to-p12
		
		In this case "fullchain_and_prvtkey.pfx" is generated, which is the windows value of ${keystore.file} property

		Helpful for debugging: https://stackoverflow.com/questions/10302489/ant-script-have-exec-tag-dump-out-entire-command-line
		-->
    <exec executable="cmd" osfamily="windows" dir="${basedir}/bin/${os.bin.dir}" spawn="false">
      <arg value="/c" />
	  <arg value="le${os.bitness}" />
	  <arg value="--key" /><arg value="${packages.home}\tomcat\conf\https_cert\privkey.key" />
	  <arg value="--csr" /><arg value="${packages.home}\tomcat\conf\https_cert\${tomcat.server}.csr" />
	  <arg value="--csr-key" /><arg value="${packages.home}\tomcat\conf\https_cert\${tomcat.server}.key" />
	  <!--<arg value="==crt" /><arg value="${packages.home}\tomcat\conf\https_cert\${tomcat.server}.crt" />-->
	  <arg value="--crt" /><arg value="${packages.home}\tomcat\conf\https_cert\fullchain_and_prvtkey.crt" />
	  <arg value="--domains" /><arg value="${https.cert.domains}" />
	  <arg value="--path" /><arg value="${packages.home}\tomcat\webapps\ROOT\.well-known\acme-challenge" />
	  <arg value="--generate-missing" />
	  <arg value="--unlink" />
	  <arg line="${https.testing}" /><!-- minus-minus-live if not testing, empty if testing. https://stackoverflow.com/questions/11840284/pass-arguments-to-apache-ant-exec-task-based-on-the-variables-value -->
	  <!--<arg value="==export-pfx" /><arg value="${keystore.pass}" />
	  <arg value="==tag-pfx" /><arg value="greenstone3-tomcat" />--><!--Convert the certificate (that contains the full chain AND private key) to pfx format hereafter using OpenSSL instead-->
	  <arg line="${https.cert.renewal}" /><!-- renew command on windows/mac appends min-min-renew XX, where if the day the renewal is run is XX days within expiry, the certificate will get renewed. -->
    </exec>

	<!-- stop the tomcat running on port 80 -->
	<antcall target="stop">
		<param name="localhost.port.http" value="80"/>
		<param name="default.tomcat.port" value="80"/>
		<param name="local.http.url" value="http://${localhost.server.http}"/><!-- For port 80 over http, leave out port number in URL -->
		<param name="http.address.restriction" value=""/>
		<param name="https.comment.out.start" value="${comment.start}"/>
		<param name="https.comment.out.end" value="${comment.end}"/>
	</antcall>

	<!-- Use OpenSSL instead of ZeroSSL to convert the certificate to the .pfx format that tomcat likes, using this cmd:
			GS3/bin/windows/openssl/bin/openssl.exe pkcs12 -inkey domain.key -in domain.crt -passin pass:pwd -passout pass:pwd -export -out ${keystore.file}
			GS3/bin/windows/openssl/bin/openssl.exe pkcs12 -inkey domain.key -in domain.crt -password pass:pwd -export -out ${keystore.file}
		where on windows, keystore.file = fullchain_and_prvtkey.pfx
	-->
	<exec executable="cmd" osfamily="windows" dir="${basedir}/bin/${os.bin.dir}/openssl/bin" spawn="false">
      <arg value="/c" />
	  <arg value="openssl.exe" />
	  <arg value="pkcs12" />
	  <arg value="-inkey" /><arg value="${packages.home}\tomcat\conf\https_cert\${tomcat.server}.key" />
	  <arg value="-in" /><arg value="${packages.home}\tomcat\conf\https_cert\fullchain_and_prvtkey.crt" />
	  <arg value="-export" />
	  <arg value="-out" /><arg value="${packages.home}\tomcat\conf\https_cert\${keystore.file}" />
	  <arg value="-name"/><arg value="greenstone3-tomcat"/><!-- See https://stackoverflow.com/questions/808669/convert-a-cert-pem-certificate-to-a-pfx-certificate -->
	  <arg value="-password"/><arg value="pass:${keystore.pass}"/>
	</exec>
	
  </target>


  <!-- On Mac as on Linux, we need sudo permissions to setup https certification.
	But unlike on Linux, on Mac we only need to sudo when running tomcat on port 80
	and when stopping tomcat running on port 80. But we don't need to sudo on a Mac when calling le.pl.
	This also means all the files in https_cert have the correct (user, not root) permissions.
  -->
  <target name="setup-https-cert-mac">
    
	<echo>********** The included tomcat will be stopped, then restarted on port 80 and stopped again</echo>
	
	<!-- create folder packages\tomcat\webapps\ROOT\.well-known\acme-challenge -->
	<mkdir dir="${packages.home}/tomcat/webapps/ROOT/.well-known/acme-challenge"/>
	<mkdir dir="${packages.home}/tomcat/conf/https_cert"/>
  
	<!--
	    See comments under setup-https-cert-WINDOWS
	-->
	<!-- We generate the account key named "privkey.key" in ${packages.home}\tomcat\conf\https_cert -->
	<if><bool><not><available file="${packages.home}/tomcat/conf/https_cert/privkey.key"/></not></bool>
	  <exec executable="openssl" osfamily="mac" dir="${basedir}/bin/${os.bin.dir}/openssl/bin" spawn="false">    
	    <arg value="genrsa" />
	    <arg value="-out" /><arg value="${packages.home}/tomcat/conf/https_cert/privkey.key" /><arg value="4096" />
	  </exec>
	</if>
  
	<!-- Also generate the domain key (for csr-key parameter to zeroSSL's le.pl)
		${packages.home}\tomcat\conf\https_cert\${tomcat.server}.key
		Using 2048 instead of 4096 bits for this. See https://zerossl.com/usage.html#First_time_run_and_regular_use
	-->
	<if><bool><not><available file="${packages.home}/tomcat/conf/https_cert/${tomcat.server}.key"/></not></bool>
	  <exec executable="openssl" osfamily="mac" dir="${basedir}/bin/${os.bin.dir}/openssl/bin" spawn="false">
	    <arg value="genrsa" />
	    <arg value="-out" /><arg value="${packages.home}/tomcat/conf/https_cert/${tomcat.server}.key" /><arg value="2048" />
	  </exec>
	</if>
  
	<!-- stop the included tomcat (also stopping derby and solr) -->
	<antcall target="stop" />
	
	<!-- rerun tomcat on port 80
		See https://ant.apache.org/manual/Tasks/antcall.html -->
	<antcall target="start">
	  <param name="sudo.or.not" value="/usr/bin/sudo" />
		<param name="localhost.port.http" value="80"/>
		<param name="default.tomcat.port" value="80"/>
		<param name="local.http.url" value="http://${localhost.server.http}"/><!-- For port 80 over http, leave out port number in URL -->
		<param name="http.address.restriction" value=""/><!-- don't prevent public access over http of port 80 -->
		<param name="https.comment.out.start" value="${comment.start}"/>
		<param name="https.comment.out.end" value="${comment.end}"/>
	</antcall>
 
	<!-- get the certificate: use zerossl's le.pl compiled up for Mac.
	   For further notes, see under setup-https-cert-WINDOWS
	-->

    <exec executable="perl" osfamily="mac" dir="${gs2build.home}/perllib/cpan/Crypt/LE/bin" spawn="false">
      <env key="PERL5LIB" value="${gs2build.home}/perllib/cpan${path.separator}${gs2build.home}/perllib/cpan/perl-5.18"/>
      <arg value="-S" />
	  <arg value="${gs2build.home}/perllib/cpan/Crypt/LE/bin/le.pl" />
	  <arg value="--key" /><arg value="${packages.home}/tomcat/conf/https_cert/privkey.key" />
	  <arg value="--csr" /><arg value="${packages.home}/tomcat/conf/https_cert/${tomcat.server}.csr" />
	  <arg value="--csr-key" /><arg value="${packages.home}/tomcat/conf/https_cert/${tomcat.server}.key" />
	  <!--<arg value="==crt" /><arg value="${packages.home}/tomcat/conf/https_cert/${tomcat.server}.crt" />-->
	  <arg value="--crt" /><arg value="${packages.home}/tomcat/conf/https_cert/fullchain_and_prvtkey.crt" />
	  <arg value="--domains" /><arg value="${https.cert.domains}" />
	  <arg value="--path" /><arg value="${packages.home}/tomcat/webapps/ROOT/.well-known/acme-challenge" />
	  <arg value="--generate-missing" />
	  <arg value="--unlink" />
	  <arg line="${https.testing}" /><!-- minus-minus-live if not testing, empty if testing. https://stackoverflow.com/questions/11840284/pass-arguments-to-apache-ant-exec-task-based-on-the-variables-value -->
	  <!--<arg value="==export-pfx" /><arg value="${keystore.pass}" />
	      <arg value="==tag-pfx" /><arg value="greenstone3-tomcat" />--><!-- not sure if this generates a keystore filename with ext .p12 or .pfx for Macs -->
	  <arg line="${https.cert.renewal}" /><!-- renew command on windows/mac appends min-min-renew XX, where if the day the renewal is run is XX days within expiry, the certificate will get renewed. -->
    </exec>

    <!-- stop the tomcat running on port 80 -->
    <antcall target="stop">
      <param name="sudo.or.not" value="/usr/bin/sudo" />
      <param name="localhost.port.http" value="80"/>
      <param name="default.tomcat.port" value="80"/>
      <param name="local.http.url" value="http://${localhost.server.http}"/>
      <param name="http.address.restriction" value=""/>
      <param name="https.comment.out.start" value="${comment.start}"/>
      <param name="https.comment.out.end" value="${comment.end}"/>
    </antcall>

	<!-- Use OpenSSL instead of ZeroSSL to convert the certificate to the .pfx format that tomcat likes, using this cmd:
			GS3/bin/windows/openssl/bin/openssl.exe pkcs12 -inkey domain.key -in domain.crt -passin pass:pwd -passout pass:pwd -export -out ${keystore.file}
			GS3/bin/windows/openssl/bin/openssl.exe pkcs12 -inkey domain.key -in domain.crt -password pass:pwd -export -out ${keystore.file}
		where on windows, keystore.file = fullchain_and_prvtkey.pfx
	-->
	<exec executable="openssl" osfamily="mac" dir="${basedir}/bin/${os.bin.dir}/openssl/bin" spawn="false">
	  <arg value="pkcs12" />
	  <arg value="-inkey" /><arg value="${packages.home}/tomcat/conf/https_cert/${tomcat.server}.key" />
	  <arg value="-in" /><arg value="${packages.home}/tomcat/conf/https_cert/fullchain_and_prvtkey.crt" />
	  <arg value="-export" />
	  <arg value="-out" /><arg value="${packages.home}/tomcat/conf/https_cert/${keystore.file}" />
	  <arg value="-name"/><arg value="greenstone3-tomcat"/>
          <!-- See https://stackoverflow.com/questions/808669/convert-a-cert-pem-certificate-to-a-pfx-certificate -->
	  <arg value="-password"/><arg value="pass:${keystore.pass}"/> 
  </exec>
	
  </target>

  <!-- certbot documentation: https://eff-certbot.readthedocs.io/en/latest/-->
  <target name="setup-https-cert-linux">
      <!-- Running as
	   certbot certonly ==standalone ==preferred-challenges http ==email EMAIL -d DOMAINS 
	   expects input from stdin to accept (A) ToS and say Yes (Y) to sharing email.
	   We can run in non-interactive mode as the user has at this stage already agreed
	   to LetsEncrypt's Terms of Service and provided an email address.
      -->

      <exec executable="sudo" dir="/tmp" failonerror="true">
	<arg value="certbot"/>
	<arg value="certonly"/>
	<arg line="${https.testing}"/><!-- https://stackoverflow.com/questions/11840284/pass-arguments-to-apache-ant-exec-task-based-on-the-variables-value -->
	<arg value="--standalone"/>
	<arg value="--non-interactive"/>
	<arg value="--agree-tos"/>
	<arg value="--preferred-challenges"/><arg value="http"/>
	<arg value="--email"/><arg value="${https.cert.email}"/>
	<arg value="--domains"/><arg value="${https.cert.domains}"/>
      </exec>
  </target>


  <!-- sudo openssl pkcs12 -export -out /tmp/DOMAIN_fullchain_and_key.p12 \
	    -in /etc/letsencrypt/live/DOMAIN/fullchain.pem \
	    -inkey /etc/letsencrypt/live/DOMAIN/privkey.pem \
	    -name tomcat
	    Must run as sudo because only admin has access to the pem files that admin
	    generated in /etc/letsencrypt
	    See https://computingforgeeks.com/tomcat-7-with-letsencrypt-ssl-certificate/
	    but also https://community.letsencrypt.org/t/using-lets-encrypt-with-tomcat/41082
	    which bypasses the step to generate the java keystore jks file
	    and uses openssl to generate a pfx file instead of a p12 file
  -->


  <target name="install-https-cert-linux" description="If the SSL certificates have been renewed, use this target to convert and install them into Tomcat">
    <exec executable="sudo" dir="/tmp" failonerror="true">
      <arg value="${basedir}/bin/${os.bin.dir}/openssl/bin/openssl"/>
      <arg value="pkcs12"/>
      <arg value="-export"/>
      <arg value="-out"/><arg value="/tmp/${tomcat.server}_fullchain_and_key.p12"/>
      <arg value="-in"/><arg value="/etc/letsencrypt/live/${tomcat.server}/fullchain.pem"/>
      <arg value="-inkey"/><arg value="/etc/letsencrypt/live/${tomcat.server}/privkey.pem"/>
      <arg value="-name"/><arg value="greenstone3-tomcat"/>
      <arg value="-password"/><arg value="pass:${keystore.pass}"/>
    </exec>

    <!-- Finally, mkdir ${packages.home}/tomcat/conf/https_cert
	   and copy the file /tmp/${tomcat.server}_fullchain_and_key.p12 into it
	   and rename to a slightly shorter and simpler name,
	   see https://stackoverflow.com/questions/8971187/ant-renaming-while-copying-file
	   The file in tmp has root permissions. But copying it from tmp into
	   the local account will give the copy local account permissions.
	   Then sudo to remove the original copy in /tmp
    -->
    <mkdir dir="${packages.home}/tomcat/conf/https_cert"/>
    <!--<copy file="/tmp/${tomcat.server}_fullchain_and_key.p12" todir="${packages.home}/tomcat/conf/https_cert"/>-->
    <copy todir="${packages.home}/tomcat/conf/https_cert">
      <fileset file="/tmp/${tomcat.server}_fullchain_and_key.p12"/>
      <globmapper from="${tomcat.server}_fullchain_and_key.p12" to="${keystore.file}"/>
    </copy>
    
    <exec executable="sudo" dir="/tmp" failonerror="true">
      <arg line="rm -f /tmp/${tomcat.server}_fullchain_and_key.p12" />
    </exec>
    
  </target>
  
  <target name="install-https-cert-linux-nopwd" description="If the SSL certificates have been renewed, use this target to convert and install them into Tomcat. Uses a script so can be setup to need no password">
    <echo>This target requires convert-https-cert.sh to have been properly setup.</echo>
      <exec executable="sudo" dir="/tmp" failonerror="true">
        <arg value="${basedir}/convert-https-cert.sh"/>
      </exec>

      <mkdir dir="${packages.home}/tomcat/conf/https_cert"/>
      <copy todir="${packages.home}/tomcat/conf/https_cert">
      <fileset file="/tmp/${tomcat.server}_fullchain_and_key.p12"/>
      <globmapper from="${tomcat.server}_fullchain_and_key.p12" to="${keystore.file}"/>
    </copy>
    
    <exec executable="sudo" dir="/tmp" failonerror="true">
      <arg line="/usr/bin/rm /tmp/${tomcat.server}_fullchain_and_key.p12" />
    </exec>
    
  </target>

  <!-- Revoke the certificate and remove it, including folders.
       See https://certbot.eff.org/docs/using.html#revoking-certificates
       which also states "if a certificate is a test certificate obtained via the
       ==staging or ==test-cert flag, that flag must be passed to the revoke subcommand."
  -->
  <target name="remove-https-cert" description="Revoke any SSL certificate and remove it">
    <echo>
      NOTE: If you're on Linux, you need to have sudo permissions to execute this target.
      Enter the sudo password if prompted.
    </echo>
    <!--
	On linux, we use certbot.
	We need to send Y(es) as inputstring to confirm that the
	/etc/letsencrypt/live/${tomcat.server} folder can be deleted.
    -->
    <exec executable="sudo" os="${os.linux},${os.solaris}" dir="/tmp" failonerror="true" inputstring="Y">
      <arg value="certbot"/>
     <arg value="revoke"/>
     <arg line="${https.testing}"/>
     <arg value="--cert-path"/><arg value="/etc/letsencrypt/live/${tomcat.server}/cert.pem"/>
    </exec>
    <!-- The above command already deletes the folder when Y(es) was passed in. Explicitly deleting:
	<exec executable="sudo"" dir="/tmp" failonerror="true">
        <arg value="certbot"/>
	<arg value="delete"/>
	<arg value="==cert-name"/><arg value="${tomcat.server}"/>
	</exec>
    -->
	
    <!-- On Windows and Mac, we use zeroSSl. For the revoke command, see https://zerossl.com/usage.html#Certificate_revocation -->
    <exec executable="cmd" osfamily="windows" dir="${basedir}/bin/${os.bin.dir}" spawn="false">
      <arg value="/c" />
      <arg value="le${os.bitness}" />	  
      <arg value="--key" /><arg value="${packages.home}\tomcat\conf\https_cert\privkey.key" />
      <arg value="--crt" /><arg value="${packages.home}\tomcat\conf\https_cert\fullchain_and_prvtkey.crt"/>
      <arg value="--revoke"/>
      <arg line="${https.testing}"/>
    </exec>
	
    <!-- On Mac, we use the le.pl we compiled up (Crypt::LE) and which needs to have PERL5LIB set correctly to run -->	
    <exec executable="perl" osfamily="mac" dir="${gs2build.home}/perllib/cpan/Crypt/LE/bin" spawn="false">
      <env key="PERL5LIB" value="${gs2build.home}/perllib/cpan${path.separator}${gs2build.home}/perllib/cpan/perl-5.18"/>
      <arg value="-S" />
      <arg value="${gs2build.home}/perllib/cpan/Crypt/LE/bin/le.pl" />
      <arg value="--key" /><arg value="${packages.home}/tomcat/conf/https_cert/privkey.key" />
      <arg value="--crt" /><arg value="${packages.home}/tomcat/conf/https_cert/fullchain_and_prvtkey.crt"/>
      <arg value="--revoke"/>
      <arg line="${https.testing}"/>
    </exec>

    <!-- And remove the https_cert folder -->
    <delete dir="${packages.home}/tomcat/conf/https_cert"/>
  </target>
  
  
    <!-- Renewing existing https certificate
	   Linux:
           Most certbot installs will renew certificates for you.
           https://eff-certbot.readthedocs.io/en/latest/using.html#renewing-certificates
           or: 'certbot renew' 
	   Windows: reuse Windows issuance target
	   see https://zerossl.com/usage.html#Certificate_renewal 
	-->
  <target name="renew-existing-https-cert" description="Renew exsiting SSL certificates">
    <echo>
      NOTE: To run this target,
          * ensure nothing is running on port 80, and the port is open
          * if you're on Linux or Mac, you need to have sudo permissions. Enter the sudo password if prompted.      

          If you want your cronjob to renew a certificate, you can add pre and post hooks
          https://eff-certbot.readthedocs.io/en/latest/using.html#renewing-certificates
      For more information run:
         certbot --help renew
    </echo>
    <if><bool><istrue value="${current.os.isunixnotmac}"/></bool>
    <exec executable="sudo" os="${os.linux},${os.solaris}" dir="/tmp" failonerror="true">
      <arg value="certbot"/>
      <arg value="renew"/>
    </exec>
    <!-- regenerate the Tomcat version -->
    <antcall target="install-https-cert-linux"/>
    </if>
    <!-- For renewal on Windows or Mac, we use ZeroSSL as intermediary between GS3 and Let's Encrypt.
	 And when using ZeroSSL we need to re-run the original (issuance) command and append "min-min-renew XX" to it,
	 where if it's within XX days of expiry, the certificate will get renewed.
	 See https://zerossl.com/usage.html#Certificate_renewal -->
    <if><bool><or>
	  <istrue value="${current.os.iswindows}"/>
	  <istrue value="${current.os.ismac}"/>
      </or></bool>
      
      <input addproperty="https.other.domains">Enter a comma separated list of additional domains besides tomcat.server=${tomcat.server} that you registered on issuance, if any</input>
      <condition property="https.cert.domains" value="${tomcat.server},${https.other.domains}" else="${tomcat.server}">
	<and>
	  <isset property="https.other.domains" />
	  <not><matches string="${https.other.domains}" pattern="^\s*$"/></not>
	</and>
	  </condition>
      <if><bool><istrue value="${current.os.iswindows}"/></bool>
	<antcall target="setup-https-cert-windows">
	  <param name="https.cert.renewal" value="--renew 10"/>			
	</antcall>
	<else>
	  <antcall target="setup-https-cert-mac">
	    <param name="https.cert.renewal" value="--renew 10"/>			
	  </antcall>
	</else>
      </if>
    </if>
  </target>

  <target name="renew-existing-https-cert-linux-auto" description="Calls a forced renewal, setting up a deploy hook to automatically run the tomcat steps after a successful renewal">
    <echo>
      NOTE: This will set up the renewal to call the script ant-update-https-cert-and-restart.sh after a successful renewal of the certificate.
      It does a forced-renewal.
      To run this target,
          * ensure nothing is running on port 80, and the port is open
          * You need to have set up sudo access with no password for running the script convert-https-cert.sh, and removing the generated file in tmp. See notes in convert-https-cert.sh for details. 

          https://eff-certbot.readthedocs.io/en/latest/using.html#renewing-certificates
      For more information run:
         certbot --help renew
    </echo>
    <exec executable="sudo" os="${os.linux},${os.solaris}" dir="/tmp" failonerror="true">
      <arg value="certbot"/>
      <arg value="renew"/>
      <arg value="--deploy-hook"/>
      <arg value="${basedir}/ant-update-https-cert-and-restart.sh"/>
      <arg value="--force-renewal"/>
    </exec>

  </target>

</project>
