<project name="collectNodeMetadata" default="collectNodeMetadata" basedir=".">

    <taskdef name="wscmtlog"
        classname="com.ibm.ws.install.configmanager.actionengine.ant.utils.AntTaskToLogToConfigManagersLogFiles"/>

    <target name="detectCurrentOSFamily"
            description="Detecs the current OS family (UNIX or Windows)">

        <!-- OS/400 setting must come before the unix setting -->
        <condition property="currentOSFamily" value="os/400">
            <equals arg1="${os.name}" arg2="OS/400"/>
        </condition>

        <condition property="currentOSFamily" value="windows">
            <os family="windows"/>
        </condition>

        <condition property="currentOSFamily" value="unix">
            <os family="unix"/>
        </condition>

        <wscmtlog>Detected current OS family to be: ${currentOSFamily}</wscmtlog>
    </target>

    <target name="getWASVersion"
            description="detects and sets properties based on version of was">
        <loadfile property="productString" srcfile="${WAS_HOME}/properties/version/WAS.product"/>

	<condition property="ND8x">
            <contains string="${productString}" substring="&lt;version&gt;8."/>
        </condition>
	<condition property="ND85">
            <contains string="${productString}" substring="&lt;version&gt;8.5"/>
        </condition>
	<condition property="ND80">
            <contains string="${productString}" substring="&lt;version&gt;8.0."/>
        </condition>
        <condition property="ND7x">
            <contains string="${productString}" substring="&lt;version&gt;7."/>
        </condition>
        <condition property="ND70">
            <contains string="${productString}" substring="&lt;version&gt;7.0."/>
        </condition>
        <condition property="ND6x">
            <contains string="${productString}" substring="&lt;version&gt;6."/>
        </condition>
        <condition property="ND61">
            <contains string="${productString}" substring="&lt;version&gt;6.1."/>
        </condition>
	<wscmtlog>ND8x=${ND8x}</wscmtlog>
	<wscmtlog>ND85=${ND85}</wscmtlog>
	<wscmtlog>ND80=${ND80}</wscmtlog>
        <wscmtlog>ND7x=${ND7x}</wscmtlog>
        <wscmtlog>ND70=${ND70}</wscmtlog>
        <wscmtlog>ND6x=${ND6x}</wscmtlog>
        <wscmtlog>ND61=${ND61}</wscmtlog>
    </target>

    <target name="setCurrentOSFamily"
            description="Detects the current OS family (UNIX or Windows)">

        <!-- OS/400 setting must come before the unix setting -->
        <condition property="isCurrentOSOS400" value="true">
            <equals arg1="${os.name}" arg2="OS/400"/>
        </condition>

        <condition property="isCurrentOSWindows" value="true">
            <os family="windows"/>
        </condition>

        <condition property="isCurrentOSUNIX" value="true">
            <os family="unix"/>
        </condition>
    </target>

    <target name="resolveCollectMetadataExecutableForTheCurrentPlatform"
            depends="getWASVersion"
            description="Sets COLLECT_METADATA to the right script for the current platform">

        <condition property="COLLECT_METADATA" value="${basedir}/scripts/collectVENodeMetadata">
            <equals arg1="${currentOSFamily}" arg2="os/400"/>
        </condition>

        <condition property="COLLECT_METADATA" value="${basedir}/scripts/collectVENodeMetadata70.sh">
            <and>
               <equals arg1="${currentOSFamily}" arg2="unix"/>
               <equals arg1="${ND7x}" arg2="true" />
            </and>
        </condition>

        <condition property="COLLECT_METADATA" value="${basedir}/scripts/collectVENodeMetadata70.bat">
            <and>
               <equals arg1="${currentOSFamily}" arg2="windows"/>
               <equals arg1="${ND7x}" arg2="true" />
            </and>
        </condition>

        <condition property="COLLECT_METADATA" value="${basedir}/scripts/collectVENodeMetadata.sh">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>

        <condition property="COLLECT_METADATA" value="${basedir}/scripts/collectVENodeMetadata.bat">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>

        <wscmtlog>collectNodeMetadata interpretor set to: ${COLLECT_METADATA}</wscmtlog>
    </target>

    <target name="resolveSetupCmdLineName">
        <available property="setupCmdLineName"
            value="setupCmdLine"
            file="${WAS_HOME}/bin/setupCmdLine"/>

        <available property="setupCmdLineName"
            value="setupCmdLine.bat"
            file="${WAS_HOME}/bin/setupCmdLine.bat"/>

        <available property="setupCmdLineName"
            value="setupCmdLine.sh"
            file="${WAS_HOME}/bin/setupCmdLine.sh"/>

        <wscmtlog>Detected setupCmdLine script name to be: ${setupCmdLineName}</wscmtlog>
        <wscmtlog>Detected setupCmdLine script name to be: ${setupCmdLineName}</wscmtlog>
    </target>

    <target name="runCollectNodeMetadata"
            description="collects the node metadata and writes to local repository">

        <exec executable="${COLLECT_METADATA}"
                output="${profilePath}/logs/collect_metadata.log" failonerror="true">
            <arg value=""/>
		<env key="WAS_USER_SCRIPT" value="${profilePath}/bin/${setupCmdLineName}"/>
            <env key="PROFILE_CONFIG_ACTION" value="true"/>
        </exec>

        <wscmtlog>Done.</wscmtlog>
    </target>

    <target name="collectNodeMetadata"
        description="Collects Node Metadata"
        depends="detectCurrentOSFamily,
            setCurrentOSFamily,
            resolveCollectMetadataExecutableForTheCurrentPlatform,
		resolveSetupCmdLineName,
		runCollectNodeMetadata"/>

</project>

