<project name="bootstrapPluginsApacheUnInstall" 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}uninstallApachePlugin.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 Apache Webserver Plugin ${timestamp}"/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="************************************************************"/>
        </antcall>        
       
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="APACHE_CONF = ${APACHE_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)">
        
        <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="${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>

    </target>


    <!--
    ***************************************************************************
    TARGET: checkConfigFiles
    Check existence of webserver config files
    ***************************************************************************
    -->      
    <target name="checkConfigFiles">
       <condition property="configFile">
          <and>
             <available file="${APACHE_CONF}"/>
          </and>
        </condition>
        
        <condition property="logMsg" value="Located config file ${APACHE_CONF}">
           <istrue value="${configFile}"/>
        </condition>
        
        <condition property="logMsg" value="Could not locate config file ${APACHE_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>
    
    <!--
    ***************************************************************************
    TARGET: removeIHSConfigurationFileLoadModuleEntry
    Comment out LoadModule entry from httpd.conf file.  Comment out existing 
    WAS plugin LoadModule definitions
    ***************************************************************************
    -->      
    <target name="removeApacheConfigurationFileLoadModuleEntry" if="configFile"
            description="Add LoadModule entry to httpd.conf file">

        <replaceregexp file="${APACHE_CONF}" match="(LoadModule.was_ap20_module.*)"
                       flags="is"
                       byline="true"                                                                     
                       replace="#\1">                                          
        </replaceregexp>                    
        <antcall target="logBoth">
           <param name="logStatement" value="Commenting out LoadModule entries"/>
        </antcall>
        
        <replaceregexp file="${APACHE_CONF}" match="(WebSpherePluginConfig.*)"
                       byline="true"
                       replace="#\1">                   
        </replaceregexp>                    
        
        <antcall target="logBoth">
           <param name="logStatement" value="Commenting out WebSpherePluginConfig entries"/>
        </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=ApacheConfFile"/>
        <echo message="    required - the full path name of the Apache web server"/>
        <echo message="      httpd.conf file"/>
      	<echo message="-DWEB_SERVER_TYPE=Apache"/>
        <echo message="    required - the full path name of the plugin-cfg.xml file"/>     
                                                            
    </target>                                                       
    
    <!--
    ***************************************************************************
    TARGET: bootstrapPluginsApacheUnInstall
    This is the main target
    ***************************************************************************
    -->      
    <target name="bootstrapPluginsApacheUnInstall"
        description="Bootstrap Apache Plugin"
        depends="detectCurrentOSFamily,  
                 setOSFileSeparator,
                 logStartupProperties,
                 checkConfigFiles,
                 removeApacheConfigurationFileLoadModuleEntry">  
                 
        <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 ApacheTarget to 
    ConfigureApachePlugin. Otherwise set ApacheTarget to "Usage" so the usage 
    information can be displayed
    ***************************************************************************
    -->
    <target name="validateAntCall"
        description="Bootstrap Apache Plugin">
        
        <condition property="ValidOptions">                   
           <and>                                            
              <isset property="APACHE_CONF"/>               
              <isset property="OS_NAME"/>
              <isset property="PLUGIN_HOME"/>        
              <isset property="WEBSERVER_TYPE"/>
              <not>
                 <equals arg1="${APACHE_CONF}" arg2=" " trim="true"/>               
              </not>
              <equals arg1="${WEBSERVER_TYPE}" arg2="APACHE" trim="true"/>              
           </and>                                           
        </condition>
	
        <condition property="ApacheTarget" value="bootstrapPluginsApacheUnInstall">
           <istrue value="${ValidOptions}"/>
        </condition>
        
        <condition property="ApacheTarget" value="Usage">
              <isfalse value="${ValidOptions}"/>
        </condition>
        
        <antcall target="${ApacheTarget}"/>  
        
    </target>

</project>
