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

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 2011-2012 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    http://glassfish.java.net/public/CDDL+GPL_1_1.html
    or packager/legal/LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at packager/legal/LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->

<project name="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-2.2.1-b144"/>

    <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-v3">
        <available file="${as.home}/modules" type="dir"/>
    </condition>

    <!-- Check if we are running JAVA SE 6 --> 
    <condition property="jdk6+">
        <or>
            <contains string="${java.version}" substring="1.6.0"/>
            <contains string="${java.version}" substring="1.7.0"/>
        </or>
    </condition>

    <target name="jdk-message" if="jdk6+">
        <echo message="Metro is being installed using Java SE ${java.version}."/>
        <echo/>
        <echo message="  Metro 2.2.1-b144 provides JAX-WS API 2.2 while Java SE ${java.version} includes"/>
        <echo message="  another version. If you want to run Metro applications"/>
        <echo message="  outside GlassFish you will need to use the endorsed"/>
        <echo message="  standards override mechanism. For more details see"/>
        <echo message="  http://download.oracle.com/javase/6/docs/technotes/guides/standards/index.html"/>
        <echo/>
        <echo message="  If you want to enable JAX-WS 2.2 for all applications"/>
        <echo message="  running under Java SE ${java.version}, you may run the command"/>
        <echo message="  'ant install-api'."/>
        <echo message="  That command will copy the file webservices-api.jar to"/>
        <echo message="  ${java.home}/lib/endorsed."/>
        <echo message="  The command needs to be run by a user that has the"/>
        <echo message="  permissions to copy files into ${java.home}/lib/endorsed."/>
        <echo/>
        <echo message="  NOTE that any Java application that incidentally"/>
        <echo message="  uses one of the APIs included in Metro but does"/>
        <echo message="  not have all Metro JARs on its classpath may"/>
        <echo message="  experience class loading issues if you proceed with"/>
        <echo message="  'install-api'."/>
        <echo/>
    </target>

    <target name="warning-note">
        <echo message="NOTE: This installation script is a work in progress and works only with GlassFish v3.1.1 or later."/>
        <echo />
        <echo message="  Metro installations via this scripts are not officially supported!"/>
        <echo />
    </target>

    <target name="gfv3-message" unless="glassfish-v3">
        <echo message="NOTE: This script can only install Metro on GlassFish v3."/>
        <echo message="  Please use the metro-on-glassfish.xml script to install on GlassFish v2"/>
        <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="gfv3-test" unless="glassfish-v3">
        <antcall target="gfv3-message"/>
        <fail message="It looks like you are trying to install Metro on GlassFish v2."/>
    </target>

    <target name="init" depends="ashome-test, gfv3-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="webservices-osgi.jar"/>
                <include name="webservices-extra-jdk-packages.jar"/>
                <include name="woodstox-core-asl.jar"/>
                <include name="stax2-api.jar"/>
                <include name="jaxb-osgi.jar"/>
            </fileset>
            <fileset dir="${as.lib.home}/endorsed">
                <include name="webservices-api-osgi.jar"/>
                <include name="jaxb-api-osgi.jar"/>
            </fileset>
        </copy>
        <echo message="...done: You can find the original Metro files stored in ${backup.dir}"/>
        <echo message="To restore the original 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 2.2.1-b144 on ${as.home} ..."/>
        <echo />

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

    <target name="test-api" description="Test if 'webservices-api.jar' is already installed in $JAVA_HOME/lib/endorsed" if="jdk6+">
        <echo message="${java.home}/lib/endorsed/webservices-api.jar"/>
        <available property="api.installed" file="${java.home}/lib/endorsed/webservices-api.jar"/>
    </target>

    <target name="check-api" depends="test-api" if="api.installed"
            description="Print a warning if 'webservices-api.jar' is found in $JAVA_HOME/lib/endorsed">
        <echo message="Found the file webservices-api.jar in ${java.home}/lib/endorsed."/>
        <echo/>
        <echo message="  This installation does not automatically overwrite"/>
        <echo message="  'webservices-api.jar' in ${java.home}/lib/endorsed."/>
        <echo message="  Please execute the command 'ant install-api' to"/>
        <echo message="  update 'webservices-api.jar'."/>
        <echo/>
        <echo message="  NOTE that any Java application that incidentally uses"/>
        <echo message="  one of the APIs included in Metro but does not"/>
        <echo message="  have all Metro JARs on its classpath may experience"/>
        <echo message="  class loading issues if you install webservices-api.jar"/>
        <echo message="  into ${java.home}/lib/endorsed."/>
    </target>

    <target name="install-api" if="jdk6+" depends="init"
            description="Installs 'webservices-api.jar' into $JAVA_HOME/lib/endorsed">
        <mkdir dir="${java.home}/lib/endorsed"/>
        <copy toDir="${java.home}/lib/endorsed" overwrite="true">
            <fileset dir="lib">
                <include name="webservices-api.jar"/>
            </fileset>
        </copy>
    </target>

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

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

        <copy todir="${as.lib.home}/endorsed" overwrite="true">
            <fileset dir="${backup.dir}">
                <include name="webservices-api-osgi.jar"/>
                <include name="jaxb-api-osgi.jar"/>
            </fileset>
        </copy>

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

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

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

</project>
