<?xml version="1.0"?>
<!--

    The contents of this file are subject to the license and copyright
    detailed in the LICENSE and NOTICE files at the root of the source
    tree and available online at

    http://www.dspace.org/license/

-->
<!--
  - DSpace installer scripts
  -->

<project name="DSpace" default="help">

    <!-- ============================================================= -->
    <!-- Description                                                   -->
    <!-- ============================================================= -->

    <description>
====================== DSpace platform build file ======================

Common usage:

  Fresh install of DSpace:
    % ant fresh_install

  Update existing installation, leaving data and configuration intact:
    % ant -Dconfig=/installdir/config/dspace.cfg update

========================================================================
    </description>

    <!-- DS-1391: Ant 1.8.0+ is required, fail if not available. -->
    <fail message="Ant 1.8.0+ is required, ${ant.version} is not supported">
        <condition>
            <not>
                <antversion atleast="1.8.0"/>
            </not>
        </condition>
    </fail>

    <!-- ============================================================= -->
    <!-- Will be using various environment variables                   -->
    <!-- ============================================================= -->
    <property environment="env" />


    <!-- ============================================================= -->
    <!-- Build parameters that are likely to need tweaking             -->
    <!-- ============================================================= -->

    <!-- Default overwrite to true. This may be overridden. -->
    <condition property="overwrite" value="true">
        <not>
            <isset property="overwrite"/>
        </not>
    </condition>

    <!-- Default configurations to use.  This may be overridden. -->
    <!-- First, the default localized config file -->
    <property name="local-config" value="config/local.cfg" />
    <!-- Next, the default dspace.cfg -->
    <property name="config" value="config/dspace.cfg" />

    <!-- Give user a chance to override without editing this file
       (and without typing -D each time s/he compiles it) -->
    <property file="${user.home}/.dspace.properties" />

    <!-- Load the configurations -->
    <!-- In Ant, properties are immutable, so the first one "wins". In this case,
         we load the local.cfg FIRST, so that its settings are used by default. -->
    <property file="${local-config}" />
    <property file="${config}" />

    <!-- Timestamp date used when creating backup directories -->
    <tstamp>
        <format property="build.date" pattern="yyyyMMdd-HHmmss" />
    </tstamp>

    <!-- Default location of GeoLite2 City database to download. This may be overridden, if URL path changes. -->
    <property name="geolite" value="http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz" />

    <!-- Default location of lucene-core JAR to download (for update_solr_indexes). This may be overridden, if URL path changes. -->
    <!-- NOTE: this URL should have the version of the JAR replaced with "[version]" -->
    <property name="lucene-core" value="http://search.maven.org/remotecontent?filepath=org/apache/lucene/lucene-core/[version]/lucene-core-[version].jar" />


    <!-- ============================================================= -->
    <!-- The DSpace class path for executing installation targets      -->
    <!-- ============================================================= -->

    <!-- We will include the environment CLASSPATH -->
    <path id="class.path">
        <pathelement path="${env.CLASSPATH}" />
        <fileset dir="lib">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <!-- ============================================================= -->
    <!-- Load various Ant libraries which define extra tasks.          -->
    <!-- ============================================================= -->
    <!--Load/initialize all Ant-Contrib libraries from DSpace 'class.path' above.
         For more info, see:
        http://ant-contrib.sourceforge.net/tasks/index.html -->
    <taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="class.path"/>

    <!-- ============================================================= -->
    <!-- Print some useful help text                                  -->
    <!-- ============================================================= -->
    <target name="help">

        <echo message="" />
        <echo message="DSpace configuration file" />
        <echo message="-----------------------------------" />
        <echo message="" />
        <echo message="Available targets are:" />
        <echo message="" />
        <echo message="update          --> Update ${dspace.dir} config, etc, lib and web applications without " />
        <echo message="                    touching your data" />
    	<echo message="update_configs  --> Update your configs directory with new configuration files"/>
    	<echo message="update_geolite  --> Dowload and install GeoCity database into ${dspace.dir}/config" />
        <echo message="update_code     --> Update compiled code (bin, lib, and etc directories)" />
        <echo message="update_webapps  --> Update web applications" />
        <echo message="update_solr_indexes --> Checks if any Solr indexes need upgrading (to latest Solr), and if so, upgrades them." />
        <echo message="" />
        <echo message="init_configs    --> Write the configuration files to ${dspace.dir}/config" />
        <echo message="install_code    --> Install compiled code into ${dspace.dir}" />
        <echo message="" />
        <echo message="fresh_install   --> Perform a fresh installation of the software. " />
        <echo message="" />
        <echo message="clean_backups   --> Remove .bak directories under install directory" />
        <echo message="test_database   --> Attempt to connect to the DSpace database in order to verify that configuration is correct" />
        <echo message="" />
        <echo message="" />
        <echo message="Available parameters are:" />
        <echo message="" />
        <echo message="-Dconfig=&lt;path/to/dspace.cfg&gt;  -- Where your dspace.cfg configuration" />
        <echo message="                                  is located" />
    	<echo message="-Doverwrite=false              -- Will force update_configs to no longer copy existing"/>
    	<echo message="                                  config to .old files when they conflict with"/>
    	<echo message="                                  the new config.  Instead, new conflicting"/>
    	<echo message="                                  file with be suffixed with .new."/>

        <echo message="-Dwars=true                    -- Build .war files when updating web" />
        <echo message="                                  applications" />
        <echo message="" />

    </target>


    <!-- ============================================================= -->
    <!-- clean out backup directories                  -->
    <!-- ============================================================= -->
    <target name="clean_backups">
        <delete includeemptydirs="true">
            <fileset dir="${dspace.dir}" includes="lib.bak-**/**" />
        </delete>
        <delete includeemptydirs="true">
            <fileset dir="${dspace.dir}" includes="etc.bak-**/**" />
        </delete>
        <delete includeemptydirs="true">
            <fileset dir="${dspace.dir}" includes="webapps.bak-**/**" />
        </delete>
        <delete includeemptydirs="true">
            <fileset dir="${dspace.dir}" includes="bin.bak-**/**" />
        </delete>
    </target>

    <!-- ============================================================= -->
    <!-- Update an installation                                        -->
    <!-- ============================================================= -->

    <target name="update" depends="update_configs,update_code,test_database,update_webapps,update_solr_indexes" description="Update installed code and web applications (without clobbering data/config)">
    </target>

    <!-- ============================================================= -->
    <!-- Update the config dir, only adds files, if a file is altered  -->
    <!-- in the target directory, the new file is suffixed wth *.new   -->
    <!-- and should be hand updated afterward                .          -->
    <!-- ============================================================= -->
    <target name="update_configs" depends="overwrite_configs,overwrite_solr_configs" description="Updates the Configuration Directory">
    	<antcall target="init_geolite" />
    </target>

    <target name="overwrite_configs" description="Overwrites a configuration directory." if="${overwrite}" depends="copy_configs_keep">

        <!--
        Copy files that are absent in target
        -->
        <copy todir="${dspace.dir}/config" preservelastmodified="true">
            <fileset dir="config-temp" includes="**/*">
                <present present="srconly" targetdir="${dspace.dir}/config" />
            </fileset>
        </copy>

        <!--
        Copy files that are different in target to *.old suffix
        -->
        <copy todir="${dspace.dir}/config" preservelastmodified="true" overwrite="true">
                <fileset dir="${dspace.dir}/config">
                   <and>
                    <different targetdir="config-temp" ignoreFileTimes="true" />
                    <present present="both" targetdir="config-temp" />
                   </and>
                    <type type="file" />
                </fileset>
            <globmapper from="*" to="*-${build.date}.old" />
        </copy>

        <!--
        Force overwrite of whats left
        -->
        <copy todir="${dspace.dir}/config" preservelastmodified="true" overwrite="true">
            <fileset dir="config-temp">
                <different targetdir="${dspace.dir}/config" ignoreFileTimes="true" />
                <type type="file" />
            </fileset>
        </copy>

        <!--
        Remove the work directory
        -->
        <delete dir="config-temp" />

        <echo>
