####
#### uninstallApp.jacl 
####
####
####  Deploy an application
####  
####  Usage:
####     
####     wsadmin -username system \
####             -password \
####             -f uninstallApp.jacl clustername appName
####
#### 
####  Arguments:
####
####      arg1 - cluster name
####      arg2 - application name 
####
####


proc uninstallApp {clusterName appName} {


  ###
  ### set up globals
  ###

  global AdminConfig
  global AdminControl
  global AdminApp



  ###
  ### We assume that there is only one cell, and we are on it
  ###

  set cellname [$AdminControl getCell]
  set cell [$AdminConfig getid /Cell:$cellname/]
 


  ###
  ### List currently installed applications
  ###

  set applications [$AdminApp list]
  puts " "
  puts "Installed Applications: "
  puts "$applications "


  puts " "
  puts "Uninstalling $appName"

  $AdminApp uninstall $appName

  $AdminConfig save





###
### Ask the ClusterMgr to refresh its list of clusters
###
###

set clusterMgr [$AdminControl completeObjectName type=ClusterMgr,cell=$cellname,*]

if {[llength $clusterMgr] == 0} {
    puts "clusterMgr MBean not found for cell $cellname"
    return
}

$AdminControl invoke $clusterMgr retrieveClusters


set cells [$AdminConfig list Cell]

foreach cell $cells {
     set cname [$AdminConfig showAttribute $cell name]
     set nodes [$AdminConfig list Node $cell]

     puts " "
     puts "Cell: $cname         "

     foreach node $nodes {

        set nname [$AdminConfig showAttribute $node name]
        puts " "
        puts "Node: $nname            "


        puts "Checking for the existence of a NodeSync MBean on node $nname"
        set nodeSync [$AdminControl completeObjectName type=NodeSync,node=$nname,*]

        if {[llength $nodeSync] == 0} {
           puts "NodeSync MBean not found for name $nname"
           return
        }
        set enabled [$AdminControl getAttribute $nodeSync serverStartupSyncEnabled]

        if {[string compare $enabled "false"] == 0} {
           puts "Invoking synchronization for node $nodeSync because serverStartupSyncEnabled is set to false..."
           $AdminControl invoke $nodeSync sync
           puts "Done with synchronization."
     }
  }
}


  ###
  ### Ask the Cluster MBean to start the cluster
  ###
  ###

  set cluster [$AdminControl completeObjectName type=Cluster,name=$clusterName,*]
  puts "Invoking start for cluster $clusterName"
  $AdminControl invoke $cluster start

}


 


###
### Main
###

if { !($argc == 2) } {
   puts "uninstallApp.jacl: "
   puts " "
   puts "Args: "
   puts "  cluster name, "
   puts "  Application name"
   puts ""
   puts "For example"
   puts "wsadmin.sh -username system \ "
   puts "           -password password \ "
   puts "           uninstallApp.jacl  clusterName appName" 
} else { 
   set clusterName  [lindex $argv 0]  
   set appName      [lindex $argv 1]

   uninstallApp $clusterName $appName
}