<!--

    Copyright (c) 1997, 2018 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

-->

<property file="build.properties"/>
<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"/>

<target name="setup-tc" if="tomcat.home">
    <echo message="In setup-TC in targets.xml"/>
    <mkdir dir="${build.home}"/>
    <mkdir dir="${build.classes.home}"/>
    <mkdir dir="${build.war.home}"/>
    <echo>TC</echo>
    &tomcat;
</target>

<target name="setup-glassfish" if="glassfish.home">
    <echo message="In setup-Glassfish in targets.xml"/>
    <mkdir dir="${build.home}"/>
    <mkdir dir="${build.classes.home}"/>
    <mkdir dir="${build.war.home}"/>
    <echo>GlassFish</echo>
    &glassfish;
</target>


<target name="compile-common" depends="setup-tc, setup-glassfish" >
    <javac
            fork="true"
            debug="${debug}"
            srcdir="${basedir}/common"
            destdir="${build.classes.home}" >
            <classpath refid="jaxws.classpath"/>
    </javac>
    <copy todir="${build.classes.home}/common">
        <fileset dir="${basedir}/common">
            <exclude name="**/*.java"/>
        </fileset>
    </copy>
</target>

<target name="server" depends="setup-tc, setup-glassfish, compile-common">	
    <antcall target="clean" />
    <antcall target="compile-common" />
    <antcall target="build-server-wsdl" />
    <antcall target="create-server-war" />
    <antcall target="deploy-server" />
</target>

<target name="build-server-wsdl" depends="setup-tc">
    <echo message="In build-server-wsdl in targets.xml"/>
    <echo> server wsdl is : ${server.wsdl}</echo>
	<wsimport
        fork="false"
        debug="${debug}"
        verbose="${verbose}"
        keep="${keep}"
        destdir="${build.classes.home}"
        extension="${extension}"
        wsdl="${basedir}/${server.wsdl}">
        <binding dir="${basedir}" includes="${server.binding}"/>
        <jvmarg value="-Dhttp.proxyHost=${http.proxyHost}"/>
        <jvmarg value="-Dhttp.proxyPort=${http.proxyPort}"/>
        <jvmarg value="-Dhttps.proxyHost=${https.proxyHost}"/>
        <jvmarg value="-Dhttps.proxyPort=${https.proxyPort}"/>
    </wsimport>
    <echo message="In javac in build-server-wsdl in targets.xml"/>
    <javac
        fork="true"
        debug="${debug}"
        srcdir="${basedir}"
        destdir="${build.classes.home}"
        includes="**/server/**">
    	<classpath>
    	    <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
    	</classpath>
    </javac>
</target>

<target name="create-server-war">
    <echo message="In create-server-war in targets.xml"/>
    <war warfile="${build.war.home}/jaxws-${ant.project.name}.war" webxml="${basedir}/etc/server/web.xml">
	<webinf dir="${basedir}/etc/server" includes="sun-jaxws.xml"/>
	<zipfileset
            dir="${basedir}/etc/server"
            includes="PingService.wsdl, *.xsd"
            prefix="WEB-INF/wsdl"/>   
	<classes dir="${build.classes.home}"/>
    </war>
</target>

  <!--Tomcat -->
  <target name="check-if-server-deployed-tom" if="tomcat.home">
    <echo message="Checking for deployed webapp at context path /${server.path}"/>
    <!--condition property="tom.webapp.deployed">
      <http url="http://${endpoint.host}:${endpoint.port}/${server.path}/"/>
    </condition-->
    <echo message="${tom.webapp.deployed}" />
  </target>

 <target name="undeploy-tomcat-proper" if="webapp.deployed">
      <echo message="Undeploying existing webapp at ${path}"/>
      <undeploy-catalina url="http://${endpoint.host}:${endpoint.port}/manager/" username="${username}" password="${password}" path="/${path}"/>
  </target>
                                                                                                                          
  <target name="undeploy-tomcat-war-present" if="war.present">
      <echo message="Undeploying existing webapp at ${path}"/>
      <undeploy-catalina url="http://${endpoint.host}:${endpoint.port}/manager/" username="${username}" password="${password}" path="/${path}"/>
  </target>
                                                                                                                          
  <target name="undeploy-server-tomcat" if="tomcat.home">
   <echo message="Undeploying existing webapp at ${server.path}"/>
    <condition  property="war.present">
        <available file="${tomcat.home}/webapps/jaxws-${ant.project.name}.war" />
    </condition>
    <!--antcall target="undeploy-tomcat-war-present" /-->
    <!--<condition property="webapp.deployed">
      <http url="http://${endpoint.host}:${endpoint.port}/${path}/"/>
    </condition>
    <antcall target="undeploy-tomcat-proper" />-->
 </target>

<target name="deploy-server-tc" depends="check-if-server-deployed-tom, undeploy-server-tomcat" if="tomcat.home">

    <echo message="Deploying war for ${ant.project.name} to tomcat"/>