====================================================================
 The DSpace configuration has been updated.

 During the upgrade process the build has attempted to overwrite the
 previous configuration while preserving your previous changes in files
 suffixed "*.old"

 To complete installation, you should do the following:

 * Check that you've properly merged any differences in the following files:

        </echo>

        <fileset dir="${dspace.dir}/config" id="config.files">
              <include name="**/*.old"/>
        </fileset>

        <pathconvert pathsep="${line.separator}" property="configfiles" refid="config.files"/>

        <echo>${configfiles}</echo>

        <echo>

  * To properly merge your previous configuration files, it's
    best to try to take the newer version and copy your
    previous changes into it.
====================================================================
        </echo>

    </target>

    <target name="copy_configs_keep" unless="${overwrite}" depends="prepare_configs">

        <!--
        Copy files that are absent in target
        -->
        <copy todir="${dspace.dir}/config" preservelastmodified="true">
            <fileset dir="config-temp" includes="**/*">
                <present present="srconly" targetdir="${dspace.dir}/config" />
            </fileset>
        </copy>

        <!--
        Copy files that are different to target to *.new suffix
        -->
        <copy todir="${dspace.dir}/config" preservelastmodified="true">
            <fileset dir="config-temp">
                <different targetdir="${dspace.dir}/config" ignoreFileTimes="true" />
                <type type="file" />
            </fileset>
            <globmapper from="*" to="*.new" />
        </copy>

        <!--
            Remove the work directory
            -->
        <delete dir="config-temp" />

        <echo>
====================================================================
 The DSpace configuration has been updated.

 During the upgrade process the build has preserved your original
 configuration and placed any conflicting new files next to your
 original suffixed with the "*.new" extension.

 To complete installation, you should do the following:

 * Check that you've properly merged any differences in the following files:

        </echo>

        <fileset dir="${dspace.dir}/config" id="config.files">
              <include name="**/*.new"/>
        </fileset>

        <pathconvert pathsep="${line.separator}" property="configfiles" refid="config.files"/>

        <echo>${configfiles}</echo>

        <echo>

  * To properly merge your previous configuration files, it's
    best to try to take the newer version and copy your
    previous changes into it. Delete the old and remove the
    ".new" suffix from the new file.

