<?xml version="1.0"?>

<project name="stopNodeFFDC" default="install" basedir=".">

    <property name="washome" value="${WAS_HOME}"/>
    <echo>washome=${washome}</echo>
    <property name="origFfdcLog" value="com.ibm.ffdc.log=./client_ffdc/"/>
    <echo>origFfdcLog=${origFfdcLog}</echo>

    <target name="install"
            description=""
            depends="detectCurrentOSFamily,
                    setOSFileSeparator,
                    setOSScriptExtension,
                    setNewFfdcLog,
                    fileExist,
                    replaceLine,
                    modifyPermissions"/>

    <target name="uninstall" description="No uninstall action necessary but this target is required."/>


    <!-- Detects Operating System on WAS Installation-->
    <target name="detectCurrentOSFamily" description="Detects the current OS family (UNIX or Windows)">
        <property name="unixBased" value="true"/>
        <condition property="currentOSFamily" value="windows">
            <os family="windows"/>
        </condition>

        <!-- If OS is not Unix-based, set OS as Windows-based -->
        <condition property="unixBased" value="false">
            <os family="windows"/>
        </condition>

        <!-- If Unix-based, set OS to be Unix-based(non-OS/400) -->
        <condition property="currentOSFamily" value="unix">
            <and>
                <os family="unix"/>
                <not>
                    <equals arg1="${os.name}" arg2="OS/400"/>
                </not>
            </and>
        </condition>
        <!-- Outputs detected OS Family | For Testing Purposes -->
        <echo>Detected current OS family to be: ${currentOSFamily}</echo>
    </target>
	

    <!-- Specify file separator, based on detected OS -->
    <target name="setOSFileSeparator"
            description="Sets the current file separator for this platform (file.separator is unreliable on ANT 1.5.4)">

        <condition property="separatorFile" value="/">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>

        <condition property="separatorFile" value="\">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
        <echo>Detected file separator to be: ${separatorFile}</echo>
    </target>


    <!-- Specify file extension for script on currently detected OS -->
    <target name="setOSScriptExtension"
        description="Sets the current file separator for this platform (file.separator is unreliable on ANT 1.5.4)">

        <condition property="OSext" value=".sh">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>

        <condition property="OSext" value=".bat">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
        <echo>Detected file extension to be: ${OSext}</echo>
    </target>
	

    <!-- Sets the com.ibm.ffdc.log JVM property based on detected OS -->
    <target name="setNewFfdcLog"
            description="Sets newFfdcLog, which is the com.ibm.ffdc.log JVM property setting.">

        <condition property="newFfdcLog" value="com.ibm.ffdc.log=&quot;$FFDCLOG&quot;">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>

        <condition property="newFfdcLog" value="com.ibm.ffdc.log=%FFDCLOG%">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
        <echo>Set new com.ibm.ffdc.log value to: ${newFfdcLog}</echo>
    </target>


    <!-- Verifies existence of file in WAS install and the bat file exist -->
    <target name="fileExist">
        <property name="thefile" value="${washome}${separatorFile}bin${separatorFile}stopNode${OSext}"/>
        <echo>thefile = ${thefile}</echo>
        <available property="scriptExists" file="${thefile}" />
    </target>


    <!-- Verifies if script has orignal text, has not been updated -->
    <target name="checkScriptOriginal" if="scriptExists">
        <loadfile property="scriptOriginal" srcfile="${thefile}">
            <filterchain>
                <linecontains>
                    <contains value="${origFfdcLog}"/>
                </linecontains>
            </filterchain>
        </loadfile>
        <!-- Outputs result of verification test to see if script is updated | For Testing Purposes -->
        <echo>Check script original, not updated: ${scriptOriginal}</echo>
    </target>


    <!-- Replace task is common for Windows and Linux install -->
    <target name="replaceLine" depends="checkScriptOriginal"
            description="" if="scriptOriginal">
        <echo>inside replaceLineWin</echo>
        <replace file="${thefile}"
           token="${origFfdcLog}"
           value="${newFfdcLog}"/>
    </target>

	
    <!-- Modify permitions of updated script file, if OS is Unix-based | Extremely Important! -->
    <target name="modifyPermissions"
      if="unixBased">
        <chmod file="${thefile}" perm="a+x"/>
    </target>
	

</project>
