<project name="bootstrapPluginsSunOneUnInstall" 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}uninstallSunOnePlugin.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 SunOne Webserver Plugin ${timestamp}"/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="************************************************************"/>
        </antcall>        
       
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="SUNONE_MAGNUS_CONF = ${SUNONE_MAGNUS_CONF} "/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="SUNONE_OBJ_CONF = ${SUNONE_OBJ_CONF} "/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="OS_NAME = ${OS_NAME}"/>
        </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="${SUNONE_MAGNUS_CONF}"/>
             <available file="${SUNONE_OBJ_CONF}"/>
          </and>
        </condition>
        <condition property="logMsg" value="Located config files ${SUNONE_MAGNUS_CONF} and ${SUNONE_OBJ_CONF}">
           <istrue value="${configFile}"/>
        </condition>
        
        <condition property="logMsg" value="Could not locate config files ${SUNONE_MAGNUS_CONF} and ${SUNONE_OBJ_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: removeSunOneConfigurationFileLoadModuleEntry
    Comment out Init and bootstrap statements from magnus.conf
    ***************************************************************************
    -->      
    <target name="removeSunOneConfigurationFileLoadModuleEntry" if="configFile"
            description="Add LoadModule entry to magnus.conf file">

        <replaceregexp file="${SUNONE_MAGNUS_CONF}" match="(Init.fn=&quot;load-modules&quot;.funcs=&quot;as_init,as_handler,as_term&quot;.*)"
                       flags="is"
                       byline="true"
                       replace="#\1">                                          
        </replaceregexp>                    
        <antcall target="logBoth">
           <param name="logStatement" value="Commenting out LoadModule entries"/>
        </antcall>
        
        <replaceregexp file="${SUNONE_MAGNUS_CONF}" match="(Init.fn=&quot;as_init&quot;.bootstrap.properties.*)"
                       flags="is"
                       byline="true"
                       replace="#\1">                   
        </replaceregexp>                    
        <antcall target="logBoth">
           <param name="logStatement" value="Commenting out Bootstrap entries"/>
        </antcall>
                        
    </target>
    
    <!--
    ***************************************************************************
    TARGET: removeSunOneConfigurationFileHandlerEntry
    Comment out service and Addlog entries from obj.conf file
    ***************************************************************************
    -->      
    <target name="removeSunOneConfigurationFileHandlerEntry" if="configFile"
            description="Add service and AddLog entry to obj.conf file">
        <replaceregexp file="${SUNONE_OBJ_CONF}" match="(service.fn=&quot;as_handler&quot;.*)"
                       flags="is"
                       byline="true"
                       replace="#\1">                   
        </replaceregexp>                    
        <antcall target="logBoth">
           <param name="logStatement" value="Commenting out fn=&quot;as_handler&quot; entries"/>
        </antcall>
        
        <replaceregexp file="${SUNONE_OBJ_CONF}" match="(AddLog.fn=&quot;as_term&quot;.*)"
                       flags="is"
                       byline="true"
                       replace="#\1">                   
        </replaceregexp>                    
        <antcall target="logBoth">
           <param name="logStatement" value="Commenting out AddLog fn=&quot;as_term&quot; 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=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: bootstrapPluginsSunOneUnInstall
    This is the main target
    ***************************************************************************
    -->      
    <target name="bootstrapPluginsSunOneUnInstall"
        description="Bootstrap UnInstall SunOne Plugin"
        depends="detectCurrentOSFamily,  
                 setOSFileSeparator,
                 logStartupProperties,
                 checkConfigFiles,
                 removeSunOneConfigurationFileLoadModuleEntry,  
                 removeSunOneConfigurationFileHandlerEntry">        
                
        <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 SunOneTarget to 
    bootstrapPluginsSunOneUnInstall. Otherwise set SunOneTarget to "Usage" so the usage 
    information can be displayed
    ***************************************************************************
    -->
    <target name="validateAntCall"
        description="Bootstrap SunOne Plugin">
        
        <condition property="ValidOptions">                   
           <and>                                            
              <isset property="SUNONE_OBJ_CONF"/>               
              <isset property="SUNONE_MAGNUS_CONF"/>
              <isset property="WAS_PLUGIN_CFG_XML"/> 
              <isset property="OS_NAME"/>
              <isset property="PLUGIN_HOME"/>
              <isset property="PLUGIN_INSTALL_TYPE"/>    
              <isset property="WEBSERVER_TYPE"/>                         
              <not>
                 <equals arg1="${SUNONE_OBJ_CONF}" arg2=" " trim="true"/>               
              </not>
              <not>   
                 <equals arg1="${SUNONE_MAGNUS_CONF}" arg2=" " trim="true"/>
              </not>
              <not>
                 <equals arg1="${WAS_PLUGIN_CFG_XML}" arg2=" " trim="true"/>
              </not>
	            <not>
                 <equals arg1="${INSTALL_ARCH}" arg2=" " trim="true"/>
              </not>                  
              <equals arg1="${WEBSERVER_TYPE}" arg2="SUNJAVASYSTEM" trim="true"/>
           </and>                                           
        </condition>
       	
        <condition property="SunOneTarget" value="bootstrapPluginsSunOneUnInstall">
           <istrue value="${ValidOptions}"/>
        </condition>
        
        <condition property="SunOneTarget" value="Usage">
              <isfalse value="${ValidOptions}"/>
        </condition>
        
        <antcall target="${SunOneTarget}"/>          
    </target>
</project>
