<?xml version="1.0"?>

<!DOCTYPE project [
    <!ENTITY update.serverdebug.xml SYSTEM "file:./portsUpdate/update.serverdebug.xml">
]>

<project name="debugPortsUpdate" default="default" basedir=".">
    <description>
        transforms the port numbers in the necessary config files.
    </description>  
    
    &update.serverdebug.xml;

    <property name="serverName"    value="dmgr"/>

    <target name="test">
    </target>
    
    <target name="default" depends="initializeVariables, updateFiles">
    </target>
    
     <taskdef name="wscmtlog"
        classname="com.ibm.ws.install.configmanager.actionengine.ant.utils.AntTaskToLogToConfigManagersLogFiles">
    </taskdef>
    
    <target name="initializeVariables" 
    	depends="test,    		
    		initialize.common.variables, 
            initialize.debugport.props">
    	
      	<property name="config.home"    value="${profilePath}/config"/>   	
      	<property file="${portdefPath}"/>
    </target>
    
    <target name="initialize.common.variables">
    	
    <property name="wasHome"    value="${WAS_HOME}"/>
	  <property name="profilePath"    value="${WS_CMT_PROFILE_LOCATION}"/>
	  <property name="cellName"    value="${WS_CMT_CELL_NAME}"/>
	  <property name="nodeName"    value="${WS_CMT_NODE_NAME}"/>
	
    <property name="config.home"    value="${profilePath}/config"/>
  	<property name="portdefPath"    value="${profilePath}/properties/portdef.props"/>
  	<property name="templatePortdefPath"    value="${templatePath}/actions/portsUpdate/portdef.props"/>
	
   	<wscmtlog>wasHome=${wasHome}</wscmtlog>
    	<wscmtlog>profilePath=${profilePath}</wscmtlog>
    	<wscmtlog>cellName=${cellName}</wscmtlog>
    	<wscmtlog>nodeName=${nodeName}</wscmtlog>
    	<wscmtlog>startingPort=${startingPort}</wscmtlog>
    	<wscmtlog>portsFile=${portsFile}</wscmtlog>
    	<wscmtlog>portdefPath=${portdefPath}</wscmtlog>
    	<wscmtlog>templatePortdefPath=${templatePortdefPath}</wscmtlog>

  	<echo message="wasHome=${wasHome}"/>
    	<echo message="profilePath=${profilePath}"/>
    	<echo message="cellName=${cellName}"/>
    	<echo message="nodeName=${nodeName}"/>
    	<echo message="startingPort=${startingPort}"/>
    	<echo message="portsFile=${portsFile}"/>
    	<echo message="startingPort=${startingPort}"/>
    	<echo message="portdefPath=${portdefPath}"/>
    	<echo message="templatePortdefPath=${templatePortdefPath}"/>

	<condition property="callingWithPortsFile">
		<isset property="portsFile"/>
	</condition>	
	<echo message="callingWithPortsFile=${callingWithPortsFile}"/>
	<wscmtlog>callingWithPortsFile=${callingWithPortsFile}</wscmtlog>
		
    	
    	<condition property="portsFileSpecifiedAsAbsolutePath">
    		<and>
		<isset property="portsFile"/>
		<available file="${portsFile}"/>
		</and>
	</condition>	
	<echo message="portsFileSpecifiedAsAbsolutePath=${portsFileSpecifiedAsAbsolutePath}"/>
	<wscmtlog>portsFileSpecifiedAsAbsolutePath=${portsFileSpecifiedAsAbsolutePath}</wscmtlog>
	
    	<condition property="portsFileSpecifiedAsRelativePath">
    		<and>
		<isset property="portsFile"/>
		<not><available file="${portsFile}"/></not>
		</and>
	</condition>	
	<echo message="portsFileSpecifiedAsRelativePath=${portsFileSpecifiedAsRelativePath}"/>
	<wscmtlog>portsFileSpecifiedAsRelativePath=${portsFileSpecifiedAsRelativePath}</wscmtlog>
	
	<condition property="callingWithStartingPort">
		<and>
		<not><isset property="portsFile"/></not>
		<isset property="startingPort"/>
		</and>
	</condition>
	<echo message="callingWithStartingPort=${callingWithStartingPort}"/>
	<wscmtlog>callingWithStartingPort=${callingWithStartingPort}</wscmtlog>
	
	<condition property="callingWithoutStartingPort">
		<and>
		<not><isset property="startingPort"/></not>
		</and>
	</condition>
	<echo message="callingWithoutStartingPort=${callingWithoutStartingPort}"/>
	<wscmtlog>callingWithoutStartingPort=${callingWithoutStartingPort}</wscmtlog>
	
	<condition property="callingFromPCT">
		<and>
		<not><isset property="portsFile"/></not>
		<not><isset property="startingPort"/></not>
		<available file="${portdefPath}"/>
		</and>
	</condition>	
	<echo message="callingFromPCT=${callingFromPCT}"/>
	<wscmtlog>callingFromPCT=${callingFromPCT}</wscmtlog>
	
	<condition property="performPortsUpdate">
		<or>
		<isset property="callingFromPCT"/>
		<isset property="portsFile"/>
		<isset property="startingPort"/>
		</or>
	</condition>	
	<echo message="performPortsUpdate=${performPortsUpdate}"/>
	<wscmtlog>performPortsUpdate=${performPortsUpdate}</wscmtlog>
		
	<condition property="callingWithDefaultPortsFile">
		<and>
		<not><isset property="portsFile"/></not>
		<not><isset property="startingPort"/></not>
		<not><available file="${portdefPath}"/></not>
		</and>
	</condition>
	<echo message="callingWithDefaultPortsFile=${callingWithDefaultPortsFile}"/>
	<wscmtlog>callingWithDefaultPortsFile=${callingWithDefaultPortsFile}</wscmtlog>
	<property name="defaultPortdefFile"    value="${wasHome}/profileTemplates/default/actions/portsUpdate/portdef.props"/>
	<wscmtlog>defaultPortdefFile=${defaultPortdefFile}</wscmtlog>
    	<echo message="defaultPortdefFile=${defaultPortdefFile}"/>
    	
    	
    </target>
    
    <target name="initialize.debugport.props" if="callingWithoutStartingPort">
    	<wscmtlog>Updating debug port: portdefPath=${portdefPath}, templatePortdefPath=${templatePortdefPath}</wscmtlog>  
    	<echo message="Updating debug port: portdefPath=${portdefPath}, templatePortdefPath=${templatePortdefPath}"/>

      <taskdef name="resolveDebugPort"
         classname="com.ibm.ws.profile.anttasks.DebugPortResolver">
      </taskdef>

	<resolveDebugPort 
		referencePortName="WC_adminhost_secure"
            portdefPath="${portdefPath}"
            debugPortKey="resolvedDebugPortNum"
            templatePortdefPath="${templatePortdefPath}"
		/>
	<wscmtlog>resolvedDebugPort ${resolvedDebugPortNum}"</wscmtlog>
	<echo message="resolvedDebugPort ${resolvedDebugPortNum}"/>
	<condition property="isResolvedDebugPort">
		<and>
		<isset property="resolvedDebugPortNum"/>
		</and>
	</condition>
    </target>
    
    <target name="replaceRegExpAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">        
      <wscmtlog>File ${file}:  setting ${port} to ${resolvedDebugPort}</wscmtlog>    
    	<echo message="File ${file}:  setting ${port} to ${resolvedDebugPortNum}"/>
       	
         <replaceregexp file="${file}"
                         match="${token}"
						 encoding="UTF-8"
                         replace="${tokenValue}"/>

    </target>

    <target name="replaceRegExpAllInstancesOfGivenTokenWithGivenValueForTheGivenDir" if="${port}">        
        <wscmtlog>All ${fileType} files in directory ${dir}:  setting ${port} to ${portValue}</wscmtlog>    
    	<echo message="All ${fileType} files in directory ${dir}:  setting ${port} to ${portValue}"/>
       	
         <replaceregexp match="${token}"
                         replace="${tokenValue}">
                         <fileset dir="${dir}" includes="**/*.${fileType}"/>
         </replaceregexp>                         
    </target>
        
    <target name="updateFiles" if="isResolvedDebugPort">
        <wscmtlog>Calling update.serverdebug.xml</wscmtlog>
        <echo message="Calling update.serverdebug.xml"/>
        <antcall target="update.serverdebug.xml">
			<param name="file" value="${config.home}/cells/${cellName}/nodes/${nodeName}/servers/${serverName}/server.xml"/>
        </antcall>		
    </target>

    
</project>
