<project name="Update HV dmgr Elasticity Policies on WAS 8.5" default="install">

  <target name="install" depends="init" if="isWASHV" >
	<antcall target="listProfiles"></antcall>      
  </target>
  
  <target name="uninstall" depends="init" if="isWASHV" >
	<!--antcall target="listProfiles"></antcall-->
  </target>
  
  
  <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"/>

  <!-- Document startup -->
  <target name="init">
    <tstamp/>
    <wscmtlog>Updating HV dmgr Elasticity Policies</wscmtlog>
    <wscmtlog>${TODAY} - ${TSTAMP}</wscmtlog>
    <wscmtlog>WAS_HOME = ${WAS_HOME}</wscmtlog>
    
    <available file="${WAS_HOME}/properties/version/WASHV.product" type="file" property="isWASHV"/>
    <wscmtlog message="isWASHV=${isWASHV}"/>   
    
    <loadproperties srcFile="${WAS_HOME}/properties/wasprofile.properties"/> 
    <property name="profileRegistry" value="${WS_PROFILE_REGISTRY}" />    
    <wscmtlog>profileRegistry=${WS_PROFILE_REGISTRY}</wscmtlog>
    <wscmtlog message="WS_PROFILE_REGISTRY=${profileRegistry}"/>

    <available file="${profileRegistry}" type="file" property="isProfileRegistryExist"/>
    <wscmtlog message="isProfileRegistryExist=${isProfileRegistryExist}"/>
    
  </target>
  
  <target name="detectCurrentOSFamily"
                description="Detecs the current OS family (UNIX or Windows)">

            <condition property="currentOSFamily" value="windows">
                <os family="windows"/>
            </condition>

            <condition property="cmdescape" value="&quot;">
                <os family="windows"/>
            </condition>

            <condition property="currentOSFamily" value="unix">
                <os family="unix"/>
            </condition>

            <condition property="cmdescape" value="">
                <os family="unix"/>
            </condition>

            <wscmtlog>Detected current OS family to be: ${currentOSFamily}</wscmtlog>
        </target>

        <target name="convertWASHomeToUNIXStylePath">
            <path id="id.was.home">
                <pathelement location="${WAS_HOME}"/>
            </path>

            <pathconvert targetos="unix"
                property="WAS_HOME_UNIX_STYLE"
                refid="id.was.home"/>
        </target>

        <target name="setScriptExtension"
                description="Sets the current script  for this platform (file.separator is unreliable on ANT 1.5.4)">

            <condition property="scriptExtension" value="sh">
                <equals arg1="${currentOSFamily}" arg2="unix"/>
            </condition>

            <condition property="scriptExtension" value="bat">
                <equals arg1="${currentOSFamily}" arg2="windows"/>
            </condition>
        </target>


   <target name="resolveMANAGEPROFILESExecutableForTheCurrentPlatform"
                description="Sets MANAGEPROFILES_EXECUTABLE to the WAS Java interpretor for the current platform">

            <condition property="MANAGEPROFILES_EXECUTABLE" value="${WAS_HOME}/bin/manageprofiles.sh">
                <equals arg1="${currentOSFamily}" arg2="unix"/>
            </condition>

            <condition property="MANAGEPROFILES_EXECUTABLE" value="${WAS_HOME}/bin/manageprofiles.bat">
                <equals arg1="${currentOSFamily}" arg2="windows"/>
            </condition>

            <wscmtlog>wsadmin interpretor set to: ${MANAGEPROFILES_EXECUTABLE}</wscmtlog>

        </target>

        <target name="resolveWSADMINExecutableForTheCurrentPlatform"
                description="Sets WSADMIN_EXECUTABLE to the WAS Java interpretor for the current platform">

            <condition property="WSADMIN_EXECUTABLE" value="${profilePath}/bin/wsadmin.sh">
                <equals arg1="${currentOSFamily}" arg2="unix"/>
            </condition>

            <condition property="WSADMIN_EXECUTABLE" value="${profilePath}/bin/wsadmin.bat">
                <equals arg1="${currentOSFamily}" arg2="windows"/>
            </condition>

            <wscmtlog>wsadmin interpretor set to: ${WSADMIN_EXECUTABLE}</wscmtlog>
        </target>
        
    <target name="getProfiles" description="get the Profiles currently on the box" if="isProfileRegistryExist">
           <wscmtlog message = "getProfiles" />
           <xmlproperty file="${profileRegistry}" validate="false" collapseAttributes="true"/>
           <wscmtlog message="profile names =${profiles.profile.name}"/>
     	   <condition property="profileExist">
              <not>
                 <contains string="${profiles.profile.name}" substring="profiles.profile.name" />
              </not>
           </condition> 	
           <wscmtlog message="profile exists --> ${profileExist}" />
    </target>

    <target name="listProfiles" depends="getProfiles" description="list the Profiles currently on the box" if="profileExist">

           <wscmtlog message = "listProfiles" />
           <wscmtlog message="profile name found=${profiles.profile.name}"/>     	 	
           <foreach target="getProfilePath" list="${profiles.profile.name}" propertyName="profileName"/>

    </target>

    <target name="getProfilePath" description="get the Profiles path"
            depends="detectCurrentOSFamily,
            convertWASHomeToUNIXStylePath,
            resolveMANAGEPROFILESExecutableForTheCurrentPlatform">
            
        <wscmtlog message = "get the ProfilePath" />
        <exec executable="${MANAGEPROFILES_EXECUTABLE}"
                 failonerror="false"
                 output="${WAS_HOME}/logs/ProfilesPath.log">
                 <arg value="-getPath"/>
                 <arg value="-profileName"/>
                 <arg value="${profileName}"/>
        </exec>
        <loadfile
                 property="allProfilePath"
                 srcFile="${WAS_HOME}/logs/ProfilesPath.log"
                 failonerror="false">
        </loadfile>

        <wscmtlog message = "${allProfilePath}" />
        <foreach target="checkProfileType"
                list="${allProfilePath}"
                propertyName="profilePath">
        </foreach>

    </target>

    <target name="checkProfileType" depends="checkProfileType.check" if="isDmgr">
        <wscmtlog message = "checkProfileAugment: profilePath = ${profilePath}" />
        <antcall target="main"></antcall>  
    </target>

    <target name="checkProfileType.check" description="Check to see if profile is a custom profile">
        <wscmtlog message ="checkProfileType.check" />    
        <loadfile property="profileVersionString" srcfile="${profilePath}/properties/version/profile.version"/>
        <condition property="isDmgr">
            <or>
                <contains string="${profileVersionString}" substring="&lt;id&gt;management"/>
                <contains string="${profileVersionString}" substring="&lt;id&gt;dmgr"/>              
            </or>
        </condition>               
        
        <wscmtlog>Detected isDmgr is = ${isDmgr}</wscmtlog>  
    </target>

    <target name="main" depends="resolveWSADMINExecutableForTheCurrentPlatform">
        <wscmtlog>**** In main **** </wscmtlog>   
  	<antcall target="updateElasticityPoliciesHV"></antcall>       
        <wscmtlog> complete </wscmtlog>
    </target>

    <target name="updateElasticityPoliciesHV"  description="Updates the Elasticity Policies of HV dmgr." >
        <wscmtlog message = "updateElasticityPoliciesHV" />
        <wscmtlog message = "${profilePath}/logs/updateElasticityPoliciesHV.log" />
        <exec executable="${WSADMIN_EXECUTABLE}" dir="scripts" output="${profilePath}/logs/updateElasticityPoliciesHV.log">
            <arg value="-conntype"/>
            <arg value="NONE"/>
            <arg value="-lang"/>
            <arg value="jython"/>
            <arg value="-f"/>
            <arg value="updateElasticityPoliciesHV.py"/> 
        </exec>
    </target>

</project>

