<project name="bootstrapPluginsDomino7Uninstall" 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}uninstallDomino7Plugin.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 Domino7 Webserver Plugin ${timestamp}"/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="************************************************************"/>
        </antcall>        
       
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="WEBSERVER_TYPE = ${WEBSERVER_TYPE}"/>
        </antcall>     
       
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="WEBSERVER_SELECTED = ${WEBSERVER_SELECTED}"/>
        </antcall>     
       
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="PLUGIN_HOME = ${PLUGIN_HOME}"/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="DOMINO7_NAMES_CONF = ${DOMINO7_NAMES_CONF} "/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="DOMINO7_NOTES_CONF = ${DOMINO7_NOTES_CONF} "/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="DOMINO7_NOTES_USER = ${DOMINO7_NOTES_USER} "/>
        </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="${DOMINO7_NOTES_CONF}"/>
             <available file="${DOMINO7_NAMES_CONF}"/>
              <not>
                 <equals arg1="${DOMINO7_NOTES_CONF}" arg2=" " trim="true"/>               
              </not>
              <not>
                 <equals arg1="${DOMINO7_NAMES_CONF}" arg2=" " trim="true"/>               
              </not>             
          </and>
        </condition>
        <condition property="logMsg" value="Located config files ${DOMINO7_NOTES_CONF} and ${DOMINO7_NAMES_CONF}">
           <istrue value="${configFile}"/>
        </condition>
        
        <condition property="logMsg" value="Could not locate config files ${DOMINO7_NOTES_CONF} and ${DOMINO7_NAMES_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: resolvePLUGINJAVAExecutableForTheCurrentPlatform
    Sets PLUGIN_JAVA_EXECUTABLE to the PLUGIN Java interpretor for the 
    current platform
    **************************************************************************
    -->      
    <target name="resolvePLUGINJAVAExecutableForTheCurrentPlatform"
            description="Sets PLUGIN_JAVA_EXECUTABLE to the PLUGIN Java interpretor for the current platform">
            
        <condition property="PLUGIN_JAVA_EXECUTABLE" value="${PLUGIN_HOME}${separatorFile}java${separatorFile}jre${separatorFile}bin${separatorFile}java">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>
        
        <condition property="PLUGIN_JAVA_EXECUTABLE" value="${PLUGIN_HOME}${separatorFile}java${separatorFile}jre${separatorFile}bin${separatorFile}javaw.exe">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
        
        <antcall target="logBoth">
           <param name="logStatement" value="JAVA interpretor set to: ${PLUGIN_JAVA_EXECUTABLE}"/>
        </antcall>     

    </target>
    
    <!--
    **************************************************************************
    TARGET: resolveClasspathForTheCurrentPlatform
    Set classpath 
    **************************************************************************
    -->      
    <target name="resolveClasspathForTheCurrentPlatform"
            description="Sets DOMINO7_CLASSPATH to the correct classpath for the current platform">
                        
        <condition property="DOMINO7_CLASSPATH" 
                value="${PLUGIN_HOME}${separatorFile}lib${separatorFile}InstallWASdsapiPlugin.jar:${DOMINO7_NOTES_CONF}">
            <and>
               <equals arg1="${currentOSFamily}" arg2="unix"/>
                  <not>
                     <equals arg1="${OS_NAME}" arg2="SunOS"/>
                  </not>
            </and>
        </condition>
        
        <condition property="DOMINO7_CLASSPATH" 
                value="${PLUGIN_HOME}${separatorFile}lib${separatorFile}InstallWASdsapiPlugin.jar:${PLUGIN_HOME}${separatorFile}lib${separatorFile}dominoutil.jar:${DOMINO7_NOTES_CONF}">
            <equals arg1="${OS_NAME}" arg2="SunOS"/>
        </condition>
        
        <condition property="DOMINO7_CLASSPATH" 
                value="${PLUGIN_HOME}${separatorFile}lib${separatorFile}InstallWASdsapiPlugin.jar;${DOMINO7_NOTES_CONF}">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>

    </target>

    <!--
    **************************************************************************
    TARGET: appendToGivenFileGivenString
    Do NOT indent these line. The entries in the httpd.conf file are not tabbed
    **************************************************************************
    -->  
    <target name="appendToGivenFileGivenString">
