# Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# IBM makes no representations or warranties about the suitability of
# this program, either express or implied, including but not limited to
# the implied warranties of merchantability, fitness for a particular
# purpose, or non-infringement. 

# 
# # Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# # IBM makes no representations or warranties about the suitability of
# # this program, either express or implied, including but not limited to
# # the implied warranties of merchantability, fitness for a particular
# # purpose, or non-infringement. 
# 
#


proc install_war {serverName nodeName args} {

   #--------------------------------------------------------------
   # check for optional argument 
   #--------------------------------------------------------------
   set app              [lindex $args 0]
   set appName          [lindex $args 1]
   set vhost            [lindex $args 2]
   set top_dir          [lindex $args 3]
   set trigo_etc_dir    [lindex $args 4]
   set min_mem          [lindex $args 5]
   set max_mem          [lindex $args 6]
   set cpath            [lindex $args 7]
   set svc_name         [lindex $args 8]
   set port             [lindex $args 9]
   set username         [lindex $args 10]

   #--------------------------------------------------------------
   # set up globals
   #--------------------------------------------------------------
   global AdminConfig
   global AdminControl
   global AdminApp

   #--------------------------------------------------------------
   # do some sanity checking 
   #     -- do we have a node by this name? 
   #--------------------------------------------------------------
   set node [$AdminConfig getid /Node:$nodeName/]
   puts "install_war: checking for existence of node $nodeName"
   if {[llength $node] == 0} {
      puts "install_war: Error -- node not found for name $nodeName"
      return 
   }

   #--------------------------------------------------------------
   #     -- was a earfile name supplied 
   #--------------------------------------------------------------
   if {[llength $app] == 0} {
      puts "install_war: Error -- No application specified."
      return 
   }

   #--------------------------------------------------------------
   # Change the list of transports associated with the WebContainer
   # for this server.
   #--------------------------------------------------------------
   puts "install_war: modifying HTTP port addresses"
   set installedServer [$AdminConfig getid /Node:$nodeName/Server:$serverName/]
   set http_nonsecure_address [list [list sslEnabled false] [list address [list [list host {}] [list port $port]]]] 
   set transports             [list transports:HTTPTransport [list $http_nonsecure_address]]
   set web_container [$AdminConfig list WebContainer $installedServer]
   $AdminConfig modify $web_container [list $transports]

   puts "install_war: Configuring for WebSphere Product Center"

   # memory parameters
   set java_vm [$AdminConfig list JavaVirtualMachine $installedServer]
   set min_heap [list initialHeapSize $min_mem]
   set max_heap [list maximumHeapSize $max_mem]

   #empty old classpath settings 
   $AdminConfig modify $java_vm {{classpath {}}}

   # JVM Custom Properties
   set sys_prop_top [list systemProperties [list [list [list name TOP] [list required false] [list value $top_dir]]]]

   set sys_prop_etc [list systemProperties [list [list [list name CCD_ETC_DIR] [list required false] [list value $trigo_etc_dir]]]]

   set sys_prop_exit [list systemProperties [list [list [list name exit_if_config_file_not_found] [list required false] [list value no]]]]

   set wpc_classpath [list classpath "$cpath"]

   set sys_prop_svcname [list systemProperties [list [list [list name svc_name] [list required false] [list value $svc_name]]]]

   set sys_prop_sec [list systemProperties [list [list [list name java.security.policy] [list required false] [list value $trigo_etc_dir/default/java.policy]]]]

   set sys_prop_sec2 [list systemProperties [list [list [list name enableJava2Security] [list required false] [list value true]]]]

   puts "install_war: Setting heap size to min: $min_mem max: $max_mem"
   $AdminConfig modify $java_vm [list $min_heap $max_heap]
   puts "install_war: Setting TOP to $top_dir"
   puts "install_war: Setting CCD_ETC_DIR to $trigo_etc_dir"
   puts "install_war: Setting Classpath to $cpath"
   puts "install_war: Setting svc_name to $svc_name"
   $AdminConfig modify $java_vm [list $sys_prop_top $sys_prop_etc $sys_prop_exit $wpc_classpath $sys_prop_svcname $sys_prop_sec $sys_prop_sec2]


   #--------------------------------------------------------------
   # Install an application onto this server
   #--------------------------------------------------------------
   puts "install_war: installing the application"
   set app_options [list -server $serverName]
   lappend app_options -node $nodeName
   lappend app_options -contextroot /
   lappend app_options -defaultbinding.virtual.host $vhost
   lappend app_options -MapWebModToVH [list [list ccd.war ccd.war,WEB-INF/web.xml $vhost ]]
   if {[llength $appName] > 0} {
      lappend app_options -appname $appName
   }
   if {[lsearch -exact [$AdminApp list] $appName] >= 0} {
     puts "install_war: Application $appName is already installed."
     puts "install_war: Removing application $appName..."
     $AdminApp uninstall $appName
   }
   $AdminApp install $app $app_options 

   #--------------------------------------------------------------
   # Save all the changes 
   #--------------------------------------------------------------
   puts "install_war: saving the configuration"
   $AdminConfig save

}

#-----------------------------------------------------------------
# Main
#-----------------------------------------------------------------
if { !($argc == 13) } {
   puts "install_war: incorrect number of parameters"
} else { 
   set nodeName         [lindex $argv 0]  
   set serverName       [lindex $argv 1]  
   set application      [lindex $argv 2]
   set appName          [lindex $argv 3]
   set vhost		    [lindex $argv 4]
   set top_dir          [lindex $argv 5]
   set trigo_etc_dir    [lindex $argv 6]
   set min_mem          [lindex $argv 7]
   set max_mem          [lindex $argv 8]
   set cpath            [lindex $argv 9]
   set svc_name         [lindex $argv 10]
   set port             [lindex $argv 11]
   set username         [lindex $argv 12]

   install_war $serverName $nodeName $application $appName $vhost $top_dir $trigo_etc_dir $min_mem $max_mem $cpath $svc_name $port $username
}
