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

    Copyright (c) 2011, 2020 Oracle and/or its affiliates. All rights reserved.

    This program and the accompanying materials are made available under the
    terms of the Eclipse Distribution License v. 1.0, which is available at
    http://www.eclipse.org/org/documents/edl-v10.php.

    SPDX-License-Identifier: BSD-3-Clause

-->

<project name="Metro on Tomcat" default="help">
    <property environment="env"/>
    <property name="tomcat.home" value="${env.CATALINA_HOME}"/>
    <property name="catalina.home" value="${tomcat.home}"/>
    <property name="catalina.lib.home" value="${catalina.home}/shared/lib"/>

    <condition property="catalina.home.exists">
        <available file="${catalina.home}" type="dir"/>
    </condition>

    <target name="catalinahome-test" unless="catalina.home.exists">
        <fail message="The environment variable CATALINA_HOME was not set or does not point to an existing directory."/>
    </target>

    <target name="init" depends="catalinahome-test"/>

    <target name="install" depends="init,uninstall,update-catalina-props" description="Install latest Metro jars">
        <echo message="Installing Metro 3.0.1 for ${catalina.home} ..."/>

        <mkdir dir="${catalina.lib.home}"/>
        <copy toDir="${catalina.lib.home}" overwrite="true">
            <fileset dir="lib">
                <include name="webservices-api.jar"/>
                <include name="webservices-rt.jar"/>
                <include name="webservices-tools.jar"/>
                <include name="webservices-extra.jar"/>
            </fileset>
        </copy>
        <echo message="... installation complete."/>
    </target>

    <target name="update-catalina-props">
        <echo message="Backing up ${catalina.home}/conf/catalina.properties..."/>
        <copy file="${catalina.home}/conf/catalina.properties" tofile="${catalina.home}/conf/catalina.properties.backup"/>
        <echo message="Adding Eclipse Metro jars to shared.loader property in ${catalina.home}/conf/catalina.properties..."/>
        <replace file="${catalina.home}/conf/catalina.properties" token="shared.loader=" value="shared.loader=$${catalina.home}/shared/lib/*.jar,"/>
    </target>

    <target name="uninstall" depends="init"
            description="Remove the Metro jar files.">
        <delete quiet="true">
            <fileset dir="${catalina.lib.home}" includes="webservices-api.jar, webservices-rt.jar, webservices-tools.jar, webservices-extra.jar"/>
        </delete>
    </target>

    <target name="help">
        <echo message="Usage:"/>
        <echo message="  ant -f metro-on-tomcat.xml {install,uninstall}"/>
        <echo/>
        <echo message="$CATALINA_HOME must be set to the installation directory of Tomcat."/>
        <echo/>
        <echo message="install:"/>
        <echo message="  Installs Metro 3.0.1 on Tomcat"/>
        <echo/>
        <echo message="uninstall: "/>
        <echo message="  Uninstalls Metro 3.0.1 from Tomcat"/>
        <echo/>
    </target>
    
</project>
