<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright (c) 1997, 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 basedir="." default="help" name="rmdemo">


    <property environment="env"/>


    <property file="build.properties"/>
    <property name="WSIT_HOME" value="${basedir}/../../../../dist/image/metro"/>
    <property name="lib.home" value="${env.METRO_HOME}/lib"/>
    <!--<property name="dist.home" value="${WSIT_HOME}/dist/image"/>-->
    <property name="build.home" value="${basedir}/build"/>
    <property name="build.classes.home" value="${build.home}/classes"/>
    <property name="build.war.home" value="${build.home}/war"/>
    <!--<property name="wsit.dir" value="${WSIT_HOME}/metro"/>-->
    <property name="wsit.dir" value="${WSIT_HOME}"/>

     <!-- Test Execution Defaults -->
    <condition property="usetomcat" value="true">
      <not>
             <isset property="useglassfish"/>
      </not>
    </condition>
    <path id="wsrm.classpath">
        <pathelement location="${java.home}/../lib/tools.jar"/>
        <fileset dir="${lib.home}">
            <include name="*.jar"/>
        </fileset>
         <!--<fileset dir="${dist.home}/metro/lib">
            <include name="*.jar"/>
        </fileset>-->
    </path>

    <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport">
        <classpath refid="wsrm.classpath"/>
    </taskdef>

    <target name="setup">
        <mkdir dir="${build.home}"/>
        <mkdir dir="${build.classes.home}"/>
        <mkdir dir="${build.classes.home}/src"/>
        <mkdir dir="${build.war.home}"/>
    </target>

    <target name="clean">
        <delete dir="${build.home}" includeEmptyDirs="true"/>
    </target>

    <target name="build-server-wsdl" depends="setup">
        <wsimport
                debug="true"
                verbose="true"
                keep="true"
                extension="true"
                destdir="${build.classes.home}"
                fork="true"
                sourcedestdir="${build.classes.home}/src"
                wsdl="${server.wsdl}">
            <binding dir="${server.dir}/etc" includes="${server.binding}"/>
        </wsimport>
        <javac
                fork="true"
                srcdir="${build.classes.home}/src"
                destdir="${build.classes.home}"
                source="1.8"
                includes="**/server/*.java">
            <classpath refid="wsrm.classpath"/>
        </javac>
    </target>

    <target name="create-war" depends="build-tests">
        <war warfile="${build.war.home}/${warName}.war" webxml="${server.dir}/etc/web.xml">
            <webinf dir="${server.dir}/etc" includes="sun-jaxws.xml"/>
            <zipfileset
                    dir="${server.dir}/etc"
                    includes="*.wsdl, *.xsd"
                    prefix="WEB-INF/wsdl"/>
            <classes dir="${build.classes.home}"/>
        </war>
    </target>

    <target name="build-tests"
        depends="setup"
        description="builds the tests">

        <javac
            srcdir="${server.dir}"
            includes="**/*.java"
            destdir="${build.classes.home}"
            source="1.8"
            debug="${debug}"
            deprecation="${deprecation}"
            optimize="${optimize}">
            <classpath refid="wsrm.classpath" />

        </javac>
    </target>

    <target name="generate-client" depends="setup">
        <wsimport
                debug="${debug}"
                verbose="true"
                keep="true"
                extension="true"
                destdir="${build.classes.home}"
                wsdl="${test.wsdl}">
            <binding dir="${client.dir}/etc" includes="${client.binding}"/>
        </wsimport>
    </target>

    <target name="client" depends="generate-client">
        <javac
                fork="true"
                srcdir="${client.dir}"
                destdir="${build.classes.home}"
                source="1.8"
                includes="**/*.java">
            <classpath refid="wsrm.classpath"/>
        </javac>
    </target>

    <target name="run">
        <java fork="true" classname="${client}">
            <classpath>
                <path refid="wsrm.classpath"/>
                <pathelement location="${build.classes.home}"/>
                <pathelement location="${basedir}/etc"/>
            </classpath>
            <!--Uncomment me if you want to see the detail
                WS RM protocol messages flowing on the wire

            <jvmarg value="-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true"/>
            -->
        </java>
    </target>

    <target name="help">
        <echo message="server:       Builds and deploy the service endpoint WAR"/>
        <echo message="client:       Builds the client"/>
        <echo message="run:          Runs the client"/>
    </target>


    <target name="server" depends="setup">

        <antcall target="clean"/>

        <antcall target="build-server-wsdl"/>

        <antcall target="create-war"/>

        <antcall target="deploy"/>
    </target>

    <target name="install" depends="clean">
        <antcall target="install-glassfish"/>
        <antcall target="install-tomcat"/>
     </target>

    <target name="install-glassfish" if="useglassfish">
     <!-- remove old test WAR files -->
     <delete verbose="true">
       <fileset dir="${env.AS_HOME}/domains/domain1/autodeploy"
               includes="rm*.war"/>
     </delete>
     <!-- install WSRM & other needed JAXWS-related files -->
     <!--todo save the installation file-->
     <ant dir="${wsit.dir}"
           antfile="metro-on-glassfish.xml" target="install"
          inheritAll="false"/>
     </target>

     <target name="install-tomcat" if="usetomcat">
     <!-- remove old test WAR files -->
     <delete verbose="true">
       <fileset dir="${env.CATALINA_HOME}/webapps"
               includes="rm*.war"/>
     </delete>
     <!-- install WSRM & other needed JAXWS-related files -->
     <!--todo save the installation file-->
     <ant dir="${wsit.dir}"
           antfile="metro-on-tomcat.xml" target="install"
           inheritAll="false"/>
     </target>

     <target name="deploy" depends="create-war">
        <antcall target="deploy-war-tomcat"/>
        <antcall target="deploy-war-glassfish"/>
     </target>

     <target name="deploy-war-tomcat" depends="create-war" if="usetomcat">
        <property name="catalina.home" value="${env.CATALINA_HOME}"/>
        <echo message="catalina.home = ${catalina.home}"/>

        <copy todir="${catalina.home}/webapps"
              file="${build.war.home}/${warName}.war" verbose="false"
              overwrite="true"/>

     </target>

     <target name="deploy-war-glassfish" depends="create-war" if="useglassfish">
        <property name="as.home" value="${env.AS_HOME}"/>
        <echo message="as.home = ${env.AS_HOME}"/>

        <copy todir="${as.home}/domains/domain1/autodeploy"
              file="${build.war.home}/${warName}.war"
              verbose="false" overwrite="true"/>

     </target>
</project>