====================================================================
        </echo>

    </target>

    <target name="prepare_configs" description="Updates a configuration directory.">

        <mkdir dir="config-temp" />

        <!--
        Copy over all config files (not including dspace.cfg)
        -->
        <copy todir="config-temp" preservelastmodified="true" overwrite="true" failonerror="false">
            <fileset dir="config" excludes="dspace.cfg" />
        </copy>

        <!--
        Copy designated dspace.cfg
        -->
        <copy file="${config}" tofile="config-temp/dspace.cfg" preservelastmodified="true" />

        <!--
        If designated config isn't the one in config/dspace.cfg then lets put a copy of that next to it
        -->
        <copy todir="config-temp" preservelastmodified="true" overwrite="true" >
            <fileset dir="config" includes="dspace.cfg">
                <different targetdir="${dspace.dir}/config" ignoreFileTimes="true" />
            </fileset>
            <globmapper from="*" to="*.default" />
        </copy>

        <!-- Finally, filter some very specific configuration files, which cannot
             be managed by DSpace's configuration service. NOTE: please refrain
             from appending files to this list unless ABSOLUTELY NECESSARY.
             MOST config files are now auto-interpolated at runtime and don't need filtering! -->
        <copy todir="config-temp" preservelastmodified="true" overwrite="true" failonerror="false">
            <fileset dir="config">
                <!-- Filter all log4j files, as log4j requires absolute paths
                     to its log files. In addition, log4j strongly prefers
                     autoconfiguring itself (e.g. see DS-3104). -->
                <include name="log4j*.properties"/>
                <!-- Filter OAI-PMH description so it can include hostname and handle prefix -->
                <include name="crosswalks/oai/description.xml"/>
                <!-- Filter sample Tomcat context.xml -->
                <include name="default.context.xml"/>
                <!-- Filter RDF configs as these are TTL files -->
                <include name="modules/rdf/*.ttl"/>
            </fileset>
            <filterchain>
                <expandproperties />
            </filterchain>
        </copy>

        <echo file='config-temp/ant.properties' message='ant.version = ${ant.version}'/>

    </target>

    <target name="overwrite_solr_configs" description="Overwrites a configuration directory." if="${overwrite}" depends="copy_solr_configs_keep">

        <!--
        Copy files that are absent in target
        -->
        <copy todir="${dspace.dir}/solr" preservelastmodified="true">
            <fileset dir="solr-config-temp" includes="**/*">
                <and>
                   <present present="srconly" targetdir="${dspace.dir}/solr" />
                   <!-- acts like <exclude name="**/data/**"/> -->
                   <filename negate="true" name="**/data/**"/>
                </and>
            </fileset>
        </copy>

        <!--
        Copy files that are different in target to *.old suffix
        -->
        <copy todir="${dspace.dir}/solr" preservelastmodified="true" overwrite="true">
                <fileset dir="${dspace.dir}/solr">
                   <and>
                    <different targetdir="solr-config-temp" ignoreFileTimes="true" />
                    <present present="both" targetdir="solr-config-temp" />
                    <!-- acts like <exclude name="**/data/**"/> -->
                    <filename negate="true" name="**/data/**"/>
                   </and>
                   <type type="file" />
                </fileset>
            <globmapper from="*" to="*-${build.date}.old" />
        </copy>

        <!--
        Force overwrite of whats left
        -->
        <copy todir="${dspace.dir}/solr" preservelastmodified="true" overwrite="true">
            <fileset dir="solr-config-temp">
                <and>
                    <different targetdir="${dspace.dir}/solr" ignoreFileTimes="true" />
                    <!-- acts like <exclude name="**/data/**"/> -->
                    <filename negate="true" name="**/data/**"/>
                </and>
                <type type="file" />
            </fileset>
        </copy>

        <!--
        Remove the work directory
        -->
        <delete dir="solr-config-temp" />

        <echo>
====================================================================
 The DSpace configuration has been updated.

 During the upgrade process the build has attempted to overwrite the
 previous configuration while preserving your previous changes in files
 suffixed "*.old"

 To complete installation, you should do the following:

 * Check that you've properly merged any differences in the following files:

        </echo>

        <fileset dir="${dspace.dir}/solr" id="config.files">
              <include name="**/*.old"/>
        </fileset>

        <pathconvert pathsep="${line.separator}" property="configfiles" refid="config.files"/>

        <echo>${configfiles}</echo>

        <echo>

  * To properly merge your previous configuration files, it's
    best to try to take the newer version and copy your
    previous changes into it.
