<?xml version="1.0" encoding="UTF-8"?>
<project name="testsuite">
    <target name="-check.skipped">
        <condition property="tests.skipped">
            <or>
                <isset property="skipTests"/>
                <isset property="maven.test.skip"/>
                <isset property="maven.test.skip.exec"/>
            </or>
        </condition>
    </target>

    <target name="create-distro" depends="-check.skipped" unless="tests.skipped">
        <echo message="Creating test server distro at ${server.dist}"/>
        <echo message="Using distribution ${server.build.dist}"/>
        <copy todir="${server.dist}">
            <fileset dir="${server.build.dist}"/>
        </copy>
        <!-- It happens when you do not specify namespaces in the XSLT templates for the newly added nodes
             then the IBM jdk transformation is adding empty namespace there - not possible to influence this
             behaviour by any environmental property - this is a bit workaround for it -->
        <echo message="Removing empty xmlns attributes (xmlns='') which IBM JDK could produce"/>
        <replace dir="target" value="">
            <include name="server/standalone/configuration/**/*.xml"/>
            <replacetoken><![CDATA[xmlns=""]]></replacetoken>
        </replace>
    </target>

    <target name="start-server" depends="-check.skipped" unless="tests.skipped">
        <echo>Starting Infinispan server from ${server.dist}</echo>
        <exec dir="${server.dist}/bin" executable="chmod" osfamily="unix">
            <arg value="+x"/>
            <arg value="../bin"/>
            <arg value="standalone.sh"/>
        </exec>
        <exec executable="${server.dist}/bin/standalone.sh" osfamily="unix" spawn="true">
            <arg line="-c ${jboss.config.file}"/>
            <env key="JAVA_OPTS" value="${server.jvm.args} -Djboss.socket.binding.port-offset=${port.offset} -Djboss.node.name=${jboss.node.name}"/>
            <env key="JBOSS_HOME" value="${server.dist}"/>
        </exec>
        <exec executable="${server.dist}/bin/standalone.bat" osfamily="windows" spawn="true">
            <arg line="-c ${jboss.config.file}"/>
            <env key="JAVA_OPTS" value="${server.jvm.args} -Djboss.socket.binding.port-offset=${port.offset} -Djboss.node.name=${jboss.node.name}"/>
            <env key="JBOSS_HOME" value="${server.dist}"/>
        </exec>
        <echo>Waiting for Infinispan server to start</echo>
        <waitfor maxwait="15" maxwaitunit="second" checkevery="1" checkeveryunit="second">
            <and>
                <socket server="127.0.0.1" port="${management.port}"/>
                <socket server="127.0.0.1" port="${hotrod.port}"/>
            </and>
        </waitfor>
        <echo message="Infinispan server started"/>
    </target>

    <target name="-do.kinit" depends="-check.skipped" unless="tests.skipped">
        <exec executable="bash" output="ps.pid" resultproperty="ps.result" failifexecutionfails="false" failonerror="false" osfamily="unix">
            <arg value="-c"/>
            <arg value="ps -eaf | grep jboss-modules.jar | grep -v -w grep | awk '{print $$2;}'"/>
        </exec>
        <echo>ps.pid: ps.pid --- ${ps.result}</echo>
        <condition property="run.ps" value="true" else="false">
            <and>
                <equals arg1="${ps.result}" arg2="0"/>
                <length file="ps.pid" when="greater" length="0"/>
            </and>
        </condition>
        <exec executable="bash" outputproperty="lsof.pid" resultproperty="lsof.result" failifexecutionfails="false" failonerror="false" osfamily="unix">
            <arg value="-c"/>
            <arg value="lsof -t -i TCP:${hotrod.port}"/>
        </exec>
        <echo>lsof.pid: ${lsof.pid} --- ${lsof.result}</echo>
        <condition property="run.lsof" value="true" else="false">
            <and>
                <equals arg1="${lsof.result}" arg2="0"/>
                <isset property="lsof.pid"/>
                <not>
                    <equals arg1="${lsof.pid}" arg2=""/>
                </not>
            </and>
        </condition>
        <!--jps/jstat are not supported in IBM JDK we should add failifexecutionfails parameter to not fail when jps command does not exist -->
        <exec executable="${server.jvm}/bin/jps" output="jps.pid" resultproperty="jps.result" failifexecutionfails="false" failonerror="false"/>
        <echo>jps.pid: jps.pid --- ${jps.result}</echo>
        <condition property="run.jps" value="true" else="false">
            <and>
                <equals arg1="${jps.result}" arg2="0"/>
                <length file="jps.pid" when="greater" length="0"/>
            </and>
        </condition>
        <exec executable="cmd" output="cmd.pid" resultproperty="cmd.result" failifexecutionfails="false" failonerror="false" osfamily="windows">
            <arg value="/C"/>
            <arg value="netstat -aon | findstr LISTENING | findstr :${hotrod.port}"/>
        </exec>
        <echo>cmd.pid: cmd.pid --- ${cmd.result}</echo>
        <condition property="run.cmd" value="true" else="false">
            <and>
                <equals arg1="${cmd.result}" arg2="0"/>
                <length file="cmd.pid" when="greater" length="0"/>
            </and>
        </condition>
        <fail message="Not yet supported on UNIX/WINDOWS favour without working ps/lsof/jps/netstat">
            <condition>
                <not>
                    <or>
                        <istrue value="${run.ps}"/>
                        <istrue value="${run.lsof}"/>
                        <istrue value="${run.jps}"/>
                        <istrue value="${run.cmd}"/>
                    </or>
                </not>
            </condition>
        </fail>
    </target>

    <target name="-do.ps" if="${run.ps}">
        <echo>this target will be called only when property $${run.ps} is set</echo>
        <loadfile srcfile="ps.pid" property="ppid" failonerror="false">
            <filterchain>
                <prefixlines prefix=" " />
                <striplinebreaks />
                <trim />
            </filterchain>
        </loadfile>
        <exec executable="kill" osfamily="unix">
            <arg line="-9 ${ppid}"/>
        </exec>
        <delete file="ps.pid"/>
    </target>

    <target name="-do.lsof" if="${run.lsof}">
        <echo>this target will be called only when property $${run.lsof} is set</echo>
        <exec executable="kill" osfamily="unix">
            <arg line="-9 ${lsof.pid}"/>
        </exec>
    </target>

    <target name="-do.jps" if="${run.jps}">
        <echo>this target will be called only when property $${run.jps} is set</echo>
        <loadfile srcfile="jps.pid" property="jpid" failonerror="false">
            <filterchain>
                <linecontains>
                    <contains value="jboss-modules.jar"/>
                </linecontains>
                <tokenfilter>
                    <deletecharacters chars="jboss-modules.jar"/>
                    <ignoreblank/>
                </tokenfilter>
                <striplinebreaks/>
            </filterchain>
        </loadfile>
        <exec executable="kill" osfamily="unix">
            <arg line="-9 ${jpid}"/>
        </exec>
        <delete file="jps.pid"/>
    </target>

    <target name="-do.cmd" if="${run.cmd}">
        <echo>this target will be called only when property $${run.cmd} is set</echo>
        <loadfile srcfile="cmd.pid" property="cpid" failonerror="false">
            <filterchain>
                <linecontains>
                    <contains value="LISTENING"/>
                    <contains value=":${hotrod.port}"/>
                </linecontains>
                <tokenfilter>
                    <replaceregex pattern=".*LISTENING([ \t]+)([0-9]+)" replace="\2" flags="gi"/>
                    <ignoreblank/>
                </tokenfilter>
                <striplinebreaks/>
            </filterchain>
        </loadfile>
        <exec executable="taskkill" osfamily="windows">
            <arg line="/F /T /PID ${cpid}"/>
        </exec>
        <delete file="cmd.pid"/>
    </target>

    <target name="kill-server" depends="-do.kinit, -do.ps, -do.lsof, -do.jps, -do.cmd"/>
</project>
