<project name="generateProfileBinCollector" default="generateProfileBinCollector" basedir=".">

    <taskdef name="wscmtlog" 
        classname="com.ibm.ws.install.configmanager.actionengine.ant.utils.AntTaskToLogToConfigManagersLogFiles"/>
    <taskdef name="foreach" 
        classname="com.ibm.ws.install.configmanager.actionengine.ant.utils.ForEachAntTask"/>
    <taskdef name="filename" 
        classname="com.ibm.ws.install.configmanager.actionengine.ant.utils.FileNameAntTask"/>

    <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="defineOSSpecificConfigFlag"
            description="Defines either configUNIX or configWindows depending on the current OS">
            
        <condition property="configOS400" value="true">
            <equals arg1="${currentOSFamily}" arg2="os/400"/>
        </condition>
        
        <condition property="configUNIX" value="true">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>
        
        <condition property="configWindows" value="true">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
    </target>
        
    <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="os/400"/>
        </condition>
        
        <condition property="separatorFile" value="/">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>
        
        <condition property="separatorFile" value="\">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
    </target>

    <target name="setOSCRLFSequence"
            description="Sets CRLF to whatever is the appropriate CRLF sequence for this platform">

        <condition property="CRLF" value="&#10;">
            <equals arg1="${currentOSFamily}" arg2="os/400"/>
        </condition>
        <condition property="CRLF" value="&#10;">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>
        <condition property="CRLF" value="&#13;&#10;">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
    </target>

    <target name="setOSShellScriptExtension"
            description="Sets the variable shellScriptExt to current OS's shell script extension">
            
        <condition property="shellScriptExt" value="">
            <equals arg1="${currentOSFamily}" arg2="os/400"/>
        </condition>
        
        <condition property="shellScriptExt" value="sh">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>
        
        <condition property="shellScriptExt" value="bat">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
    </target>
    
    <target name="generateProxyShellScriptForThisFileOS400"
            description="Generates a proxy shell script for the given shell script for OS400"
            if="configOS400">
            
        <filename path="${pathShellScriptThis}" property="fileShellScriptThis"/>        
        
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="false">#!/usr/bin/qsh${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">binDir=$$(/usr/bin/dirname $$0)${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">. $${binDir}/setupCmdLine${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">$${WAS_HOME}/bin/${fileShellScriptThis} &quot;$@&quot;${CRLF}</echo>
        
    </target>

    <target name="generateProxyShellScriptForThisFileUNIX"
            description="Generates a proxy shell script for the given shell script for UNIX"
            if="configUNIX">
            
        <filename path="${pathShellScriptThis}" property="fileShellScriptThis"/>        
        
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="false">#!/bin/sh${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">binDir=`dirname $${0}`${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">. $${binDir}/setupCmdLine.sh${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">$${WAS_HOME}/bin/${fileShellScriptThis} &quot;$@&quot;${CRLF}</echo>
        
        <chmod perm="u+rwx,g+rx-w,o+rx-w" file="${profilePath}/bin/${fileShellScriptThis}"/>
    </target>

    <target name="generateProxyShellScriptForThisFileWindows"
            description="Generates a proxy shell script for the given shell script for Windows"
            if="configWindows">
            
        <filename path="${pathShellScriptThis}" property="fileShellScriptThis"/>
        
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="false">@echo off${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">SETLOCAL${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">call &quot;%~dp0setupCmdLine.bat&quot;${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">call &quot;%WAS_HOME%\bin\${fileShellScriptThis}&quot; %*${CRLF}</echo>
        <echo file="${profilePath}/bin/${fileShellScriptThis}" append="true">ENDLOCAL${CRLF}</echo>
    </target>
        
    <target name="generateProxyShellScriptForThisFile"
            description="Generates the proxy shell script for the given file">
            
        <antcall target="generateProxyShellScriptForThisFileOS400">
            <param name="pathShellScriptThis" value="${pathShellScriptThis}"/>
        </antcall>
        <antcall target="generateProxyShellScriptForThisFileUNIX">
            <param name="pathShellScriptThis" value="${pathShellScriptThis}"/>
        </antcall>
        <antcall target="generateProxyShellScriptForThisFileWindows">
            <param name="pathShellScriptThis" value="${pathShellScriptThis}"/>
        </antcall>            
    </target>
        
    <target name="generateProxyShellScriptForCollector"
            description="Generates shell scripts in profile bin directory for collector tool"
            unless="configOS400">

        <antcall target="generateProxyShellScriptForThisFile">
            <param name="pathShellScriptThis" value="${WAS_HOME}${separatorFile}bin${separatorFile}collector.${shellScriptExt}"/>
        </antcall>
    </target>

    <target name="generateProxyShellScriptForCollectorOS400"
            description="Generates shell scripts in profile bin directory for collector tool"
            if="configOS400">

        <antcall target="generateProxyShellScriptForThisFile">
            <param name="pathShellScriptThis" value="${WAS_HOME}${separatorFile}bin${separatorFile}collector"/>
        </antcall>
    </target>

    <target name="generateProfileBinCollector"
        description="Generates bin shell scripts in the profile bin folder for collector tool"
        depends="detectCurrentOSFamily,
            defineOSSpecificConfigFlag,
            setOSFileSeparator,
            setOSCRLFSequence,
            setOSShellScriptExtension,
            generateProxyShellScriptForCollectorOS400,
            generateProxyShellScriptForCollector"/>                      
</project>