====================================================================
        </echo>

    </target>

    <target name="copy_solr_configs_keep" unless="${overwrite}" depends="prepare_solr_configs">

        <!--
        Copy files that are absent in target
        -->
        <copy todir="${dspace.dir}/solr" preservelastmodified="true">
            <fileset dir="solr-config-temp" includes="**/*">
                <and>
                    <present present="srconly" targetdir="${dspace.dir}/solr"/>
                    <!-- acts like <exclude name="**/data/**"/> -->
                    <filename negate="true" name="**/data/**"/>
                </and>
            </fileset>
        </copy>

        <!--
        Copy files that are different to target to *.new suffix
        -->
        <copy todir="${dspace.dir}/solr" preservelastmodified="true">
            <fileset dir="solr-config-temp">
                <and>
                    <different targetdir="${dspace.dir}/solr" ignoreFileTimes="true"/>
                    <!-- acts like <exclude name="**/data/**"/> -->
                    <filename negate="true" name="**/data/**"/>
                </and>
                <type type="file" />
            </fileset>
            <globmapper from="*" to="*.new" />
        </copy>

        <!--
            Remove the work directory
            -->
        <delete dir="solr-config-temp" />

        <echo>
====================================================================
 The DSpace configuration has been updated.

 During the upgrade process the build has preserved your original
 configuration and placed any conflicting new files next to your
 original suffixed with the "*.new" extension.

 To complete installation, you should do the following:

 * Check that you've properly merged any differences in the following files:

        </echo>

        <fileset dir="${dspace.dir}/solr" id="config.files">
              <include name="**/*.new"/>
        </fileset>

        <pathconvert pathsep="${line.separator}" property="configfiles" refid="config.files"/>

        <echo>${configfiles}</echo>

        <echo>

  * To properly merge your previous configuration files, it's
    best to try to take the newer version and copy your
    previous changes into it. Delete the old and remove the
    ".new" suffix from the new file.

====================================================================
        </echo>

    </target>

    <target name="prepare_solr_configs" description="Prepares solr configuration directory.">

        <mkdir dir="solr-config-temp" />

        <!--
        Prepare configuration files
        -->
        <copy todir="solr-config-temp" preservelastmodified="true" overwrite="true" failonerror="false">
            <fileset dir="solr">
                <!-- exclude all "data" directories from being overwritten -->
                <exclude name="**/data/**"/>
            </fileset>
        </copy>

    </target>




    <!-- ============================================================= -->
    <!-- Update core code only (no webapps or configs)                 -->
    <!-- ============================================================= -->

    <target name="update_code" description="Update installed code (without clobbering data/config)">

        <move todir="${dspace.dir}/bin.bak-${build.date}" failonerror="no">
            <fileset dir="${dspace.dir}/bin">
                <include name="**/*" />
            </fileset>
        </move>

        <copy todir="${dspace.dir}/bin" preservelastmodified="true">
            <fileset dir="bin" />
        </copy>

        <chmod dir="${dspace.dir}/bin" perm="u+x" includes="**/*" />

        <move todir="${dspace.dir}/lib.bak-${build.date}" failonerror="no">
            <fileset dir="${dspace.dir}/lib">
                <include name="**/*" />
            </fileset>
        </move>

        <copy todir="${dspace.dir}/lib" preservelastmodified="true">
            <fileset dir="lib" />
        </copy>

        <move todir="${dspace.dir}/etc.bak-${build.date}" failonerror="no">
            <fileset dir="${dspace.dir}/etc">
                <include name="**/*" />
            </fileset>
        </move>

        <copy todir="${dspace.dir}/etc" preservelastmodified="true">
            <fileset dir="etc" />
        </copy>

    	<copy todir="${dspace.dir}/solr" preservelastmodified="true">
    	     <fileset dir="solr"/>
    	</copy>

        <echo>
====================================================================
 ${dspace.dir}/bin was backed up to

 ${dspace.dir}/bin.bak-${build.date}

 ${dspace.dir}/lib was backed up to

 ${dspace.dir}/lib.bak-${build.date}

 ${dspace.dir}/etc was backed up to

 ${dspace.dir}/etc.bak-${build.date}

 Please review these directories and delete if no longer needed.
====================================================================
        </echo>

    </target>


    <!-- ============================================================= -->
    <!-- Update Web Applications only                                  -->
    <!-- ============================================================= -->

    <target name="update_webapps" description="Update Web Applications (without clobbering data/config)">

        <move todir="${dspace.dir}/webapps.bak-${build.date}" failonerror="no">
            <fileset dir="${dspace.dir}/webapps">
                <include name="**/*" />
            </fileset>
        </move>

        <echo>
====================================================================
 ${dspace.dir}/webapps was backed up to

 ${dspace.dir}/webapps.bak-${build.date}

 Please review this directory and delete it if it's no longer needed.
====================================================================
        </echo>

        <antcall target="copy_webapps" />

        <echo>
