<?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="Eclipse Metro on GlassFish" default="help" basedir=".">
    <property environment="env"/>
    <property name="as.home" value="${env.AS_HOME}"/>
    <property name="as.lib.home" value="${as.home}/modules"/>
    <property name="backup.dir" value="${as.home}/.metro-3.0.1"/>
    <property name="osgi.cache.dir" value="${as.home}/domains/domain1/osgi-cache"/>

    <condition property="notInstalled">
        <not>
            <available file="${backup.dir}"/>
        </not>
    </condition>

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

    <condition property="glassfish">
        <available file="${as.home}/modules" type="dir"/>
    </condition>

    <target name="warning-note">
        <echo message="NOTE: This installation script is a work in progress and works only with GlassFish v6 or later."/>
        <echo />
        <echo message="  Eclipse Metro installations via this scripts are not officially supported!"/>
        <echo />
        <echo message="  This script moves default domain osgi cache directory"/>
        <echo message="  ${as.home}/domains/domain1/osgi.cache directory under ${backup.dir}."/>
        <echo message="  If your Glassfish installation consists of different than"/>
        <echo message="  default domain setup, osgi-cache directory for each of"/>
        <echo message="  the domains has to be deleted manually before restarting Glassfish."/>
        <echo />
    </target>

    <target name="gf-message" unless="glassfish">
        <echo message="NOTE: This script can only install Eclipse Metro on GlassFish 6.x."/>
        <echo/>
    </target>

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

    <target name="gf-test" unless="glassfish">
        <antcall target="gf-message"/>
        <fail message="It looks like you are trying to install Metro on older GlassFish version."/>
    </target>

    <target name="init" depends="ashome-test, gf-test"/>

    <target name="backup" if="notInstalled"
            description="Back-up existing Metro jars">
        <echo message="Creating a back-up of default Metro installation..."/>

        <mkdir dir="${backup.dir}"/>
        <copy todir="${backup.dir}" failonerror="false">
            <fileset dir="${as.lib.home}">
                <include name="jakarta.xml.bind-api.jar"/>
                <include name="webservices-api-osgi.jar"/>
                <include name="webservices-osgi.jar"/>
                <include name="webservices-extra-jdk-packages.jar"/>
                <include name="woodstox-core.jar"/>
                <include name="stax2-api.jar"/>
                <include name="jaxb-osgi.jar"/>
<!--
                <include name="xmlsec.jar"/>
                <include name="commons-logging.jar"/>
-->
            </fileset>
        </copy>
        <move todir="${backup.dir}" failonerror="false">
            <fileset dir="${osgi.cache.dir}"/>
        </move>
        <echo message="...done: You can find the original Eclipse Metro files stored in ${backup.dir}"/>
        <echo message="To restore the original Eclipse Metro installation, run this script with 'uninstall' target"/>
    </target>

    <target name="install" depends="init, backup, check-api" description="Install latest Metro jars">
        <antcall target="warning-note" />
        <echo message="Installing Metro 3.0.1 on ${as.home} ..."/>
        <echo />

        <copy todir="${as.lib.home}" overwrite="true">
            <fileset dir="osgi">
                <include name="jakarta.xml.bind-api.jar"/>
                <include name="webservices-api-osgi.jar"/>
                <include name="webservices-osgi.jar"/>
                <include name="webservices-extra-jdk-packages.jar"/>
                <include name="woodstox-core.jar"/>
                <include name="stax2-api.jar"/>
                <include name="jaxb-osgi.jar"/>
<!--
                <include name="xmlsec.jar"/>
                <include name="commons-logging.jar"/>
-->
            </fileset>
        </copy>
        <echo message="... installation complete."/>
    </target>

    <target name="uninstall" unless="notInstalled"
            depends="init"
            description="Restore original Eclipse Metro jars and tool scripts">
        <echo message="Uninstalling Metro 3.0.1 from ${as.home} ..."/>

        <copy todir="${as.lib.home}" overwrite="true">
            <fileset dir="${backup.dir}">
                <include name="jakarta.xml.bind-api.jar"/>
                <include name="webservices-api-osgi.jar"/>
                <include name="webservices-osgi.jar"/>
                <include name="webservices-extra-jdk-packages.jar"/>
                <include name="woodstox-core.jar"/>
                <include name="stax2-api.jar"/>
                <include name="jaxb-osgi.jar"/>
<!--
                <include name="xmlsec.jar"/>
                <include name="commons-logging.jar"/>
-->
            </fileset>
        </copy>

        <delete dir="${backup.dir}" includeemptydirs="true"/>
        <delete dir="${osgi.cache.dir}" includeemptydirs="true"/>

        <echo message="... uninstall complete."/>
        <echo message="The original Eclipse Metro installation has been restored."/>
    </target>

    <target name="help">
        <echo message="install: "/>
        <echo message="  Installs Metro 3.0.1 on GlassFish"/>
        <echo/>
        <echo message="uninstall: "/>
        <echo message="  Uninstalls Metro 3.0.1 from GlassFish and restores the original Eclipse Metro installation"/>
        <echo/>
        <echo message="$AS_HOME must be set to the installation directory of GlassFish. (e.g. '.../glassfish-vX/glassfish')"/>
        <echo/>
        <echo message="Usage:"/>
        <echo message="  ant -f metro-on-glassfish.xml {install | uninstall}"/>
        <antcall target="gf-message"/>
        <antcall target="warning-note"/>
    </target>

</project>
