<!DOCTYPE project [
    <!ENTITY importExternalLogs SYSTEM "file:./scripts/importExternalLogs.xml">
]>
<project name="createProfileShortCut2StartMenuManaged" default="createProfileShortCut2StartMenuManaged" 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="windows">
            <os family="windows"/>
        </condition>

        <condition property="currentOSFamily" value="linux">
            <equals arg1="${os.name}" arg2="Linux"/>
        </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="configLinux" value="true">
            <equals arg1="${currentOSFamily}" arg2="linux"/>
        </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="linux"/>
        </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="copyJScripts"
            if="configWindows">
            <copy todir="${profilePath}${separatorFile}temp">
                 <fileset dir="${WAS_HOME}${separatorFile}properties${separatorFile}version${separatorFile}nif${separatorFile}config${separatorFile}script">
    	              <include name="*.*"/>
    	         </fileset>
            </copy>
            <wscmtlog>Copying JScripts to: ${profilePath}${separatorFile}temp</wscmtlog>
    </target>
        
    <target name="replaceTokensForJScriptWindows"
            description="Replaces tokens for profileShortCut2StartMenu.js"
            if="configWindows">
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}temp${separatorFile}profileShortCut2StartMenuManaged.js"/>
			<param name="token" value="${WASROOT}"/>
			<param name="tokenValue" value="&quot;${WAS_HOME}&quot;"/>
	    </antcall>			
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}temp${separatorFile}profileShortCut2StartMenuManaged.js"/>
			<param name="token" value="${PROFILEROOT}"/>
			<param name="tokenValue" value="&quot;${profilePath}&quot;"/>
	    </antcall>						
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}temp${separatorFile}profileShortCut2StartMenuManaged.js"/>
			<param name="token" value="${PROFILENAME}"/>
			<param name="tokenValue" value="${profileName}"/>
	    </antcall>			
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}temp${separatorFile}profileShortCut2StartMenuManaged.js"/>
			<param name="token" value="\"/>
			<param name="tokenValue" value="\\"/>
	    </antcall>			
	    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}temp${separatorFile}profileShortCut2StartMenuManaged.js"/>
			<param name="token" value="\\\\"/>
			<param name="tokenValue" value="\\"/>
	    </antcall>
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}temp${separatorFile}profileShortCutStartMenuDelete.js"/>
			<param name="token" value="${PROFILENAME}"/>
			<param name="tokenValue" value="${profileName}"/>
	    </antcall>		    			
    </target>

    <target name="resolveResourceBundleString" 
        if="configWindows"
        description="Resolve resourcebundle strings in profileShortCut2StartMenuManaged.js">
        
        <property name="BUNDLE_NAME" value="com.ibm.ws.install.configmanager.resourcebundle.ConfigManagerResourceBundle"/>
        
        <resourcebundle resourceBundleName="${BUNDLE_NAME}" resourceBundleKey="Program.firststeps" param="" property="firststeps"/>
        <resourcebundle resourceBundleName="${BUNDLE_NAME}" resourceBundleKey="Program.profiles" param="" property="profilesDir"/>
      
        <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
		<param name="file" value="${profilePath}${separatorFile}temp${separatorFile}profileShortCut2StartMenuManaged.js"/>
		<param name="token" value="${FIRSTSTEPS}"/>
		<param name="tokenValue" value="${firststeps}"/>
	</antcall> 
        <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
		<param name="file" value="${profilePath}${separatorFile}temp${separatorFile}profileShortCut2StartMenuManaged.js"/>
		<param name="token" value="${PROFILESDIR}"/>
		<param name="tokenValue" value="${profilesDir}"/>
	</antcall> 
    </target>
        
    <target name="createStartMenuShortCut"
            description="Creates 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 ${profilePath}${separatorFile}temp${separatorFile}profileShortCut2StartMenuManaged.js</wscmtlog>
            <wscmtlog>The output will be recorded in: ${WS_CMT_LOG_HOME}/${profileName}/createShortcutForProfile.log</wscmtlog>
  	    <!-- fail on error was true, but changed to false to accomodate F69142-65189 -->   
            <exec executable="${VAR}${separatorFile}system32${separatorFile}CScript.exe"
                  output="${WS_CMT_LOG_HOME}/${profileName}/createShortcutForProfile.log"
                  failonerror="false"
				  resultproperty="rcProperty">               
                  <arg value="${profilePath}${separatorFile}temp${separatorFile}profileShortCut2StartMenuManaged.js"/>
            	<arg value="//E:JScript"/>
            </exec>
			<antcall target="importLogsToMain" >
				<param name="logfile" value="${WS_CMT_LOG_HOME}/${profileName}/createShortcutForProfile.log" />
				<param name="description" value="Creating Shortcut for Profile" />
				<param name="precondition" value="foetrue" />
			</antcall>
    </target>
                      
	<!--**************************************************-->
	<target name="chmodScripts"
		description="allows 2 shell scripts to be executable"
		if="configLinux">
			<chmod file="${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}updateProfiles.menu.sh" perm="ug+x"/>
			<chmod file="${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}removeAProfile.sh" perm="ug+x"/>
	</target>
    <target name="detectLinuxScriptExists">
		<property name="baseDirectory" value="${WAS_HOME}${separatorFile}properties${separatorFile}version${separatorFile}nif${separatorFile}config"/>
        <property name="linuxscript" value="${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}updateProfiles.menu.sh"/>
    	<wscmtlog>linuxscript : ${linuxscript}</wscmtlog>     
    	
    	<condition property="linuxscriptexists">
		<available file="${linuxscript}"/>
    	</condition>    	
    </target>
	
    <target name="copyLinuxScripts"
            if="configLinux">
            <copy todir="${profilePath}${separatorFile}properties${separatorFile}script">
                 <fileset dir="${baseDirectory}${separatorFile}script">
                         <include name="*.sh"/>
                 </fileset>
            </copy>
            <wscmtlog>Copying Linux Menu Files to: ${profilePath}${separatorFile}properties</wscmtlog>
            <copy todir="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications">
                 <fileset dir="${baseDirectory}${separatorFile}linuxMenu${separatorFile}applications">
                         <include name="FirstSteps.desktop"/>
                 </fileset>
            </copy>
            <copy todir="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}directories">
                 <fileset dir="${baseDirectory}${separatorFile}linuxMenu${separatorFile}directories">
                         <include name="*.directory"/>
                 </fileset>
            </copy>
            <copy todir="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}menus">
                 <fileset dir="${baseDirectory}${separatorFile}linuxMenu${separatorFile}menus">
                         <include name="*.menu"/>
                 </fileset>
            </copy>
    </target>
    <target name="loadPortsDef"
    	if="configLinux">
    		<property file="${profilePath}/properties/portdef.props"/>
    </target>
    <target name="replaceTokensForLinuxScripts"
            description="Replaces tokens for linux menu files"
            if="configLinux">
			<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
				<param name="dir" value="${profilePath}${separatorFile}properties${separatorFile}script"/>
				<param name="token" value="${WASROOT}"/>
				<param name="tokenValue" value="&quot;${WAS_HOME}&quot;"/>
				<param name="fileType" value="sh"/>
		    </antcall>
			<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
				<param name="dir" value="${profilePath}${separatorFile}properties${separatorFile}script"/>
				<param name="token" value="${PROFILEROOT}"/>
				<param name="tokenValue" value="&quot;${profilePath}&quot;"/>
				<param name="fileType" value="sh"/>
		    </antcall>
			<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
				<param name="dir" value="${profilePath}${separatorFile}properties${separatorFile}script"/>
				<param name="token" value="${INPUTPROFILENAME}"/>
				<param name="tokenValue" value="${profileName}"/>
				<param name="fileType" value="sh"/>
		    </antcall>
			<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
				<param name="dir" value="${profilePath}${separatorFile}properties${separatorFile}script"/>
				<param name="token" value="${INPUTPROFILEPATH}"/>
				<param name="tokenValue" value="${profilePath}"/>
				<param name="fileType" value="sh"/>
		    </antcall>
			<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
				<param name="dir" value="${profilePath}${separatorFile}properties${separatorFile}script"/>
				<param name="token" value="${CELLNAME}"/>
				<param name="tokenValue" value="${cellName}"/>
				<param name="fileType" value="sh"/>
		    </antcall>


		<!--APPLICATIONS-->
		<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
			<param name="dir" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications"/>
			<param name="token" value="${PROFILENAME}"/>
			<param name="tokenValue" value="${profileName}"/>
			<param name="fileType" value="desktop"/>
	    </antcall>
		<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
			<param name="dir" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications"/>
			<param name="token" value="${PROFILEROOT}"/>
			<param name="tokenValue" value="${profilePath}"/>
			<param name="fileType" value="desktop"/>
	    </antcall>

	    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
	    <param name="dir" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications"/>
	    <param name="token" value="${WASROOT}"/>
	    <param name="tokenValue" value="${WAS_HOME}"/>
	    <param name="fileType" value="desktop"/>
	    </antcall>

	    <!--No AdminConsole for managed profile-->
	    <!--<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications${separatorFile}AdminConsole.desktop"/>
			<param name="token" value="${CONSOLEPORT}"/>
			<param name="tokenValue" value="${WC_adminhost}"/>
	    </antcall>-->
		<!--No samples for managed profile-->
	    <!--<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications${separatorFile}Samples.desktop"/>
			<param name="token" value="${SAMPLESPORT}"/>
			<param name="tokenValue" value="${WC_defaulthost}"/>
	    </antcall>-->
		
		<!--MENUS-->
	    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}menus${separatorFile}ProfileName.menu"/>
			<param name="token" value="${PROFILENAME}"/>
			<param name="tokenValue" value="${profileName}"/>
	    </antcall>
		<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}menus${separatorFile}ProfileName.menu"/>
			<param name="token" value="${PROFILEROOT}"/>
			<param name="tokenValue" value="${profilePath}"/>
	    </antcall>

		<!--DIRECTORIES-->
	    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
			<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}directories${separatorFile}ProfileName.directory"/>
			<param name="token" value="${PROFILENAME}"/>
			<param name="tokenValue" value="${profileName}"/>
	    </antcall>

	    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenDirectory">
	    <param name="dir" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}directories"/>
	    <param name="token" value="${WASROOT}"/>
	    <param name="tokenValue" value="${WAS_HOME}"/>
	    <param name="fileType" value="directory"/>
	    </antcall>

		<!--RENAME ProfileName.directory FILES-->
		<move file="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}directories${separatorFile}ProfileName.directory" tofile="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}directories${separatorFile}${profileName}.directory"/>

		<!--RENAME ProfileName.Menu FILES-->
		<move file="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}menus${separatorFile}ProfileName.menu" tofile="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}menus${separatorFile}${profileName}.menu"/>

    </target>

    <target name="resolveResourceBundleStringForLinux" 
        if="configLinux"
        description="Resolve resourcebundle strings in profileShortCut2StartMenuDefault">
        
        <property name="BUNDLE_NAME" value="com.ibm.ws.install.configmanager.resourcebundle.ConfigManagerResourceBundle"/>
        
                <resourcebundle resourceBundleName="${BUNDLE_NAME}" resourceBundleKey="Program.adminconsole" param="" property="adminconsole"/>
        <resourcebundle resourceBundleName="${BUNDLE_NAME}" resourceBundleKey="Program.firststeps" param="" property="firststeps"/>
        <resourcebundle resourceBundleName="${BUNDLE_NAME}" resourceBundleKey="Program.startServer" param="" property="startServer"/>
        <resourcebundle resourceBundleName="${BUNDLE_NAME}" resourceBundleKey="Program.stopServer" param="" property="stopServer"/>
        <resourcebundle resourceBundleName="${BUNDLE_NAME}" resourceBundleKey="Program.nd.serverStart" param="" property="startServerND"/>
        <resourcebundle resourceBundleName="${BUNDLE_NAME}" resourceBundleKey="Program.nd.serverStop" param="" property="stopServerND"/>

    <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
		<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications${separatorFile}FirstSteps.desktop"/>
		<param name="token" value="${FIRSTSTEPS}"/>
		<param name="tokenValue" value="${firststeps}"/>
	</antcall> 
     <!--<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
		<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications${separatorFile}AdminConsole.desktop"/>
		<param name="token" value="${ADMINCONSOLE}"/>
		<param name="tokenValue" value="${adminconsole}"/>
	</antcall> 
	<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
		<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications${separatorFile}StartServerDmgr.desktop"/>
		<param name="token" value="${STARTSERVER}"/>
		<param name="tokenValue" value="${startServerND}"/>
	</antcall>
	<antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
		<param name="file" value="${profilePath}${separatorFile}properties${separatorFile}linuxMenu${separatorFile}applications${separatorFile}StopServerDmgr.desktop"/>
		<param name="token" value="${STOPSERVER}"/>
		<param name="tokenValue" value="${stopServerND}"/>
	</antcall>-->
    </target>
    <target name="createStartMenuShortCutForLinux"
            description="Creates Start Menu shortcuts for profile created"
            if="configLinux" unless="disableWASDesktopIntegration"> 
            <property environment="env"/>
			<mkdir dir="${WS_CMT_LOG_HOME}/${profileName}"/>
            <wscmtlog>Executing /bin/sh ${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}updateProfiles.menu.sh</wscmtlog>
            <wscmtlog>The output will be recorded in: ${WS_CMT_LOG_HOME}/${profileName}/createShortcutForProfile.log</wscmtlog>
            <exec executable="/bin/sh"
            	output="${WS_CMT_LOG_HOME}/${profileName}/createShortcutForProfile.log">
            	<arg value="${profilePath}${separatorFile}properties${separatorFile}script${separatorFile}updateProfiles.menu.sh"/>
            </exec>
			<antcall target="importLogsToMain" >
				<param name="logfile" value="${WS_CMT_LOG_HOME}/${profileName}/createShortcutForProfile.log" />
				<param name="description" value="Creating Shortcut for Profile" />
				<param name="precondition" value="foefalse" />
			</antcall>
            <exec executable="killall">
            	<arg value="gnome-panel"/>
            </exec>
    </target>
    <!--********************************************************-->
    <target name="createProfileShortCut2StartMenuManaged"
        description="Set firststeps batch and shell scripts to account for server installed"
        depends="detectCurrentOSFamily,
            setOSFileSeparator,
            defineOSSpecificConfigFlag,
            defineDisableWASDesktopIntegrationFlag,
            copyJScripts,
            replaceTokensForJScriptWindows,
            resolveResourceBundleString,
            createStartMenuShortCut,
            detectLinuxScriptExists,
            copyLinuxScripts,
            loadPortsDef,
            replaceTokensForLinuxScripts,
            resolveResourceBundleStringForLinux,
            createStartMenuShortCutForLinux,
            chmodScripts"/>
</project>