====================================================================
 Updated DSpace Web application directories are in the
 '${dspace.dir}/webapps' directory.

 * Stop your Web servlet container (Tomcat, Jetty, Resin etc.)

 * If your Web servlet container already loads the DSpace
   UI web applications from ${dspace.dir}/webapps/, then you
   can jump straight to restarting your Web servlet container

 * Otherwise, you will need to copy any web applications from
   ${dspace.dir}/webapps/ to the appropriate place for your servlet
   container.  (e.g. '$CATALINA_HOME/webapps' for Tomcat)

   If you're using Tomcat, you should first delete any existing
   DSpace directories in '$CATALINA_HOME/webapps/' (e.g. you would
   delete an existing $CATALINA_HOME/webapps/dspace/ directory).
   Otherwise, Tomcat may continue to use old code in that directory.

 * Start up your Web servlet container again.
====================================================================
        </echo>

    </target>


    <!-- ============================================================= -->
	<!-- Copy Web Applications to Runtime Location -->
    <!-- ============================================================= -->

    <target name="copy_webapps">

        <!-- Copy webapp files to /webapps (excluding any filtered files) -->
        <copy todir="${dspace.dir}/webapps" preservelastmodified="true" failonerror="no">
            <fileset dir="webapps">
                <exclude name="**/web.xml" />
                <exclude name="**/robots.txt" />
            </fileset>
        </copy>

        <!-- Ensure specific webapp files (web.xml, robots.txt) are filtered -->
        <copy todir="${dspace.dir}/webapps" preservelastmodified="false" failonerror="no">
            <fileset dir="webapps">
                <include name="**/web.xml" />
                <include name="**/robots.txt" />
            </fileset>
            <filterchain>
                <expandproperties />
            </filterchain>
        </copy>

        <antcall target="build_webapps_wars"/>

    </target>


    <!-- ============================================================= -->
    <!-- Compress Web Applications                                     -->
    <!-- (Only executes if the "wars" property is defined)             -->
    <!-- ============================================================= -->

    <target name="build_webapps_wars" description="Compress Web Applications into .war files" if="wars">

        <war destfile="${dspace.dir}/webapps/xmlui.war">
            <fileset dir="${dspace.dir}/webapps/xmlui/" />
        </war>

        <war destfile="${dspace.dir}/webapps/oai.war">
            <fileset dir="${dspace.dir}/webapps/oai/" />
        </war>

        <war destfile="${dspace.dir}/webapps/jspui.war">
            <fileset dir="${dspace.dir}/webapps/jspui/" />
        </war>

        <war destfile="${dspace.dir}/webapps/rdf.war">
            <fileset dir="${dspace.dir}/webapps/rdf/"/>
        </war>

        <war destfile="${dspace.dir}/webapps/rest.war">
            <fileset dir="${dspace.dir}/webapps/rest/"/>
        </war>

        <war destfile="${dspace.dir}/webapps/sword.war">
            <fileset dir="${dspace.dir}/webapps/sword/" />
        </war>

    	<war destfile="${dspace.dir}/webapps/solr.war">
    	            <fileset dir="${dspace.dir}/webapps/solr/" />
    	</war>
    </target>


    <!-- ============================================================= -->
    <!-- Install DSpace and Dependencies                               -->
    <!-- ============================================================= -->

    <target name="init_installation">

        <mkdir dir="${dspace.dir}/bin" />

        <mkdir dir="${dspace.dir}/config" />

        <mkdir dir="${dspace.dir}/lib" />

        <mkdir dir="${dspace.dir}/etc" />

        <mkdir dir="${dspace.dir}/webapps" />

        <mkdir dir="${dspace.dir}/triplestore" />

        <mkdir dir="${org.dspace.app.itemexport.work.dir}" />

        <mkdir dir="${org.dspace.app.itemexport.download.dir}" />

        <mkdir dir="${handle.dir}" />

        <mkdir dir="${dspace.dir}/log" />

        <mkdir dir="${upload.temp.dir}" />

        <mkdir dir="${report.dir}" />

    	<mkdir dir="${dspace.dir}/solr" />

    	<!-- Runtime files (Could be erased - just for performing purposes) -->
    	<mkdir dir="${dspace.dir}/var" />
    	<!-- Runtime files for OAI 2.0 -->
    	<mkdir dir="${dspace.dir}/var/oai" />
    </target>


    <!-- ============================================================= -->
    <!-- Fresh install of config files                                 -->
    <!-- ============================================================= -->

    <!-- Copies the configuration files to ${dspace.dir}/config. -->
    <target name="init_configs" depends="init_installation,prepare_configs">

        <copy todir="${dspace.dir}/config" preservelastmodified="true" failonerror="false">
            <fileset dir="config-temp" excludes="dspace.cfg" />
        </copy>

        <!-- Remove the prepare_configs work directory -->
        <delete dir="config-temp" />

        <copy file="${config}" tofile="${dspace.dir}/config/dspace.cfg" preservelastmodified="true" />

        <echo file='${dspace.dir}/config/ant.properties' message='ant.version = ${ant.version}'/>

    </target>

    <!-- ============================================================= -->
    <!-- Check the connection to the database                          -->
    <!-- ============================================================= -->

    <!-- Test the connection to the database -->
    <target name="test_database">
        <java classname="org.dspace.app.launcher.ScriptLauncher" classpathref="class.path" fork="yes" failonerror="yes">
            <sysproperty key="log4j.configuration" value="file:config/log4j-console.properties" />
            <sysproperty key="dspace.log.init.disable" value="true" />
            <sysproperty key="dspace.dir" value="${dspace.dir}" />
            <arg value="database" />
            <arg value="test" />
        </java>
    </target>

    <!-- ============================================================= -->
    <!-- Install fresh code but do not touch the database              -->
    <!-- ============================================================= -->

    <target name="install_code"
            depends="init_installation,init_configs"
            description="Do a fresh install of the code, preserving any data."
            >

        <delete failonerror="no">
            <fileset dir="${dspace.dir}/bin" includes="**/*" />
        </delete>

        <copy todir="${dspace.dir}/bin" preservelastmodified="true">
            <fileset dir="bin" />
        </copy>

        <chmod dir="${dspace.dir}/bin" perm="u+x" includes="**/*" />

        <delete failonerror="no">
            <fileset dir="${dspace.dir}/lib" includes="**/*" />
        </delete>

        <copy todir="${dspace.dir}/lib" preservelastmodified="true">
            <fileset dir="lib" />
        </copy>

        <!-- NB: no regular use is made of etc/ files in an installed
        - system, so this step is 'deprecated', and will eventually
        - be removed.  -->
        <delete failonerror="no">
            <fileset dir="${dspace.dir}/etc" includes="**/*" />
        </delete>

        <copy todir="${dspace.dir}/etc" preservelastmodified="true">
            <fileset dir="etc" />
        </copy>

    	<copy todir="${dspace.dir}/solr" preservelastmodified="true">
    	    <fileset dir="solr" />
    	</copy>

        <echo>
