<?xml version="1.0"?>

<!DOCTYPE project [
    <!ENTITY update.server-component-template.xml SYSTEM "file:./portsUpdate/update.server-component-template.xml">
    <!ENTITY update.serverentry-template.xml SYSTEM "file:./portsUpdate/update.serverentry-template.xml">
    <!ENTITY update.serverindex.xml SYSTEM "file:./portsUpdate/update.serverindex.xml">
    <!ENTITY update.virtualhosts.xml SYSTEM "file:./portsUpdate/update.virtualhosts.xml">
    <!ENTITY update.wsadmin.properties SYSTEM "file:./portsUpdate/update.wsadmin.properties">    
    <!ENTITY update.startmenu.xml SYSTEM "file:./portsUpdate/update.startmenu.xml">
]>

<project name="portsUpdate" default="default" basedir=".">
    <description>
        transforms the port numbers in the necessary config files.
    </description>  
    
    
    &update.server-component-template.xml;
    &update.serverentry-template.xml;
    &update.serverindex.xml;
    &update.virtualhosts.xml;
    &update.wsadmin.properties;    
    &update.startmenu.xml;
    
    <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="detectProductLevel">
       <!-- The existance of the WAS.product file tells us we are on a full WAS Base install.
             Otherwise we are on the Embed product.  EWAS.product should exist instead. -->
       <condition property="isBase">
          <available file="${WAS_HOME}/properties/version/WAS.product"/>
       </condition>
       <condition property="isEmbed">
          <isfalse value="${isBase}"/>
       </condition>
    </target>

    <target name="initializeVariables" 
    	depends="test,
                detectProductLevel,    		
    		initialize.common.variables, 
    		initialize.protdef.props.from.default.portsFile,
    		initialize.protdef.props.from.portsFile, 
    		initialize.protdef.props.from.startingPort,
    		initialize.protdef.props.from.recommendedValues">
    	
      	<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"/>   			
	
 	<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>startingPort=${startingPort}</wscmtlog>
    <wscmtlog>portdefPath=${portdefPath}</wscmtlog>
    <wscmtlog>templatePath=${templatePath}</wscmtlog>
    <wscmtlog>defaultPorts=${defaultPorts}</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="defaultPorts=${defaultPorts}"/>
    <echo message="templatePath=${templatePath}"/>

	<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="callingWithDefaultPortsFile">
		<isset property="defaultPorts"/>
	</condition>
	<echo message="callingWithDefaultPortsFile=${callingWithDefaultPortsFile}"/>
	<wscmtlog>callingWithDefaultPortsFile=${callingWithDefaultPortsFile}</wscmtlog>
	<property name="defaultPortdefFile"    value="${wasHome}/profileTemplates/secureproxy/actions/portsUpdate/portdef.props"/>
	<wscmtlog>defaultPortdefFile=${defaultPortdefFile}</wscmtlog>
    <echo message="defaultPortdefFile=${defaultPortdefFile}"/>
    	
    <condition property="callingWithRecommendedPorts">
		<and>
		<not><isset property="portsFile"/></not>
		<not><isset property="startingPort"/></not>
		<not><isset property="callingWithDefaultPortsFile"/></not>	
		</and>
	</condition>
	<echo message="callingWithRecommendedPorts=${callingWithRecommendedPorts}"/>
	<wscmtlog>callingWithRecommendedPorts=${callingWithRecommendedPorts}</wscmtlog>
		
        <condition property="performPortsUpdate">
                <or>
                <isset property="portsFile"/>
                <isset property="startingPort"/>
                <isset property="callingWithRecommendedPorts"/>
                <isset property="callingWithDefaultPortsFile"/>
                </or>
        </condition>
        <echo message="performPortsUpdate=${performPortsUpdate}"/>
        <wscmtlog>performPortsUpdate=${performPortsUpdate}</wscmtlog>

    </target>
    
    <target name="initialize.protdef.props.from.default.portsFile" if="callingWithDefaultPortsFile">
        <echo message="Copying file ${defaultPortdefFile} to file ${portdefPath}......"/>
        <copy file="${defaultPortdefFile}" tofile="${portdefPath}" overwrite="true"/>
    </target>

    <target name="initialize.protdef.props.from.portsFile" if="callingWithPortsFile" 
    	depends="copyingFromAbosolutePath,copyingFromRelativePath">
    </target>
    <target name="copyingFromAbosolutePath" if="portsFileSpecifiedAsAbsolutePath">
    	<echo message="copyingFromAbosolutePath: Copying file ${portsFile} to file ${portdefPath}......"/>
    	<copy file="${portsFile}" tofile="${portdefPath}" overwrite="true"/>
    </target>       
    <target name="copyingFromRelativePath" if="portsFileSpecifiedAsRelativePath">
    	<echo message="copyFromRelativePath: Copying file ${user.dir}/${portsFile} to file ${portdefPath}......"/>
    	<copy file="${user.dir}/${portsFile}" tofile="${portdefPath}" overwrite="true"/>
    </target>
 
    <target name="initialize.protdef.props.from.startingPort" if="callingWithStartingPort">
    	<wscmtlog>Creating file ${portdefPath} from starting port ${startingPort}</wscmtlog>  
    	<echo message="Creating file ${portdefPath} from starting port ${startingPort}"/>
    	<echo file="${portdefPath}" append="false"></echo>

        <taskdef name="resolvePort"
           classname="com.ibm.ws.profile.anttasks.PortResolver">
        </taskdef>


	<resolvePort 
		minimumPortNumber="${startingPort}" 
		WAS_HOME="${wasHome}"
		portKeys="PROXY_HTTP_ADDRESS PROXY_HTTPS_ADDRESS PROXY_SIP_ADDRESS PROXY_SIPS_ADDRESS IPC_CONNECTOR_ADDRESS BOOTSTRAP_ADDRESS"
		portKeyValuePairs="resolvedKeyValuePairs"
		/>
    	
    	<echo message="${resolvedKeyValuePairs}${line.separator}"/>
    	
    	<echo file="${portdefPath}" append="false">${resolvedKeyValuePairs}${line.separator}</echo>
    	
    </target>
      
    <target name="initialize.protdef.props.from.recommendedValues" if="callingWithRecommendedPorts">
    	<wscmtlog>Creating file ${portdefPath} from recommended ports</wscmtlog>  
    	<echo message="Creating file ${portdefPath} from recommended ports"/>
    	<echo file="${portdefPath}" append="false"></echo>

        <taskdef name="resolvePortsWithRecommendedValues"
			classname="com.ibm.ws.profile.anttasks.PortResolverRecommendedValues">
		</taskdef>
    
    	<resolvePortsWithRecommendedValues 
			WAS_HOME="${wasHome}" 
			templatePath="${templatePath}"
			portKeyValuePairs="resolvedKeyValuePairs"
			/>
    	
    	<echo message="${resolvedKeyValuePairs}${line.separator}"/>
    	
    	<echo file="${portdefPath}" append="false">${resolvedKeyValuePairs}${line.separator}</echo>
    	
    </target>  
    
    <target name="replaceRegExpAllInstancesOfGivenTokenWithGivenValueForTheGivenFile" if="${port}">        
        <wscmtlog>File ${file}:  setting ${port} to ${portValue}</wscmtlog>    
    	<echo message="File ${file}:  setting ${port} to ${portValue}"/>
       	
         <replaceregexp file="${file}"
                         match="${token}"
                         replace="${tokenValue}"
                         encoding="UTF-8"/>

    </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}"
                         encoding="UTF-8"
                         replace="${tokenValue}">
                         <fileset dir="${dir}" includes="**/*.${fileType}"/>
         </replaceregexp>                         
    </target>
        
    <target name="updateFiles" if="performPortsUpdate">
		
        <antcall target="update.serverindex.xml">
			<param name="file" value="${config.home}/cells/${cellName}/nodes/${nodeName}/serverindex.xml"/>
		</antcall>		
		
        <antcall target="update.virtualhosts.xml">
			<param name="file" value="${config.home}/cells/${cellName}/virtualhosts.xml"/>
		</antcall>   	
		
        <antcall target="update.serverindex.xml">
			<param name="file" value="${config.home}/templates/servertypes/PROXY_SERVER/serverindex.xml"/>
		</antcall>  		
				   
        <!-- workaround for windows as the wsadmin listener locks this file -->
        <attrib file="${profilePath}/properties/wsadmin.properties" readonly="false" />
        <copy file="${profilePath}/properties/wsadmin.properties" tofile="${profilePath}/temp/wsadmin.properties"/>
        <antcall target="update.wsadmin.properties">
             <param name="file" value="${profilePath}/temp/wsadmin.properties"/>
        </antcall>
        <copy file="${profilePath}/temp/wsadmin.properties" tofile="${profilePath}/properties/wsadmin.properties"/>
        <delete file="${profilePath}/temp/wsadmin.properties"/>
		  	
		<!--dir paremeter is set within this target-->
        <antcall target="update.startmenu.xml">
		</antcall>  				   
    </target>
   
</project>
