<project name="bootstrapPluginsIIS6" default="bootstrapPluginsIIS6" basedir=".">

   <!--
# IBM Confidential OCO Source Material
# 5639-D57, 5630-A36, 5630-A37, 5724-D18 (C) COPYRIGHT International Business Machines Corp. 1997, 2011
# 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 install log
   ***************************************************************************
   -->      
   <target name="updateLogFile"
        description="Update webserver specific logfile">
<echo file="${install.log}" append="true">${stringToAppend}${CRLF}</echo>
   </target>

    <!-- 
    ***************************************************************************
    TARGET: logBoth
    Log statement to both webserver install log and cfg manager log
    ***************************************************************************
    -->      
    <target name="logBoth">
       <echo>${logStatement}</echo>
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="${logStatement}"/>
        </antcall>            
    </target>    

    <!-- 
    ***************************************************************************
    TARGET: logStartupProperties
    Log startup information to the install log
    ***************************************************************************
    -->      
    <target name="logStartupProperties"
            description="Log properties passed to script">
        <property name="install.log" value="${PLUGIN_HOME}\logs\config\installIIS6Plugin.log"/> 
        <mkdir dir="${PLUGIN_HOME}\logs\config"/>
        <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="** Configure IIS6 Webserver Plugin ${timestamp}"/>
        </antcall>     
        
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="************************************************************"/>
        </antcall>        
       
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="PLUGIN_HOME            = ${PLUGIN_HOME}"/>
        </antcall>     
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="WAS_PLUGIN_CFG_XML     = ${WAS_PLUGIN_CFG_XML} "/>
        </antcall>     
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="OS_NAME                = ${OS_NAME}"/>
        </antcall>
	
        <antcall target="updateLogFile">
           <param name="stringToAppend" value="INSTALLER ARCHITECTURE = ${INSTALL_ARCH}"/>
        </antcall>
	
        <antcall target="updateLogFile">
         <param name="stringToAppend" value="PLUGIN_INSTALL TYPE      = ${PLUGIN_INSTALL_TYPE}"/>
        </antcall>            

        <antcall target="updateLogFile">
          <param name="stringToAppend" value="WEBSERVER_TYPE          = ${WEBSERVER_TYPE}"/>
        </antcall>     

        <antcall target="updateLogFile">
          <param name="stringToAppend" value="WEBSERVER_NAME          = ${WEBSERVER_NAME}"/>
        </antcall> 

        <antcall target="updateLogFile">
          <param name="stringToAppend" value="PLUGIN_CONFIGURE_IIS6   = ${PLUGIN_CONFIGURE_IIS6}"/>
        </antcall> 

    </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)">
                    
        <property name="separatorFile" value="\"/>
        
        <property name="CRLF" value="&#13;&#10;"/>
    
    </target>

    <!--
    **************************************************************************
    TARGET: replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile
    Replace all instances of the token with the token value in the specified file
    **************************************************************************
    -->      
    <target name="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
        <replace file="${file}"
         token="${token}"
         value="${tokenValue}"/>
    </target>

    
    <!--
    **************************************************************************
    TARGET: SetupUninstall
    Create uninstall file to be run at uninstall time.  Install file has the
    pattern 99SBootStrap<WEBSERVERNAME><timestamp>
    **************************************************************************
    -->
    <target name="SetupUninstall" if="cfgFiles.ok"
            description="Setup uninstall script that will be run during uninstall time">

       <property name="uninstall.location" value="${PLUGIN_HOME}${separatorFile}config${separatorFile}actionRegistry${separatorFile}actions"/> 
       <property name="uninstall.file" value="${uninstall.location}${separatorFile}IIS6UnInstall.ant"/>

       <antcall target="logBoth">
          <param name="logStatement" value="Updating uninstall file ${uninstall.file}"/>
       </antcall>
        
       <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
                    <param name="file" value="${uninstall.file}"/>
                    <param name="token" value="$(PLUGIN_HOME)"/>
                    <param name="tokenValue" value="${PLUGIN_HOME}"/>
       </antcall>    
       <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
                    <param name="file" value="${uninstall.file}"/>
                    <param name="token" value="$(OS_NAME)"/>
                    <param name="tokenValue" value="${OS_NAME}"/>
       </antcall>    
        
       <antcall target="chkLogDir"/>
            
    </target>


    <!--
    **************************************************************************
    TARGET: createLogDir
    create directory for webserver plugin log
    **************************************************************************
    -->
    <target name="createLogDir" if="default.logDir.notfound">
        <antcall target="logBoth">
           <param name="logStatement" value="Creating directory ${default.log.dir}"/>
        </antcall>
        <mkdir dir="${default.log.dir}"/>
    </target>
    
    <!--
    **************************************************************************
    TARGET: chkLogDir
    check whether PLUGINHOME/logs/<webservername> directory exists
    **************************************************************************
    -->
    <target name="chkLogDir">
    
       <property name="default.log.dir" value="${PLUGIN_HOME}${separatorFile}logs${separatorFile}${WEBSERVER_NAME}"/>  
  
        <condition property="default.logDir.notfound">
           <not>
              <available file="${default.log.dir}" type="dir"/>
           </not>
        </condition>
  
        <condition property="logDirMsg" value="Log directory ${default.log.dir} exists">
           <isfalse value="${default.logDir.notfound}"/>
        </condition>
        
        <condition property="logDirMsg" value="Log directory ${default.log.dir} does not exist">
           <istrue value="${default.logDir.notfound}"/>
        </condition>
        
        <antcall target="logBoth">
           <param name="logStatement" value="${logDirMsg}"/>
        </antcall>
        <antcall target="createLogDir" />
    </target>
    

    <!--
    **************************************************************************
    TARGET: createCfgDir
    create directory for plugin-cfg.xml
    **************************************************************************
    -->
    <target name="createCfgDir" if="default.cfgDir.notfound">
        <antcall target="logBoth">
           <param name="logStatement" value="Creating directory ${default.config.dir}"/>
        </antcall>
        <mkdir dir="${default.config.dir}"/>
    </target>

    <!--
    **************************************************************************
    TARGET: updatePlgCfg
    copy plugin-cfg.xml from template directory to 
    PLUGIN_HOME\config\webserver name directory.  
    If the directory does not exist it must be created
    Replace the PLUGIN_HOME and WAS_HOSTNAME tags with the appropriate values
    **************************************************************************
    -->
    <target name="updatePlgCfg" if="update.cfg">
        <antcall target="logBoth">
           <param name="logStatement" value="Installing default plugin-cfg.xml file in directory ${default.config.dir}"/>
        </antcall>
       
        
        <antcall target="createCfgDir"/>
       
        <copy file="${PLUGIN_HOME}${separatorFile}config${separatorFile}templates${separatorFile}plugin-cfg.xml" 
              tofile="${default.config.file}"
              overwrite="false"/>
             
        <replace file="${default.config.file}" 
                 token="$(PLUGIN_HOME)"
                 value="${PLUGIN_HOME}" />
        
        <replace file="${default.config.file}" 
                 token="$(WAS_HOSTNAME)"
                 value="${WAS_HOSTNAME}" />       
                           
        <replace file="${default.config.file}" 
                 token="$(WEBSERVER_NAME)"
                 value="${WEBSERVER_NAME}" />       
    </target>    


    <!--
    **************************************************************************
    TARGET: updatePlgKeyStore
    copy plugin key store files  from etc directory to 
    plug-in config file directory.  
    **************************************************************************
    -->
    <target name="updatePlgKeyStore" if="plugin.cfgFile.found">
        <antcall target="logBoth">
           <param name="logStatement" value="Installing default keystore files in directory ${default.config.dir}"/>
        </antcall>

        <copy file="${PLUGIN_HOME}${separatorFile}etc${separatorFile}plugin-key.kdb" 
              tofile="${default.config.dir}${separatorFile}plugin-key.kdb"
              overwrite="false"/>

        <copy file="${PLUGIN_HOME}${separatorFile}etc${separatorFile}plugin-key.sth" 
              tofile="${default.config.dir}${separatorFile}plugin-key.sth"
              overwrite="false"/>

      <!--  <copy file="${PLUGIN_HOME}${separatorFile}etc${separatorFile}plugin-key.crl" 
              tofile="${default.config.dir}${separatorFile}plugin-key.crl"
              overwrite="false"/>  -->

        <copy file="${PLUGIN_HOME}${separatorFile}etc${separatorFile}plugin-key.rdb" 
              tofile="${default.config.dir}${separatorFile}plugin-key.rdb"
              overwrite="false"/>
    </target>    
    

    <!--
    **************************************************************************
    TARGET: chkPlgCfg
    Check if plugin-cfg.xml file exists
    If plugin-cfg.xml exists it will not be overwritten
    If non default location is specified, file MUST exist.
    **************************************************************************
    -->
    <target name="checkPlgCfg"
            description="check plugin-cfg.xml.">
        
       <property name="default.config.dir" value="${PLUGIN_HOME}${separatorFile}config${separatorFile}${WEBSERVER_NAME}"/>  
       <property name="default.config.file" value="${default.config.dir}${separatorFile}plugin-cfg.xml"/>
                   
       <condition property="default.cfgDir.notfound">
          <not>
             <available file="${default.config.dir}" type="dir"/>
          </not>
       </condition>
       
       <condition property="default.cfgFile.notfound">
          <not>
             <available file="${default.config.file}"/>
          </not>
       </condition>

       <condition property="standalone.cfgFile.found">
          <available file="${WAS_PLUGIN_CFG_XML}"/>
       </condition>

       <!--
       **********************************************************************
       cfgFiles.ok if 1) file exists
                      2) Given file is same as default plug-in config file
       **********************************************************************
       -->
       <condition property="cfgFiles.ok">
          <or>
             <istrue value="${standalone.cfgFile.found}"/>
             <equals arg1="${default.config.file}" arg2="${WAS_PLUGIN_CFG_XML}" />
          </or>
       </condition>
              
       <condition property="cfgLogMsg" value="Default plugin-cfg.xml exists in ${default.config.dir}.  The file will not be overwritten">
          <and>
             <isfalse value="${default.cfgFile.notfound}"/>
             <istrue value="${cfgFiles.ok}"/>
          </and>
       </condition>

       <condition property="cfgLogMsg" value="Default plugin-cfg.xml does not exist in ${default.config.dir}.">
          <and>
             <istrue value="${default.cfgFile.notfound}"/>
             <istrue value="${cfgFiles.ok}"/>
          </and>
       </condition>
       
       <condition property="cfgLogMsg" 
                  value="INSTALL ERROR!!! ${WAS_PLUGIN_CFG_XML} does not exist.${CRLF}
           The file MUST exist for a local_standalone configuration. If you selected to have the${CRLF}
           definition for webserver ${WEBSERVER_NAME} created by the plugin installer please check${CRLF}
           ${PLUGIN_HOME}${separatorFile}logs${separatorFile}configure_iis6_webserver.log${CRLF}
           to determine the cause of the failure.">
          <isfalse value="${cfgFiles.ok}"/>
       </condition>
       
       <antcall target="logBoth">
          <param name="logStatement" value="${cfgLogMsg}"/>
       </antcall>
       
       <condition property="fatalError">
          <isfalse value="${cfgFiles.ok}"/>
       </condition>
        
       <antcall target="terminateOnFailure"/>
       
       <!--
       ******************************************************************************
       update.cfg if default config file does not exist
       ******************************************************************************
       -->
       <condition property="update.cfg">
          <and>
             <istrue value="${default.cfgFile.notfound}"/>
             <istrue value="${cfgFiles.ok}"/>
          </and>
       </condition>
       
       <antcall target="updatePlgCfg"/>
       
       <!--
       ******************************************************************************
       plugin.cfgFile.found if plugin config file exist
       ******************************************************************************
       -->
       <condition property="plugin.cfgFile.found">
          <available file="${WAS_PLUGIN_CFG_XML}"/>
       </condition>
       
       <antcall target="updatePlgKeyStore"/>


    </target>

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


    <!--
    **************************************************************************
    TARGET: createIISBinDir
    create directory for IIS bin
    **************************************************************************
    -->
    <target name="createIISBinDir" if="IISBinDir.notfound">
        <antcall target="logBoth">
           <param name="logStatement" value="Creating directory ${IISBin.dir}"/>
        </antcall>
        <mkdir dir="${IISBin.dir}"/>        
    </target>
    
    <!--
    **************************************************************************
    TARGET: installBinDir
    create and place files in PLUGIN_HOME/bin/IIS_<webserver_name> directory
    **************************************************************************
    -->
    <target name="installBinDir">
    
       <property name="IISBin.dir" value="${PLUGIN_HOME}${separatorFile}bin${separatorFile}IIS_${WEBSERVER_NAME}"/>  
  
        <condition property="IISBinDir.notfound">
           <not>
              <available file="${IISBin.dir}" type="dir"/>
           </not>
        </condition>
  
        <condition property="logBinMsg" value="IIS binary directory ${IISBin.dir} exists">
           <isfalse value="${IISBinDir.notfound}"/>
        </condition>
        
        <condition property="logBinMsg" value="IIS binary directory ${IISBin.dir} does not exist">
           <istrue value="${IISBinDir.notfound}"/>
        </condition>
        
        <antcall target="logBoth">
           <param name="logStatement" value="${logBinMsg}"/>
        </antcall>
        
        <antcall target="createIISBinDir" />
        
        <copy file="${PLUGIN_HOME}${separatorFile}bin${bitsDir}iisWASPlugin_http.dll" 
              tofile="${IISBin.dir}${separatorFile}iisWASPlugin_http.dll"
              overwrite="false"/>
              
        <echo file="${IISBin.dir}${separatorFile}plugin-cfg.loc" append="false">${WAS_PLUGIN_CFG_XML}
        </echo>        

    </target>


    <!--
    **************************************************************************
    TARGET: ConfigureIIS6Plugin
    **************************************************************************
    -->
    <target name="ConfigureIIS6Plugin"  if="cfgFiles.ok"
        description="Start IIS Plugin Configuration"
        depends="logStartupProperties">
        
        <antcall target="logBoth">
           <param name="logStatement" value="Installing IIS 6 plugin"/>
        </antcall>
        
        <antcall target="logBoth">
           <param name="logStatement" value="Running configureIIS"/>
        </antcall>
        
        <exec dir="${PLUGIN_HOME}/bin" executable="${PLUGIN_HOME}/bin/configureIIS.exe" 
              output="${PLUGIN_HOME}\logs\config\installIIS6Plugin_configureIIS.log"
              failonerror="true">
              <arg value="&quot;-install&quot; &quot;iisWASPlugin_http.dll&quot; &quot;sePlugins&quot; &quot;${PLUGIN_HOME}${separatorFile}bin${separatorFile}IIS_${WEBSERVER_NAME}&quot; &quot;IBM WebSphere 60 IIS plug-in&quot;"/>	
        </exec>    
        
        <antcall target="logBoth">
           <param name="logStatement" value="Running enableIISExtension"/>
        </antcall>
        
        <exec dir="${PLUGIN_HOME}/bin" executable="cmd"
              failonerror="true">
              <arg value="/c enableIISExtension.vbs &quot;${PLUGIN_HOME}\logs\config\installIIS6Plugin_enableIISExtension.log&quot;"/>	
        </exec> 
       
        <!-- <antcall target="SetupUninstall"/> -->
                
    </target>
    
    <!--
    **************************************************************************
    TARGET: startIIS6Configuration
    **************************************************************************
    -->
    <target name="startIIS6Configuration"
            depends="setOSFileSeparator,
                     SetBitsDir,
                     logStartupProperties,                   
                     chkInstall_Arch,
                     checkPlgCfg,
                     chkLogDir,
                     ConfigureIIS6Plugin,
                     installBinDir">
                     
        <antcall target="logBoth">
           <param name="logStatement" value="Install complete"/>
        </antcall>

    </target>
    
    <!-- TARGET:  chkInstall_Arch
    **************************************************************************
    Verifies that if 64 bit is chosen, then /64bits/ directory must exist.
    **************************************************************************
    -->
    <target name="chkInstall_Arch">
       <condition property="64bit.Dir.found">
             <available type="dir" file="${PLUGIN_HOME}${separatorFile}bin${separatorFile}64bits"/>
       </condition>
       
       <condition property="mismatch.detected">
          <and>
             <equals arg1="${INSTALL_ARCH}" arg2="64" trim="true"/>
             <isfalse value="${64bit.Dir.found}"/>
          </and>
       </condition>

       <condition property="cfgLogMsg" value="****  64 bit Architecture was chosen, but installed plugin doesn't contain the proper structure.  **** ${CRLF}****  Either choose 32 bit within Plugin Configuration Tool or Uninstall Plugin and Install 64 bit plugin.  **** ">
             <istrue value="${mismatch.detected}"/>
       </condition>
       
       <condition property="cfgLogMsg" value="64 bit directory was located.">
          <and>
             <equals arg1="${INSTALL_ARCH}" arg2="64" trim="true"/>
             <isfalse value="${mismatch.detected}"/>
          </and>
       </condition>
       
       <condition property="cfgLogMsg" value="Verified directories.">
             <isfalse value="${mismatch.detected}"/>
       </condition>

       <antcall target="logBoth">
          <param name="logStatement" value="${cfgLogMsg}"/>
       </antcall>
       
       <condition property="fatalError">
          <istrue value="${mismatch.detected}"/>
       </condition>

      <antcall target="terminateOnFailure"/>
    
    </target>

    <!-- TARGET:  SetBitsDir
    **************************************************************************
    This will set the bitsdir to /, /32bits/, or /64bits/
    **************************************************************************
    -->
    <target name="SetBitsDir">
        <available type="dir" file="${PLUGIN_HOME}${separatorFile}bin${separatorFile}64bits" property="plugin.install.64bit"/>
        
        <!-- z/os packaging is more simple -->
        <condition property="bitsDir" value="/">
            <equals arg1="${OS_NAME}" arg2="ZOS"/>
        </condition>
        
        <condition property="bitsDir" value="${separatorFile}32bits${separatorFile}">
           <and>
             <equals arg1="${INSTALL_ARCH}" arg2="32" trim="true"/>
             <istrue value="${plugin.install.64bit}"/>    
           </and>
        </condition>
         
        <condition property="bitsDir" value="${separatorFile}64bits${separatorFile}">
           <and>
             <equals arg1="${INSTALL_ARCH}" arg2="64" trim="true"/>
             <istrue value="${plugin.install.64bit}"/>    
           </and>
        </condition>

        <condition property="bitsDir" value="${separatorFile}">
           <and>
             <equals arg1="${INSTALL_ARCH}" arg2="32" trim="true"/>
             <isfalse value="${plugin.install.64bit}"/> 
           </and>
        </condition>

     <!--   <condition property="bitsDir" value="${separatorFile}">
           <and>
             <equals arg1="${INSTALL_ARCH}" arg2="64" trim="true"/>
             <not>
                <istrue value="${plugin.install.64bit}"/> 
             </not>
           </and>
        </condition>  -->
        <echo message="bitsDir is ${bitsDir} PLUGIN64: ${plugin.install.64bit} OS-is-32bit: ${is.thirtytwo}"/>    
    </target>
 
    <!--
    **************************************************************************
    TARGET: Usage
    Displays usage information
    **************************************************************************
    -->
    <target name="Usage" if="CMDLINE_CFG"
            description="Display usage information">       
      	<echo message="Usage: -ant -f 99SBootStrapPluginsIIS6.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, c:\Program Files\IBM\WebSphere\Plugins)"/>
                                                            
    </target>                         
    

    <!--
    **************************************************************************
    TARGET: bootstrapPluginsIIS6
    This is the main target. Check that all required config options are present.
    If all required config options are present set IIS6Target to ConfigureIIS6Plugin
    Otherwise set IIS6Target to "Usage" so the usage information can be displayed
    **************************************************************************
    -->
    <target name="bootstrapPluginsIIS6"
        description="Bootstrap IIS6 Plugin">
        
        <condition property="ValidOptions">   
           <and>                                            
              <equals arg1="${PLUGIN_CONFIGURE_IIS6}" arg2="TRUE" trim="true" casesensitive="false"/>
              <isset property="WAS_PLUGIN_CFG_XML"/> 
              <isset property="PLUGIN_HOME"/>   
              <isset property="PLUGIN_INSTALL_TYPE"/>
	            <isset property="INSTALL_ARCH"/>
              <isset property="WEBSERVER_TYPE"/>                       
	            <isset property="WEBSERVER_NAME"/> 
              <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="IIS" trim="true"/>
           </and>
        </condition>
              
        <condition property="IIS6Target" value="startIIS6Configuration">
           <istrue value="${ValidOptions}"/>
        </condition>
        
        <condition property="IIS6Target" value="Usage">
              <isfalse value="${ValidOptions}"/>
        </condition>
        
        <antcall target="${IIS6Target}"/>
        
    </target>

</project>