====================================================================
 The DSpace code has been installed.
====================================================================
        </echo>

    </target>


    <!-- ============================================================= -->
    <!-- Do a fresh system install                                     -->
    <!-- ============================================================= -->

    <target name="fresh_install"
            depends="init_installation,init_configs,test_database,install_code"
            description="Do a fresh install of the system, overwriting any data">

        <delete failonerror="no">
            <fileset dir="${dspace.dir}/webapps" includes="**/*" />
        </delete>

        <antcall target="copy_webapps" />

    	<antcall target="init_geolite" />

        <echo>
====================================================================
 The DSpace code has been installed.

 To complete installation, you should do the following:

 * Setup your Web servlet container (e.g. Tomcat) to look for your
   DSpace web applications in: ${dspace.dir}/webapps/

   OR, copy any web applications from ${dspace.dir}/webapps/ to
   the appropriate place for your servlet container.
   (e.g. '$CATALINA_HOME/webapps' for Tomcat)

 * Start up your servlet container (e.g. Tomcat). DSpace now will
   initialize the database on the first startup.

 * Make an initial administrator account (an e-person) in DSpace:

   ${dspace.dir}/bin/dspace create-administrator

 You should then be able to access your DSpace's 'home page':

   ${dspace.url}

====================================================================
        </echo>

    </target>

	<!-- installs GeoCity resolution database -->
    <target name="update_geolite">
        <echo>Downloading: ${geolite}</echo>
        <trycatch property="geolite.error">
            <try>
                <get src="${geolite}"
                     dest="${dspace.dir}/config/GeoLite2-City.tar.gz"
                     verbose="true"/>
                <untar src="${dspace.dir}/config/GeoLite2-City.tar.gz"
                       dest="${dspace.dir}/config/"
                       compression='gzip'>
                    <patternset>
                        <include name='**/GeoLite2-City.mmdb'/>
                    </patternset>
                    <mapper type='flatten'/>
                </untar>
                <delete file="${dspace.dir}/config/GeoLite2-City.tar.gz"/>
            </try>
            <catch>
                <echo>
====================================================================
WARNING : FAILED TO DOWNLOAD GEOLITE DATABASE FILE
          (Used for DSpace Solr Usage Statistics)

Underlying Error: ${geolite.error}

In order to use DSpace Solr Usage Statistics, you will need to
manually re-run:

ant update_geolite

OR

You may manually install this file by following these steps:
(1) Download the latest database archive from ${geolite}
(2) Unpack it.'
(3) Copy the file 'GeoLite2-City.mmdb' to '${usage-statistics.dbfile}'.

