<project name="bootstrapPluginsIHSUnInstall" default="validateAntCall" basedir=".">

   <!--
# IBM Confidential OCO Source Material
# 5639-D57, 5630-A36, 5630-A37, 5724-D18 (C) COPYRIGHT International Business Machines Corp. 1997, 2009
# The source code for this program is not published or otherwise divested
# of its trade secrets, irrespective of what has been deposited with the
# U.S. Copyright Office.
   -->
   
   <!-- 
   ***************************************************************************
   TARGET: updateLogFile
   Log statement to webserver uninstall log
   ***************************************************************************
   -->      
<target name="updateLogFile"
        description="Update webserver specific logfile">
<echo file="${install.log}" append="true">${stringToAppend}${CRLF}</echo>
</target>
   
   <!-- 
   ***************************************************************************
   TARGET: updateLogFile
   Log statement to webserver uninstall log and stdout
   ***************************************************************************
   -->      
    <target name="logBoth">
       <echo>${logStatement}</echo>
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="${logStatement}"/>
        </antcall>            
    </target>    

   <!-- 
   ***************************************************************************
   TARGET: logStartupProperties
   Log startup properties to the uninstall log
   ***************************************************************************
   -->      
    <target name="logStartupProperties"
            description="Log properties passed to script">
        <property name="install.log" value="${PLUGIN_HOME}${separatorFile}logs${separatorFile}unconfig${separatorFile}uninstallIHSPlugin.log"/> 
        <mkdir dir="${PLUGIN_HOME}${separatorFile}logs${separatorFile}unconfig"/>
        <touch file="${install.log}"/>
        <tstamp>
           <format property="timestamp" pattern="MM/dd/yyyy hh:mm aa"/>
        </tstamp>
        <antcall target="updateLogFile">
           <param name="stringToAppend" value=" "/>
        </antcall>        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="************************************************************"/>
        </antcall>        
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="** Uninstall IHS Webserver Plugin ${timestamp}"/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="************************************************************"/>
        </antcall>        
       
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="IHS_CONF = ${IHS_CONF} "/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="OS_NAME = ${OS_NAME}"/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="OS_NAME = ${WEBSERVER_TYPE}"/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="OS_NAME = ${PLUGIN_HOME}"/>
        </antcall>     
                
    </target>    
    
    <!--
    ***************************************************************************
    TARGET: detectCurrentOSFamily
    Detect current OS Family
    ***************************************************************************
    -->      
    <target name="detectCurrentOSFamily"
            description="Detecs the current OS family (UNIX or Windows)">

        <!-- z/OS setting must come before the unix setting -->
        <condition property="currentOSFamily" value="ZOS">
            <equals arg1="${OS_NAME}" arg2="ZOS"/>
        </condition>
        
        <condition property="currentOSFamily" value="windows">
            <os family="windows"/>
        </condition>
        
        <condition property="currentOSFamily" value="unix">            
            <os family="unix"/>
        </condition>
        
        <echo>Detected current OS family to be: ${currentOSFamily}</echo>
    </target>
    
    <!--
    ***************************************************************************
    TARGET: setOSFileSeparator
    Set the appropriate file separator for the OS
    ***************************************************************************
    -->      
    <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="${OS_NAME}" arg2="OS/400"/>
        </condition>

        <condition property="separatorFile" value="/">
            <equals arg1="${OS_NAME}" arg2="ZOS"/>
        </condition>

        <condition property="separatorFile" value="/">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>
        
        <condition property="separatorFile" value="\">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
        
        <echo>file separator is ${separatorFile}</echo>
                
        <condition property="CRLF" value="&#13;&#10;">
            <os family="windows"/>
        </condition>
        
        <condition property="CRLF" value="&#10;">
            <os family="unix"/>
        </condition>

        <condition property="CRLF" value="&#10;">
            <equals arg1="${OS_NAME}" arg2="ZOS"/>
        </condition>
    
    </target>

    <!--
    ***************************************************************************
    TARGET: checkConfigFiles
    Check existence of webserver config files
    ***************************************************************************
    -->      
    <target name="checkConfigFiles">
       <condition property="configFile">
          <and>
             <available file="${IHS_CONF}"/>
          </and>
        </condition>
        
        <condition property="logMsg" value="Located config file ${IHS_CONF}">
           <istrue value="${configFile}"/>
        </condition>
        
        <condition property="logMsg" value="Could not locate config file ${IHS_CONF}">
           <isfalse value="${configFile}"/>
        </condition>
        
        <antcall target="logBoth">
           <param name="logStatement" value="${logMsg}"/>
        </antcall>
        
        <condition property="fatalError">
           <isfalse value="${configFile}"/>
        </condition>
        
        <antcall target="terminateOnFailure"/>

    </target>

    <!--
    ***************************************************************************
    TARGET: terminateOnFailure
    ***************************************************************************
    -->      
    <target name="terminateOnFailure" if="fatalError">
       <fail message="Fatal error.  Terminating plugin configuration"/>
    </target>

    <!--
    **************************************************************************
    TARGETs: e2a_for_zos and a2e_for_zos
    Convert ZOS EBCDIC IHS configuration file to ASCII for updating by ant,
    and back for use by IHS.
    Executed only if IHS_CONF_EBCDIC is true.
    **************************************************************************
    -->
    <target name="e2a_for_zos" if="IHS_CONF_EBCDIC">
        <exec executable="${PLUGIN_HOME}${separatorFile}bin${separatorFile}e2a.sh" failonerror="true">
            <arg value="${file}"/>
        </exec>
    </target>

    <target name="a2e_for_zos" if="IHS_CONF_EBCDIC">
        <exec executable="${PLUGIN_HOME}${separatorFile}bin${separatorFile}a2e.sh" failonerror="true">
            <arg value="${file}"/>
        </exec>
    </target>
    
    <!--
    ***************************************************************************
    TARGET: removeIHSConfigurationFileLoadModuleEntry
    Comment out LoadModule entry from httpd.conf file.  Comment out existing 
    WAS plugin LoadModule definitions
    ***************************************************************************
    -->      
    <target name="removeIHSConfigurationFileLoadModuleEntry" if="configFile"
            description="Add LoadModule entry to httpd.conf file">

        <!-- IHS_CONF_EBCDIC will be used by e2a_for_zos and a2e_for_zos -->
        <condition property="IHS_CONF_EBCDIC" value="true">
            <equals arg1="${OS_NAME}" arg2="ZOS"/>
        </condition>

        <!-- For ZOS only, convert IHS config file from EBCDIC to ASCII -->
        <antcall target="e2a_for_zos">
            <param name="file" value="${IHS_CONF}"/>
        </antcall>

        <replaceregexp file="${IHS_CONF}" match="(LoadModule.was_ap22_module.*)"
                       flags="is"
                       byline="true"
                       replace="#\1">                                          
        </replaceregexp>                    
        <antcall target="logBoth">
           <param name="logStatement" value="Commenting out LoadModule entries"/>
        </antcall>
        
        <replaceregexp file="${IHS_CONF}" match="(WebSpherePluginConfig.*)"
                       byline="true"
                       replace="#\1">                   
        </replaceregexp>                    
        <antcall target="logBoth">
           <param name="logStatement" value="Commenting out WebSpherePluginConfig entries"/>
        </antcall>

        <!-- For ZOS only, convert IHS config file back to EBCDIC -->
        <antcall target="a2e_for_zos">
            <param name="file" value="${IHS_CONF}"/>
        </antcall>
                        
    </target>
           
    <!--
    **************************************************************************
    TARGET: Usage
    Displays usage information
    **************************************************************************
    -->
    <target name="Usage" if="CMDLINE_CFG"
            description="Display usage information">       
      	<echo message="Usage: -ant -f ConfigureApache.ant [[option=optionValue]..]"/>
      	<echo message=""/>
      	<echo message="Valid Options"/>
        <echo message="============="/>
      	<echo message="-DPLUGIN_HOME=pluginDirectory"/>
        <echo message="    required - the location of the plugin home directory"/>
        <echo message="      (ie, \usr\IBM\WebSphere\Plugins)"/>
      	<echo message="-DOS_NAME=operatingSystem"/>
        <echo message="    required - the current operating system.  Valid values are:"/>
      	<echo message="      AIX"/>
      	<echo message="      Linux"/>
      	<echo message="      SunOS"/>
      	<echo message="      HP-UX"/>
      	<echo message="      windows"/>
      	<echo message="-DAPACHE_CONF=IHSConfFile"/>
        <echo message="    required - the full path name of the Apache web server"/>
        <echo message="      httpd.conf file"/>
      	<echo message="-DWEB_SERVER_TYPE=IHS"/>
        <echo message="    required - the full path name of the plugin-cfg.xml file"/>     
                                                            
    </target>                                                       
            
    <!--
    ***************************************************************************
    TARGET: bootstrapPluginsIHSUnInstall
    This is the main target
    ***************************************************************************
    -->      
    <target name="bootstrapPluginsIHSUnInstall"
        description="Bootstrap IHS Plugin"
        depends="detectCurrentOSFamily,  
                 setOSFileSeparator,
                 logStartupProperties,
                 checkConfigFiles,
                 removeIHSConfigurationFileLoadModuleEntry">   
                 
        <antcall target="logBoth">
           <param name="logStatement" value="Uninstall complete"/>
        </antcall>     
                
    </target>
    
    <!--
    **************************************************************************
    TARGET: validateAntCall
    This is the main target. Check that all required config options are present.         
    If all required config options are present set IHSTarget to 
    ConfigureIHSPlugin. Otherwise set IHSTarget to "Usage" so the usage 
    information can be displayed
    ***************************************************************************
    -->
    <target name="validateAntCall"
        description="Bootstrap IHS Plugin">
        
        <condition property="ValidOptions">                   
           <and>                                            
              <isset property="IHS_CONF"/>               
              <isset property="OS_NAME"/>
              <isset property="PLUGIN_HOME"/>        
              <isset property="WEBSERVER_TYPE"/>
              <not>
                 <equals arg1="${IHS_CONF}" arg2=" " trim="true"/>               
              </not>
              <equals arg1="${WEBSERVER_TYPE}" arg2="IHS" trim="true"/>              
           </and>                                           
        </condition>
	
        <condition property="IHSTarget" value="bootstrapPluginsIHSUnInstall">
           <istrue value="${ValidOptions}"/>
        </condition>
        
        <condition property="IHSTarget" value="Usage">
              <isfalse value="${ValidOptions}"/>
        </condition>
        
        <antcall target="${IHSTarget}"/>  
        
    </target>

</project>