<echo file="${file}" append="true">${stringToAppend}${CRLF}</echo>
    </target>

    <!--
    **************************************************************************
    TARGET: unInstallWASdsapiPluginWindows
    Uninstalls domino plugin on Windows
    **************************************************************************
    -->      
    <target name="unInstallWASdsapiPluginWindows" if="configFile">
        
        <antcall target="logBoth">
           <param name="logStatement" value="Run ${PLUGIN_JAVA_EXECUTABLE} -classpath ${DOMINO7_CLASSPATH} InstallWASdsapiPlugin -d ${DOMINO7_NAMES_CONF} -l ${PLUGIN_HOME}${separatorFile}logs${separatorFile}unconfig${separatorFile}uninstallDomino7Plugin.log -v remove -version v6"/>
        </antcall>
        
        <antcall target="logBoth">
           <param name="logStatement" value="The results will be logged to ${PLUGIN_HOME}${separatorFile}logs${separatorFile}unconfig${separatorFile}uninstallDomino7Plugin.log"/>
        </antcall> 
        
 
        <dirname property="DOMINO7_NOTES_PATH" file="${DOMINO7_NOTES_CONF}"/>
        
        <replaceregexp file="${DOMINO7_NOTES_PATH}${separatorFile}notes.ini" match="(WebSpherePluginCfg=.*)"
                       byline="true"
                       replace=" ">                                          
        </replaceregexp>                    
            
        <exec executable="${PLUGIN_JAVA_EXECUTABLE}">
            <arg value="-classpath"/>
            <arg value="&quot;${DOMINO7_CLASSPATH}&quot;"/>
            <arg value="InstallWASdsapiPlugin"/>
            <arg value="-d"/>
            <arg value="&quot;${DOMINO7_NAMES_CONF}&quot;"/>
            <arg value="-l"/>
            <arg value="&quot;${PLUGIN_HOME}${separatorFile}logs${separatorFile}unconfig${separatorFile}uninstallDomino7Plugin.log&quot;"/>
            <arg value="-v"/>
            <arg value="remove"/>
            <arg value="-version"/>
            <arg value="v6"/>
            <env key="PATH" path="${DOMINO7_NOTES_PATH}"/>
            <env key="LIBPATH" path="${DOMINO7_NOTES_PATH}"/>
        </exec>


    </target>

    <!--
    **************************************************************************
    TARGET: InstallWASdsapiPluginUnix
    Uninstalls domino plugin on Unix
    **************************************************************************
    -->      
    <target name="unInstallWASdsapiPluginUnix" if="configFile">
        
        <antcall target="logBoth">
           <param name="logStatement" value="Run ${PLUGIN_JAVA_EXECUTABLE} -classpath ${DOMINO7_CLASSPATH} InstallWASdsapiPlugin -d ${DOMINO7_NAMES_CONF} -l ${PLUGIN_HOME}${separatorFile}logs${separatorFile}unconfig${separatorFile}uninstallDomino7Plugin.log -v remove -version v6"/>
        </antcall>
        
        <antcall target="logBoth">
           <param name="logStatement" value="The results will be logged to ${PLUGIN_HOME}${separatorFile}logs${separatorFile}unconfig${separatorFile}uninstallDomino7Plugin.log"/>
        </antcall> 
                
        <dirname property="DOMINO7_NOTES_PATH" file="${DOMINO7_NOTES_CONF}"/>
        <dirname property="DOMINO7_NAMES_PATH" file="${DOMINO7_NAMES_CONF}"/>

        <property name="dominoUnInstall.file" value="${PLUGIN_HOME}${separatorFile}bin${separatorFile}domino7UnInstall.sh"/> 

        <echo file="${dominoUnInstall.file}">
           su ${DOMINO7_NOTES_USER} -c &quot;LIBPATH=${DOMINO7_NOTES_PATH}:${DOMINO7_NAMES_PATH}:$LIBPATH;\
             PATH=${DOMINO7_NOTES_PATH}:${DOMINO7_NAMES_PATH}:$PATH;\
             LD_LIBRARY_PATH=LD_LIBRARY_PATH:${DOMINO7_NOTES_PATH};\
             export PATH LIBPATH LD_LIBRARY_PATH;
             ${PLUGIN_JAVA_EXECUTABLE} \
             -classpath ${DOMINO7_CLASSPATH} \
             InstallWASdsapiPlugin \
             -d ${DOMINO7_NAMES_CONF} \
             -l ${PLUGIN_HOME}${separatorFile}logs${separatorFile}unconfig${separatorFile}uninstallDomino7Plugin.log \
             -v remove \
             -version v6&quot;
        </echo>

        <exec executable="/bin/sh" dir="${PLUGIN_HOME}${separatorFile}bin">
           <arg value="-c"/>
           <arg value=". ${dominoUnInstall.file}"/>
        </exec>
       
        <delete file="${dominoUnInstall.file}"/> 
                        
    </target>
    
    <!--
    **************************************************************************
    TARGET: removePluginCfgScript
    Unix only
    **************************************************************************
    -->
    <target name="removePluginCfgScript">
        <antcall target="logBoth">
           <param name="logStatement" value="Removing file ${DOMINO7_NAMES_PATH}${separatorFile}setupPluginCfg.sh"/>
        </antcall>
        
       <delete file="${DOMINO7_NAMES_PATH}${separatorFile}setupPluginCfg.sh"/>
    </target>
    <!--
    **************************************************************************
    TARGET: ConfigureDomino7PluginWindows
    **************************************************************************
    -->
    <target name="ConfigureDomino7PluginWindows"
            description="Configure Domino7 Plugin only if all required options are set"
            depends="detectCurrentOSFamily,  
                     setOSFileSeparator,  
                     logStartupProperties,                   
                     checkConfigFiles,
                     resolvePLUGINJAVAExecutableForTheCurrentPlatform,
            	       resolveClasspathForTheCurrentPlatform,
                     unInstallWASdsapiPluginWindows">        
        
        <antcall target="logBoth">
           <param name="logStatement" value="Uninstall complete"/>
        </antcall>
        
    </target>
    
    <!--
    **************************************************************************
    TARGET: ConfigureDomino7PluginUnix
    **************************************************************************
    -->
    <target name="ConfigureDomino7PluginUnix"
            description="Configure Domino7 Plugin only if all required options are set"
            depends="detectCurrentOSFamily,  
                     setOSFileSeparator, 
                     logStartupProperties,                    
                     checkConfigFiles,
                     resolvePLUGINJAVAExecutableForTheCurrentPlatform,
            	       resolveClasspathForTheCurrentPlatform,
                     unInstallWASdsapiPluginUnix,
                     removePluginCfgScript">
                     
        <antcall target="logBoth">
           <param name="logStatement" value="Uninstall complete"/>
        </antcall>
        
    </target>
        
    <!--
    **************************************************************************
    TARGET: Usage
    Displays usage information
    **************************************************************************
    -->
    <target name="Usage" if="CMDLINE_CFG"
            description="Display usage information">       
      	<echo message="Usage: -ant -f ConfigureDomino7.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="-DDOMINO7_NAMES_CONF=Domino7NamesNsf"/>
        <echo message="    required - the full path name of the Domino7 names.nsf file"/>
      	<echo message="-DDOMINO7_NOTES_CONF=Domino7NotesJar"/>
        <echo message="    required - the full path name of the Domino7 Notes.jar file"/>     
                                                            
    </target>                                               

    <!--
    **************************************************************************
    TARGET: validateAntCall
    This is the main target. Check that all required config options are present.
    If all required config options are present set Domino7Target to 
    ConfigureDomino7Plugin. Otherwise set Domino7Target to "Usage" so the usage
    information can be displayed
    **************************************************************************
    -->
    <target name="validateAntCall"
        description="Validate call to this script">
        
        <condition property="ValidOptions">                   
           <and>                                            
              <isset property="DOMINO7_NAMES_CONF"/>               
              <isset property="DOMINO7_NOTES_CONF"/>
              <isset property="DOMINO7_NOTES_USER"/>
              <isset property="OS_NAME"/>
              <isset property="PLUGIN_HOME"/> 
              <isset property="WEBSERVER_TYPE"/>                       
              <not>
                 <equals arg1="${DOMINO7_NAMES_CONF}" arg2=" " trim="true"/>               
              </not>
              <not>   
                 <equals arg1="${DOMINO7_NOTES_CONF}" arg2=" " trim="true"/>
              </not>
              <not>  
                 <equals arg1="${DOMINO7_NOTES_USER}" arg2=" " trim="true"/>
              </not>
              <equals arg1="${WEBSERVER_TYPE}" arg2="DOMINO" trim="true"/>
              <equals arg1="${WEBSERVER_SELECTED}" arg2="domino7" trim="true"/>              
           </and>                                           
        </condition>
              
        <condition property="Domino7Target" value="Usage">
           <isfalse value="${ValidOptions}"/>
        </condition>
        
        <condition property="Domino7Target" value="ConfigureDomino7PluginWindows">
           <and>
              <istrue value="${ValidOptions}"/>
              <os family="windows"/>
           </and>
        </condition>

        <condition property="Domino7Target" value="ConfigureDomino7PluginUnix">
           <and>
              <istrue value="${ValidOptions}"/>
              <os family="unix"/>
           </and>
        </condition>

        <antcall target="${Domino7Target}"/>  
        
    </target>
</project>
