<!--

    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 Deployment" default="-deploy">

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

    <!--<property name="deploy.dir" value="${deploy.dir}/"/>-->

    <!-- PRESETDEFS -->
    <presetdef name="tc.echo">
        <echo message="Tomcat:  ${message}"/>
    </presetdef>

    <presetdef name="tc.fail">
        <fail message="Tomcat:  ${message}"/>
    </presetdef>

    <!-- Deployment: DO NOT EDIT -->
    <target name="-deploy">

        <if>
            <isset property="archive.file"/>
            <then>
                <fileset file="${archive.file}" id="deploy.archive.set"/>
                <tc.echo message="Deploying:  ${archive.file}"/>
            </then>
        </if>

        <ts.tomcat.undeploy>
            <fileset refid="deploy.archive.set"/>
        </ts.tomcat.undeploy>

        <ts.tomcat.deploy>
            <fileset refid="deploy.archive.set"/>
        </ts.tomcat.deploy>
    </target>

    <target name="-deploy.all">
        <ts.tomcat.undeploy>
            <fileset refid="deploy.all.archive.set"/>
        </ts.tomcat.undeploy>

        <ts.tomcat.deploy>
            <fileset refid="deploy.all.archive.set"/>
        </ts.tomcat.deploy>
    </target>

    <target name="-undeploy.all">
        <fileset dir="${deploy.dir}" id="undeploy.all.archive.set">
                <include name="**/*.war"/>
        </fileset>
        <ts.tomcat.undeploy>
            <fileset refid="undeploy.all.archive.set"/>
        </ts.tomcat.undeploy>
    </target>

    <target name="-undeploy">
        <if>
            <isset property="archive.file"/>
            <then>
                <fileset file="${archive.file}" id="deploy.archive.set"/>
                <tc.echo message="Undeploying:  ${archive.file}"/>
            </then>
        </if>

        <ts.tomcat.undeploy>
            <fileset refid="deploy.archive.set"/>
        </ts.tomcat.undeploy>
    </target>

    <target name="-isdeployed">

    </target>

    <macrodef name="ts.tomcat.deploy">

        <!--

        -->
        <element name="archiveset" implicit="yes" optional="true"
                 description="Fileset for dirs to search for archives"/>

        <sequential>

        <var name="archive.count" unset="true"/>
        <var name="tomcat.deploy.wait.time" unset="true"/>

        <resourcecount property="archive.count">
            <archiveset/>
        </resourcecount>
        <ts.verbose message="archive count - ${archive.count}"/>

        <tc.echo message="${archive.count} archive(s) to deploy"/>
        <if>
            <not>
                <equals arg1="${archive.count}" arg2="0"/>
            </not>
        <then>
            <copy todir="${deploy.dir}" flatten="true">
                <archiveset/>
                <chainedmapper>
                <filtermapper>
                    <replacestring from="vi_built_" to=""/>
                </filtermapper>
                <flattenmapper/>
                </chainedmapper>
            </copy>

            <!-- Set a default in case the deliverable foes not set it in the jte file -->
            <property name="impl.deploy.timeout.multiplier" value="10"/>

            <math result="tomcat.deploy.wait.time"
                  operand1="${archive.count}"
                  operation="*"
                  operand2="${impl.deploy.timeout.multiplier}"
                  datatype="int"/>

            <for param="archive">
                <archiveset/>

            <sequential>
                <propertyregex property="archive.name.only"
                  input="@{archive}"
                  regexp=".*[\/|\\](.*)"
                  select="\1"
                  casesensitive="false"
                  override="true"/>

                <propertyregex property="archive.name.minus.suffix"
                  input="${archive.name.only}"
                  regexp="\.war" replace=""
                  casesensitive="false"
                  override="true"/>

                <propertyregex property="archive.name.minus.suffix.and.vibuilt"
                  input="${archive.name.minus.suffix}"
                  regexp="vi_built_" replace=""
                  casesensitive="false"
                  override="true"
                  defaultvalue="${archive.name.minus.suffix}"/>

                <waitfor maxwait="${tomcat.deploy.wait.time}" maxwaitunit="second"
                         timeoutproperty="deploy.timeout">
                   <available file="${deploy.dir}/${archive.name.minus.suffix.and.vibuilt}"
                                   type="dir"/>
                </waitfor>

                <if>
                    <available file="${deploy.dir}/${archive.name.minus.suffix.and.vibuilt}"
                               type="dir"/>
                    <then>
                        <tc.echo message="Deployment Succeeded:  ${archive.name.only}"/>
                    </then>
                <else>
                    <!-- Assume timeourt occurred and warn user to either increase timeout -->
                    <tc.echo message="Deployment timeout reached - ${tomcat.deploy.wait.time} seconds."/>
                    <tc.fail message="Deployment Failed for one ore more archives.  Check the log to see which failed."/>
                </else>
                </if>
            </sequential>
            </for>
        </then>
        </if>
        </sequential>
    </macrodef>

    <macrodef name="ts.tomcat.undeploy">

        <!--

        -->
        <element name="archiveset" implicit="yes" optional="true"
                 description="Fileset for dirs to search for archives"/>

        <sequential>

        <pathconvert property="files.to.remove"
                     pathsep=",">
            <archiveset/>
            <flattenmapper/>
        </pathconvert>

        <if>
            <contains string="${files.to.remove}" substring="vi_built_"/>
            <then>
                <pathconvert property="files.to.remove2"
                     pathsep=",">
                    <archiveset/>
                    <chainedmapper>
                        <flattenmapper/>
                        <globmapper from="vi_built_*" to="*"/>
                    </chainedmapper>
                </pathconvert>
                <var name="files.to.remove" value="${files.to.remove2}"/>
            </then>
        </if>

        <ts.verbose message="files.to.remove = ${files.to.remove}"/>

        <if>
            <not>
                <equals arg1="${files.to.remove}" arg2=""/>
            </not>
        <then>

            <fileset dir="${deploy.dir}"
                     includes="${files.to.remove}"
                     id="deploy.dir.archiveset">
            </fileset>

            <var name="files.to.remove" unset="true"/>
            <pathconvert property="files.to.remove"
                         pathsep=",">
                <fileset refid="deploy.dir.archiveset"/>
                <flattenmapper/>
            </pathconvert>
            <ts.verbose message="files.to.remove = ${files.to.remove}"/>

            <var name="archive.count" unset="true"/>
            <var name="tomcat.deploy.wait.time" unset="true"/>

            <!-- Set a default in case the deliverable foes not set it in the jte file -->
            <property name="impl.deploy.timeout.multiplier" value="10"/>


            <resourcecount property="archive.count">
                <fileset refid="deploy.dir.archiveset"/>
            </resourcecount>
            <ts.verbose message="archive count - ${archive.count}"/>

            <tc.echo message="${archive.count} archive(s) to undeploy"/>
            <if>
                <not>
                    <equals arg1="${archive.count}" arg2="0"/>
                </not>
            <then>
                <var name="undeploy.failed" value="false"/>

                <delete includeEmptyDirs="true"
                    quiet="true">
                   <fileset refid="deploy.dir.archiveset"/>
                </delete>

                <math result="tomcat.deploy.wait.time"
                      operand1="${archive.count}"
                      operation="*"
                      operand2="${impl.deploy.timeout.multiplier}"
                      datatype="int"/>

                <for param="archive" list="${files.to.remove}">
                <sequential>
                    <ts.verbose message="archive = @archive"/>

                    <propertyregex property="archive.name.minus.suffix"
                                  input="@{archive}"
                                  regexp="\.war" replace=""
                                  casesensitive="false"
                                  override="true"/>


                    <waitfor maxwait="${tomcat.deploy.wait.time}" maxwaitunit="second"
                             timeoutproperty="deploy.timeout">
                        <not>
                            <available file="${deploy.dir}/${archive.name.minus.suffix}"
                                       type="dir"/>
                        </not>
                    </waitfor>

                    <if>
                        <not>
                            <available file="${deploy.dir}/${archive.name.minus.suffix}"
                                       type="dir"/>
                        </not>
                    <then>
                        <tc.echo message="Undeployment Succeeded:  @{archive}"/>
                    </then>
                    <else>
                        <!-- Assume timeout occurred and warn user to either increase timeout -->
                        <ts.verbose message="Undeployment timeout reached - ${tomcat.deploy.wait.time} seconds."/>
                        <tc.fail message="Undeployment Failed for some archives.  Check the log to see which failed."/>
                    </else>
                    </if>
                </sequential>
                </for>
            </then>
            </if>
        </then>
        </if>
        </sequential>
    </macrodef>

</project>
