<!--

    Copyright (c) 2018 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="TOMCAT Configuration for VI">

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

    <!--
      Tomcat configuration properties: These must be set by the user set in the ts.jte file
  
      webcontainer.home - base directory of tomcat for use with the SAAJ VI Implementation
    -->

    <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>
    </target>

    <property name="server.start.delay.secs"	value="10"/>
    <property name="server.spawn.delay.secs"	value="5"/>
    <property name="startup.path"		value="${webcontainer.home}/bin/startup"/>
    <property name="shutdown.path"		value="${webcontainer.home}/bin/shutdown"/>

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

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

    <!--
        Start The Tomcat Server by calling the appropriate target
        depending on whether we are on unix/linux or windows.
    -->
    <target name="start.tomcat">
       <antcall target="start.tomcatserver.unix"/>
       <antcall target="start.tomcatserver.win"/>
    </target>

    <!--
        Stop The Tomcat Server by calling the appropriate target
        depending on whether we are on unix/linux or windows.
    -->
    <target name="stop.tomcat">
       <antcall target="stop.tomcatserver.unix"/>
       <antcall target="stop.tomcatserver.win"/>
    </target>

    <!--
        Start The Tomcat Server on unix/linux
    -->
    <target name="start.tomcatserver.unix" depends="configPlatform" if="isUnixPlatform">
      <spawn executable="${exec.startup}" failifexecutionfails="false">
        <arg line="${exec.startup.part2}" />
      </spawn>
      <sleep seconds="${server.spawn.delay.secs}"/> 
    </target>

    <!--
        Start the Tomcat Server on windows
    -->
    <target name="start.tomcatserver.win" depends="configPlatform" if="isWindowsPlatform">
      <!-- Create wrapper on windows for startup.bat since CATALINA_HOME needs to be set -->
      <echo file="${startup.path}wrapper.bat">set CATALINA_HOME=${webcontainer.home}${line.separator}</echo>
      <echo file="${startup.path}wrapper.bat" append="true">%CATALINA_HOME%\bin\startup.bat${line.separator}</echo>
      <spawn executable="${exec.startup}" failifexecutionfails="false">
        <arg line="${exec.startup.part2}" />
      </spawn>
      <sleep seconds="${server.spawn.delay.secs}"/> 
      <delete file="${startup.path}wrapper.bat"/>
    </target>

    <!--
        Stop The Tomcat Server on unix/linux
    -->
    <target name="stop.tomcatserver.unix" depends="configPlatform" if="isUnixPlatform">
      <spawn executable="${exec.shutdown}" failifexecutionfails="false">
        <arg line="${exec.shutdown.part2}" />
      </spawn>
      <sleep seconds="${server.spawn.delay.secs}"/> 
    </target>

    <!--
        Stop The Tomcat Server on windows
    -->
    <target name="stop.tomcatserver.win" depends="configPlatform" if="isWindowsPlatform">
      <!-- Create wrapper on windows for shutdown.bat since CATALINA_HOME needs to be set -->
      <echo file="${shutdown.path}wrapper.bat">set CATALINA_HOME=${webcontainer.home}${line.separator}</echo>
      <echo file="${shutdown.path}wrapper.bat" append="true">%CATALINA_HOME%\bin\shutdown.bat${line.separator}</echo>
      <spawn executable="${exec.shutdown}" failifexecutionfails="false">
        <arg line="${exec.shutdown.part2}" />
      </spawn>
      <sleep seconds="${server.spawn.delay.secs}"/> 
      <delete file="${shutdown.path}wrapper.bat"/>
    </target>

    <target name="tomcat-start">
      <if>
      <not>
        <http url="http://${webServerHost}:${webServerPort}"/>
      </not>
      <then>
        <echo message="Starting TOMCAT ...."/>
	<antcall target="start.tomcat"/>
        <echo message=" ...Waiting for server to be startup ...(sleeping for ${server.start.delay.secs} seconds) " /> 
        <sleep seconds="${server.start.delay.secs}"/> 
        <echo message="TOMCAT started ...."/>
      </then>
      <else>
        <echo message="TOMCAT is already started ...."/>
      </else>
      </if>
      <antcall target="check.tomcat.running.unix"/>
    </target>
    
    <target name="check.tomcat.running.unix" depends="configPlatform" if="isUnixPlatform">
      <echo>checking</echo>
      <exec executable="sh" newenvironment="false" dir=".">
        <arg value="-c" />
        <arg value="/bin/ps -aef | grep tomcat" />
      </exec>
    </target>
    
    <target name="tomcat-stop">
      <if>
        <http url="http://${webServerHost}:${webServerPort}"/>
      <then>
        <echo message="Stopping TOMCAT ...."/>
	<antcall target="stop.tomcat"/>
        <echo message=" ...Waiting for server to shutdown ...(sleeping for ${server.start.delay.secs} seconds) " /> 
        <sleep seconds="${server.start.delay.secs}"/> 
        <echo message="TOMCAT stopped ...."/>
      </then>
      <else>
        <echo message="TOMCAT is already stopped ...."/>
      </else>
      </if>
    </target> 

    <target name="start-server">
      <antcall target="tomcat-start"/>
    </target>

    <target name="stop-server">
      <antcall target="tomcat-stop"/>
    </target>

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

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

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

    <target name="delete.tck.jars">
       <delete file="${webcontainer.home}/shared/lib/saajtck.jar" quiet="true"/>
       <delete file="${webcontainer.home}/shared/lib/tsharness.jar" quiet="true"/>
    </target>
</project>