<!--deploy-catalina url="http://localhost:8080/manager" username="${username}" password="${password}"
            path="/${path}" war="file:${build.war.home}/jaxws-${ant.project.name}.war"/-->
     <copy file="${build.war.home}/jaxws-${ant.project.name}.war" todir="${tomcat.home}/webapps"/>
</target>

<!--Glassfish -->
<target name="deploy-server-glassfish" if="glassfish.home">
    <copy file="${build.war.home}/jaxws-${ant.project.name}.war"
        todir="${glassfish.home}/domains/${domain}/autodeploy"/>
</target>

<target name="deploy-server" depends="deploy-server-tc, deploy-server-glassfish"/>

<target name="generate-client" depends="setup-tc, setup-glassfish">
    <wsimport
        fork="false"
        debug="${debug}"
        verbose="${verbose}"
        extension="${extension}"
        keep="${keep}"
        destdir="${build.classes.home}"
        wsdl="${client.wsdl}">
        <binding dir="${basedir}" includes="${client.binding}"/>
        <jvmarg value="-Dhttp.proxyHost=${http.proxyHost}"/>
        <jvmarg value="-Dhttp.proxyPort=${http.proxyPort}"/> 
    </wsimport>
    <javac
        fork="true"
        debug="${debug}"
        srcdir="${build.classes.home}"
        destdir="${build.classes.home}"
        includes="**/client/**"
        excludes="${excludes}">
        <classpath>
            <pathelement location="${build.classes.home}"/>
            <path refid="jaxws.classpath"/>
        </classpath>
    </javac>
</target>

<target name="client" depends="generate-client, compile-common">
    <javac
        fork="true"
        debug="${debug}"
        srcdir="${basedir}"
        destdir="${build.classes.home}"
        includes="**/client/**"
        excludes="${excludes}">
        <classpath>
            <pathelement location="${build.classes.home}"/>
            <path refid="jaxws.classpath"/>
        </classpath>
    </javac>
</target>

<target name="run-tc" if="tomcat.home">
   <mkdir dir="${build.classes.home}/META-INF/services"/>
   <java fork="true" classname="${client}">
        <sysproperty key="tomcat.home" value="${tomcat.home}"/>
        <sysproperty key="endpoint.host" value="${endpoint.host}"/>
        <sysproperty key="endpoint.port" value="${endpoint.port}"/>
        <sysproperty key="service.url" value="${service.url}"/>
        <sysproperty key="com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump" value="true"/>
        <sysproperty key="javax.net.debug" value="ssl, handshake" />
        <sysproperty key="javax.net.ssl.trustStore" value="${javax.net.ssl.trustStore}"/>
        <sysproperty key="javax.net.ssl.trustStoreType" value="${javax.net.ssl.trustStoreType}"/>
        <sysproperty key="javax.net.ssl.trustStorePassword" value="${javax.net.ssl.trustStorePassword}"/>
       
        <classpath>
            <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
            <pathelement location="${build.classes.home}/../"/>
        </classpath>
        <!--jvmarg value="-Dhttp.proxyHost=${http.proxyHost}"/>
        <jvmarg value="-Dhttp.proxyPort=${http.proxyPort}"/>
        <jvmarg value="-Dhttps.proxyHost=${https.proxyHost}"/>
        <jvmarg value="-Dhttps.proxyPort=${https.proxyPort}"/--> 
        <jvmarg value="-DWSIT_HOME=${tomcat.home}"/>
    </java>  
</target>

<target name="run-glassfish" if="glassfish.home">
   <mkdir dir="${build.classes.home}/META-INF/services"/>
   <java fork="true" classname="${client}">
        <sysproperty key="glassfish.home" value="${glassfish.home}"/>
        <sysproperty key="WSIT_HOME" value="${glassfish.home}"/>
        <sysproperty key="endpoint.host" value="${endpoint.host}"/>
        <sysproperty key="endpoint.port" value="${endpoint.port}"/>
        <sysproperty key="service.url" value="${service.url}"/>
        <sysproperty key="DebugSecurity" value="true"/>
        <sysproperty key="com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump" value="true"/>
      
        <sysproperty key="javax.net.debug" value="ssl, handshake" />
        <classpath>
            <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
            <pathelement location="${build.classes.home}/../"/>
        </classpath>
        <jvmarg value="-Dhttps.proxyHost=${https.proxyHost}"/>
        <jvmarg value="-Dhttps.proxyPort=${https.proxyPort}"/>
        <jvmarg value="-DWSIT_HOME=${glassfish.home}"/>
        <!--
        <jvmarg value="-Xdebug"/>
        <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009"/>
        -->                                                                                                                                                   
    </java>
</target>

<target name="run" depends="setup-tc, run-tc, setup-glassfish, run-glassfish"/>

<target name="clean">
    <delete dir="${build.home}" includeEmptyDirs="true" />
</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>
