<!--

    Copyright (c) 2018, 2021 Oracle and/or its affiliates. All rights reserved.

    This program and the accompanying materials are made available under the
    terms of the Eclipse Public License v. 2.0, which is available at
    http://www.eclipse.org/legal/epl-2.0.

    This Source Code may also be made available under the following Secondary
    Licenses when the conditions for such availability set forth in the
    Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
    version 2 with the GNU Classpath Exception, which is available at
    https://www.gnu.org/software/classpath/license.html.

    SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->

<project name="GlassFish Configuration for VI">

    <!-- IMPORTS -->
    <import file="./ts.common.props.xml" optional="true"/>
    <import file="../../../../../../bin/xml/ts.common.props.xml" optional="true"/>

    <!--
      GlassFish configuration properties: See the ts.jte file
    -->

    <property name="server.start.delay.mins" value="5"/>

    <property name="cli.path"        value="${webcontainer.home}/bin/asadmin"/>

    <target name="configPlatform"
           depends="checkPlatform, configUnix, configWindows">
    </target>

    <target name="checkPlatform" >
        <condition property ="isUnixPlatform">
           <os family="unix"/>
        </condition>
        <condition property ="isWindowsPlatform">
           <os family="windows"/>
        </condition>
        <condition property ="os" value="unix">
           <os family="unix"/>
        </condition>
        <condition property ="os" value="windows">
           <os family="windows"/>
        </condition>
      	<condition property ="isMac">
         <os family="mac"/>
      	</condition>
    </target>

    <target name="config.vi" depends="configPlatform">
    	<antcall target="stop-server"/>
    	<antcall target="copy.tck.jars"/>
    	<antcall target="start-server"/>
    </target>

    <target name="clean.vi" depends="configPlatform">
    	<antcall target="start-server"/>
    	<antcall target="delete.tck.jars"/>
    	<antcall target="stop-server"/>
    	<antcall target="start-server"/>
    </target>

    <target name="configUnix" depends="checkPlatform" if="isUnixPlatform" >
           <echo message="on unix"/>
           <property name="dir.sep" value="/"/>
           <property name="exec.asadmin" value="${cli.path}" />
           <property name="exec.asadmin.part2" value="" />
    </target>

    <target name="configWindows" depends="checkPlatform" if="isWindowsPlatform" >
           <echo message="on windows"/>
           <property name="dir.sep" value="\\"/>
           <property name="exec.asadmin" value="cmd.exe" />
           <property name="exec.asadmin.part2" value= " /c ${cli.path}" />
    </target>

    <!--
        Start The App Server by calling the appropriate target
        depending on whether we are on unix/linux or windows.
    -->
    <target name="start-server">
       <antcall target="start.appserver.unix"/>
       <antcall target="start.appserver.win"/>
    </target>
    <!--
        Start The App Server on unix/linux
    -->
    <target name="start.appserver.unix" depends="configPlatform" if="isUnixPlatform">
      <exec executable="${exec.asadmin}" failifexecutionfails="false">
        <arg line="${exec.asadmin.part2}" />
        <arg line=" start-domain"/>
        <arg line=" ${glassfish.domain.name}"/>
      </exec>
      <echo message=" The App Server is now available." />
    </target>

    <!--
        Start the App Server on windoze
    -->
    <target name="start.appserver.win" depends="configPlatform" if="isWindowsPlatform">
      <spawn executable="${exec.asadmin}" failifexecutionfails="false">
        <arg line="${exec.asadmin.part2}" />
        <arg line=" start-domain"/>
        <arg line=" ${glassfish.domain.name}"/>
      </spawn>
      <echo message=" ...Waiting for server to be up ...(sleeping for ${server.start.delay.mins} minutes) " /> 
      <!-- 
        Currently the waitfor task allocates a port everytime it checks the admin server URL.
        If one of the server ports is allocated to the waitfor task we get a server startup error.
        Asadmin start-domain is a synchronous call but in the case of ant on windows the asadmin
        start-domain never returns to the ant exec task so it hangs forever.  For now we simply
        sleep for server.start.delay.mins minutes to give the app server more than enough time to come up.  This is
        a temparay fix until we implement something better.
      -->
      <sleep minutes="${server.start.delay.mins}"/> 
      <echo message=" HTTP server is now available at http://${webServerHost}:${webServerPort}" />
    </target>

    <!--
        Stop The App Server
    -->
    <target name="stop-server" depends="configPlatform">
      <exec executable="${exec.asadmin}" failifexecutionfails="false">
        <arg line="${exec.asadmin.part2}" />
        <arg line=" stop-domain"/>
        <arg line=" ${glassfish.domain.name}"/>
      </exec>
    </target>

    <target name="copy.tck.jars">
        <copy file="${ts.home}/lib/saajtck.jar" tofile="${extension.dir}/saajtck.jar" overwrite="yes" />
        <copy file="${ts.home}/lib/tsharness.jar" tofile="${extension.dir}/tsharness.jar" overwrite="yes" />
    </target>

    <target name="delete.tck.jars">
       <delete file="${extension.dir}/saajtck.jar" quiet="true"/>
       <delete file="${extension.dir}/tsharness.jar" quiet="true"/>
    </target>

</project>