====================================================================
                </echo>
            </catch>
        </trycatch>

    </target>

    <target name="check_geolite">
        <condition property="need.geolite">
            <not>
                <available file="${usage-statistics.dbfile}" />
            </not>
        </condition>
    </target>

    <target name="init_geolite" depends="check_geolite" if="need.geolite">
        <antcall target="update_geolite" />
    </target>

    <!-- Check if any Solr indexes need updating to the version of Solr/Lucene we are using. -->
    <target name="update_solr_indexes">
        <echo>Checking if any Solr indexes (${dspace.dir}/solr/*) need upgrading...</echo>

        <!--
            For each index, this is currently a two step process:
             (1) Ensure the index is upgraded to Solr/Lucene 3.5.0
                 (really old indexes need upgrading to this version first)
             (2) Then, we can upgrade from 3.5.0 to latest version of Lucene
        -->

        <!-- Determine what version of Solr/Lucene is being used in DSpace. -->
        <java classname="org.dspace.app.util.IndexVersion" classpathref="class.path" fork="yes" outputproperty="latest_version">
            <sysproperty key="log4j.configuration" value="file:config/log4j-console.properties" />
            <sysproperty key="dspace.log.init.disable" value="true" />
            <arg value="-v" />
        </java>
        <echo>Current version of Solr/Lucene: ${latest_version}</echo>

        <!-- First, let's do the Solr Statistics index -->
        <property name="stats.index" value="${dspace.dir}/solr/statistics/data/index/"/>
        <if>
            <available file="${stats.index}" type="dir"/>
            <then>
                <!-- Ensure Statistics index is >= Solr/Lucene 3.5.0 -->
                <antcall target="check_solr_index">
                    <param name="indexDir" value="${stats.index}"/>
                    <param name="version" value="3.5.0"/>
                    <param name="included" value="false"/>
                </antcall>
                <!-- Ensure Statistics index is upgraded to latest version (included in DSpace) -->
                <antcall target="check_solr_index">
                    <param name="indexDir" value="${stats.index}"/>
                    <param name="version" value="${latest_version}"/>
                    <param name="included" value="true"/>
                </antcall>
            </then>
        </if>

        <!-- Next, let's do the Discovery Solr index.
             NOTE: Discovery will be reindexed post database migration, but this
             is here as a safety measure for older versions of Solr/Lucene -->
        <property name="discovery.index" value="${dspace.dir}/solr/search/data/index/"/>
        <if>
            <available file="${discovery.index}" type="dir"/>
            <then>
                <!-- Ensure Discovery index is >= Solr/Lucene 3.5.0 -->
                <antcall target="check_solr_index">
                    <param name="indexDir" value="${discovery.index}"/>
                    <param name="version" value="3.5.0"/>
                    <param name="included" value="false"/>
                </antcall>
                <!-- Ensure Discovery index is upgraded to latest version (included in DSpace) -->
                <antcall target="check_solr_index">
                    <param name="indexDir" value="${discovery.index}"/>
                    <param name="version" value="${latest_version}"/>
                    <param name="included" value="true"/>
                </antcall>
            </then>
        </if>

        <!-- Next, let's do the OAI-PMH Solr index -->
        <property name="oai.index" value="${dspace.dir}/solr/oai/data/index/"/>
        <if>
            <available file="${oai.index}" type="dir"/>
            <then>
                <!-- Ensure OAI index is >= Solr/Lucene 3.5.0 -->
                <antcall target="check_solr_index">
                    <param name="indexDir" value="${oai.index}"/>
                    <param name="version" value="3.5.0"/>
                    <param name="included" value="false"/>
                </antcall>
                <!-- Ensure OAI index is upgraded to latest version (included in DSpace) -->
                <antcall target="check_solr_index">
                    <param name="indexDir" value="${oai.index}"/>
                    <param name="version" value="${latest_version}"/>
                    <param name="included" value="true"/>
                </antcall>
            </then>
        </if>

        <!-- Finally, cleanup any Lucene JARs that were downloaded into the Ant build directory -->
        <!-- (These JARs are automatically downloaded when an index needs an upgrade). -->
        <echo>Cleanup any downloaded lucene-core-*.jar files. We don't need them anymore.</echo>
        <delete>
            <fileset dir="." includes="lucene-core-*.jar"/>
        </delete>
    </target>

    <!-- Target to check an existing Solr index to see if it            -->
    <!-- meets a particular version requirement.                        -->
    <!-- If the index is outdated, "upgrade_solr_index" is              -->
    <!-- called to upgrade it to the specified version.                 -->
    <!-- REQUIRES these params:                                         -->
    <!--   * indexDir  = Full path to Index directory                   -->
    <!--   * version   = Version of Solr to check against.              -->
    <!--   * included  = Whether this version of Solr/Lucene is already -->
    <!--                 included in DSpace classpath.                  -->
    <target name="check_solr_index">
        <!-- Check if the Solr Statistics index is AT LEAST compatible with Solr/Lucene version 3.5 -->
        <echo>Checking if the Solr index at ${indexDir} is >= Solr ${version}</echo>
        <java classname="org.dspace.app.util.IndexVersion" classpathref="class.path" fork="yes" resultproperty="version_returncode" outputproperty="version_compare">
            <sysproperty key="log4j.configuration" value="file:config/log4j-console.properties" />
            <sysproperty key="dspace.log.init.disable" value="true" />
            <arg value="${indexDir}" />
            <arg value="${version}" />
        </java>

        <!-- Fail if the previous command returns a non-zero result, as this
             means we couldn't determine the Solr index version.
             We need this special error handling, since we are capturing the
             output in ${version_compare}. -->
        <fail>
          <condition>
              <not>
                <equals arg1="${version_returncode}" arg2="0"/>
              </not>
          </condition>
ERROR occurred while checking Solr index version:
${version_compare}
        </fail>

        <!-- If the above java command returned -1, that means this index is NOT yet
             upgraded to the specified Solr version. So, let's upgrade it! -->
        <if>
            <equals arg1="${version_compare}" arg2="-1"/>
            <then>
                <echo message="The Solr index in ${indexDir} needs an upgrade to Solr ${version}"/>
                <!-- Call 'upgrade_solr_index' to actually upgrade the index-->
                <antcall target="upgrade_solr_index">
                    <param name="indexDir" value="${indexDir}"/>
                    <param name="version" value="${version}"/>
                    <param name="included" value="${included}"/>
                </antcall>
            </then>
            <else>
                <echo message="The Solr index in ${indexDir} IS >= Solr ${version}. Looks good!"/>
            </else>
        </if>
    </target>

    <!-- Target to actually *upgrade* an existing Solr index            -->
    <!-- REQUIRES these params:                                         -->
    <!--   * indexDir  = Full path to Index directory                   -->
    <!--   * version   = Version of Solr to upgrade to                  -->
    <!--   * included  = Whether this version of Solr/Lucene is already -->
    <!--                 included in DSpace classpath. If 'false', then -->
    <!--                 the appropriate Lucene JAR will be downloaded. -->
    <target name="upgrade_solr_index">
        <echo>Upgrading Solr/Lucene Index at ${indexDir} to Solr/Lucene ${version}.</echo>

        <!-- Replace the "[version]" placeholders in ${lucene-core} with the actual ${version}-->
        <propertyregex property="lucene-core.jar" input="${lucene-core}" regexp="\[version\]" replace="${version}" global="true"/>

        <!-- Download the appropriate version of the lucene-core.jar, if we
             haven't already AND it's not included on our DSpace classpath. -->
        <if>
            <and>
                <not>
                    <available file="./lucene-core-${version}.jar"/>
                </not>
                <equals arg1="${included}" arg2="false" casesensitive="false"/>
            </and>
            <then>
                <echo>Downloading ${lucene-core.jar}</echo>
                <trycatch property="lucene.download.error">
                    <try>
                        <get src="${lucene-core.jar}" dest="./lucene-core-${version}.jar" verbose="true"/>
                    </try>
                    <catch>
                        <echo>
====================================================================
WARNING : FAILED TO DOWNLOAD LUCENE-CORE.JAR
          (Needed to upgrade your existing Solr indexes)

Underlying Error: ${lucene.download.error}

In order to upgrade your Solr indexes to the latest version,
you will need to do the following:

(1) Manually download the lucene-core-${version}.jar at:

    ${lucene-core.jar}

(2) Place the JAR in the "[src]/dspace/target/dspace-installer" directory.

    [src]/dspace/target/dspace-installer/${lucene-core}.jar

(3) Manually re-run the following 'ant' upgrade command:

    ant update_solr_indexes

For more information, please see the Upgrade Instructions.
====================================================================
                        </echo>
                    </catch>
                </trycatch>
            </then>
        </if>

        <!-- If we downloaded a "lucene-core-*.jar" in the previous step,
             then use that downloaded JAR to upgrade the Solr/Lucene Index -->
        <if>
            <and>
                <available file="./lucene-core-${version}.jar"/>
                <equals arg1="${included}" arg2="false" casesensitive="false"/>
            </and>
            <then>
                <echo>Upgrading the Solr index in ${indexDir}. Depending on the index size, this may take a while (please be patient)...</echo>
                <!-- Run the Lucene IndexUpgrader on this index. This will upgrade the index based on the version of "lucene-core.jar" -->
                <java classname="org.apache.lucene.index.IndexUpgrader" classpath="./lucene-core-${version}.jar" fork="yes" failonerror="yes">
                    <sysproperty key="log4j.configuration" value="file:config/log4j-console.properties" />
                    <sysproperty key="dspace.log.init.disable" value="true" />
                    <arg value="${indexDir}" />
                </java>
            </then>
        </if>

        <!-- Otherwise, if "included=true" then use the Lucene JAR included
             in the DSpace classpath to upgrade the Solr/Lucene Index -->
        <if>
            <equals arg1="${included}" arg2="true" casesensitive="false"/>
            <then>
                <echo>Upgrading the Solr index in ${indexDir}. Depending on the index size, this may take a while (please be patient)...</echo>
                <!-- Run the Lucene IndexUpgrader on this index. This will upgrade the index based on the version of "lucene-core.jar" -->
                <java classname="org.apache.lucene.index.IndexUpgrader" classpathref="class.path" fork="yes" failonerror="yes">
                    <sysproperty key="log4j.configuration" value="file:config/log4j-console.properties" />
                    <sysproperty key="dspace.log.init.disable" value="true" />
                    <arg value="${indexDir}" />
                </java>
            </then>
        </if>
    </target>

</project>
