<!DOCTYPE project [
    <!ENTITY importExternalLogs SYSTEM "file:./scripts/importExternalLogs.xml">
]>
<project name="deleteProfileShortCutFromStartMenu" default="deleteProfileShortCutFromStartMenu" basedir=".">
    <taskdef name="wscmtlog" classname="com.ibm.ws.install.configmanager.actionengine.ant.utils.AntTaskToLogToConfigManagersLogFiles"/>
    <taskdef name="resourcebundle" classname="com.ibm.ws.install.configmanager.actionengine.ant.utils.ResourceBundleAntTask"/> 
  	&importExternalLogs;         
    <target name="detectCurrentOSFamily"
            description="Detects 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="linux">
            <equals arg1="${os.name}" arg2="Linux"/>
        </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="configLinux" value="true">
            <equals arg1="${currentOSFamily}" arg2="linux"/>
        </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="linux"/>
        </condition>
        
        <condition property="separatorFile" value="/">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>
        
        <condition property="separatorFile" value="\">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
    </target>

    <target name="defineDisableWASDesktopIntegrationFlag"
            description="Define the flag for start menu creation based on environment variable DisableWASDesktopIntegration">

        <property environment="env"/>
        <condition property="disableWASDesktopIntegration" value="true">
            <equals arg1="${env.DisableWASDesktopIntegration}" arg2="false"/>
        </condition>

    </target>
        
    <target name="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
        <replace file="${file}"
            token="${token}"
            value="${tokenValue}"/>
    </target>

    <target name="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
        <replace dir="${dir}"           
            token="${token}"
            value="${tokenValue}">
            <include name="**/*.${fileType}"/>
        </replace>
    </target>    

    <target name="processJScriptForDeletion"
            if="configWindows">                       
            
            <copy file="${WAS_HOME}${separatorFile}properties${separatorFile}version${separatorFile}nif${separatorFile}config${separatorFile}script${separatorFile}profileShortCutStartMenuDelete.js"
                  tofile="${WAS_HOME}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js"/>
                  
        <property name="BUNDLE_NAME" value="com.ibm.ws.install.configmanager.resourcebundle.ConfigManagerResourceBundle"/>
        <resourcebundle resourceBundleName="${BUNDLE_NAME}" resourceBundleKey="Program.profiles" param="" property="profilesDir"/>
        
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${WAS_HOME}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js"/>
			<param name="token" value="${PROFILESDIR}"/>
			<param name="tokenValue" value="${profilesDir}"/>
	    </antcall>            
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${WAS_HOME}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js"/>
			<param name="token" value="${PROFILENAME}"/>
			<param name="tokenValue" value="${profileName}"/>
	    </antcall>            
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${WAS_HOME}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js"/>
			<param name="token" value="${WASROOT}"/>
			<param name="tokenValue" value="&quot;${WAS_HOME}&quot;"/>
	    </antcall>  
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${WAS_HOME}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js"/>
			<param name="token" value="\"/>
			<param name="tokenValue" value="\\"/>
	    </antcall>			
	    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${WAS_HOME}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js"/>
			<param name="token" value="\\\\"/>
			<param name="tokenValue" value="\\"/>
	    </antcall>	   	          
    </target>
        
    <target name="deleteStartMenuShortCut"
            description="Deletes Start Menu shortcuts for profile created"
            if="configWindows" unless="disableWASDesktopIntegration"> 
            <property environment="env"/>            
			<property name="VAR" value="${env.SYSTEMROOT}"/>
			<available file="${env.SystemRoot}${separatorFile}system32${separatorFile}CScript.exe" property="VAR" value="${env.SystemRoot}"/>
            <wscmtlog>Executing ${VAR}${separatorFile}system32${separatorFile}CScript ${WAS_HOME}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js</wscmtlog>            
            <exec executable="${VAR}${separatorFile}system32${separatorFile}CScript.exe">                  
                  <arg value="${WAS_HOME}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js"/>
            </exec>
    </target>

    <target name="cleanupJScript"
            if="configWindows">                       
            
            <delete file="${WAS_HOME}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js"/>
    </target>

	<!--******************************************************-->
    <target name="processLinuxScriptForDeletion"
            if="configLinux">                       
	    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}removeAProfile.sh"/>
			<param name="token" value="${WASROOT}"/>
			<param name="tokenValue" value="&quot;${WAS_HOME}&quot;"/>
	    </antcall>
	    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}removeAProfile.sh"/>
			<param name="token" value="${INPUTPROFILENAME}"/>
			<param name="tokenValue" value="${profileName}"/>
	    </antcall>
	    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}removeAProfile.sh"/>
			<param name="token" value="${INPUTPROFILEPATH}"/>
			<param name="tokenValue" value="${profilePath}"/>
	    </antcall>
    </target>
        
    <target name="deleteStartMenuShortCutForLinux"
            description="Deletes Start Menu shortcuts for profile created"
            if="configLinux" unless="disableWASDesktopIntegration"> 
			<mkdir dir="${WS_CMT_LOG_HOME}/${profileName}"/>
            <wscmtlog>Executing /bin/sh ${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}removeAProfile.sh</wscmtlog>
            <wscmtlog>The output will be recorded in: ${WS_CMT_LOG_HOME}/${profileName}/deleteShortcutForProfile.log</wscmtlog>
            <exec executable="/bin/sh"
            	output="${WS_CMT_LOG_HOME}/${profileName}/deleteShortcutForProfile.log">
                  <arg value="${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}removeAProfile.sh"/>
            </exec>
			<antcall target="importLogsToMain" >
				<param name="logfile" value="${WS_CMT_LOG_HOME}/${profileName}/deleteShortcutForProfile.log" />
				<param name="description" value="Deleting Shortcut for Profile" />
				<param name="precondition" value="foefalse" />
			</antcall>
            <delete dir="${profilePath}${separatorFile}properties${separatorFile}linuxMenu"/>
            <delete dir="${profilePath}${separatorFile}properties${separatorFile}script"/>
    </target>
	<!--******************************************************-->
	                      
    <target name="deleteProfileShortCutFromStartMenu"
        description="Set firststeps batch and shell scripts to account for server installed"
        depends="detectCurrentOSFamily,
            setOSFileSeparator,
            defineOSSpecificConfigFlag,
            defineDisableWASDesktopIntegrationFlag,
            processJScriptForDeletion,
            deleteStartMenuShortCut,
            cleanupJScript,
            processLinuxScriptForDeletion,
            deleteStartMenuShortCutForLinux"/>
</project>
