#!/usr/local/bin/wish8.4
#
#
package require BLT
#
#
set netmond_server [ lindex $argv 0 ]
set netmond_port   [ lindex $argv 1 ]
set node_name      [ lindex $argv 2 ]
set fd 0
#
namespace eval shState {
variable shState_exitFlag        0      	\
        background              black   	\
	foreground              white   	\
        font                    {Courier 10}    \
}
#	
proc  AskNodeState { node } {
    if { [catch { socket $::netmond_server $::netmond_port } ::fd ] } {
	puts "error $::fd"
	return ""
    }
    puts $::fd "ANY $node\n\n"
    flush $::fd
    set rez "" 
    lappend rez [ clock format [clock seconds ] -format "%d-%m-%Y %T %Z\n" ]  
    set str ""
    while (1) {
	gets $::fd str
#	puts $str
	if { [ regexp "Invalid" $str  ] } { break }
	if { [ regexp "$node!.*" $str] } {   
#	if { [ regexp "$node!.*STATE" $str] } {   }
	    lappend rez "$str\n"	
	}
    }    
    close $::fd
    return [ join $rez {} ]
}
#
proc ShowNodeState_GUIconstructor { } {
    set w .shNodeState
    set fl $w.viewFrame
    
    toplevel $w
    wm withdraw $w
    
    grid rowconfig  $w  1 -weight 1
    grid columnconfig  $w  0 -weight 1 -minsize 0
    
    frame $fl	-relief raised
    grid  $fl -in $w -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
    
    grid rowconfig    $fl 0 -weight 1 -minsize 0
    grid columnconfig $fl 0 -weight 1 -minsize 0
    
    scrollbar $fl.hs -orient horiz -command "$fl.txt xview"	
    scrollbar $fl.vs  -command "$fl.txt yview"	
    
    text $fl.txt -state disabled -xscrollcommand "$fl.hs set" -yscrollcommand "$fl.vs set" \
        -background $::shState::background -foreground $::shState::foreground \
        -wrap none -font $::shState::font

    $fl.txt tag configure normalTag -foreground white
    $fl.txt tag configure upTag     -foreground green
    $fl.txt tag raise upTag
    $fl.txt tag configure dgrdTag   -foreground magenta
    $fl.txt tag raise dgrdTag    
    $fl.txt tag configure warnTag   -foreground yellow
    $fl.txt tag raise warnTag    
    $fl.txt tag configure errorTag  -foreground red
    $fl.txt tag raise errorTag
    
    grid $fl.txt -in $fl \
	-row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $fl.hs -in $fl \
	-row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $fl.vs -in $fl \
	-row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news

    update idletasks
    bind $w <Home> { 	.shNodeState.viewFrame.txt see 0.0 }
    bind $w <End> { .shNodeState.viewFrame.txt see end }
    bind $w <Up>  { .shNodeState.viewFrame.txt yview scroll -1 units }
    bind $w <Down>  { .shNodeState.viewFrame.txt yview scroll +1 units }
    bind $w <Control-Up>  { .shNodeState.viewFrame.txt yview scroll -1 pages }
    bind $w <Control-Down>  { .shNodeState.viewFrame.txt yview scroll +1 pages }
    bind $w <Prior>  { .shNodeState.viewFrame.txt yview scroll -1 pages }
    bind $w <Next>  { .shNodeState.viewFrame.txt yview scroll +1 pages }
    bind $w <Left>  { .shNodeState.viewFrame.txt xview scroll -1 units }
    bind $w <Right>  { .shNodeState.viewFrame.txt xview scroll +1 units }
    bind $w <Control-Left>  { .shNodeState.viewFrame.txt xview scroll -1 pages }
    bind $w <Control-Right>  { .shNodeState.viewFrame.txt xview scroll +1 pages }
}
#
proc ShowNodeState { node } {
    set w .shNodeState
    set fl $w.viewFrame

    if { ![winfo exists $w] } { ShowNodeState_GUIconstructor }    
    wm title $w "$node state report"
    $fl.txt configure -state normal
    $fl.txt delete 0.0 end
    $fl.txt insert end [ AskNodeState $node ]
    set cls [lindex [split [$fl.txt index end] '.'] 0 ]
    set j 0
    set max_s 0
    for { set i 0 } { $i < $cls } { incr i } {
        incr j
        set str [ $fl.txt get $i.0 $j.0 ]
	set sx [string length $str ]
	if { $max_s < $sx } { set max_s $sx }
        if { [ regexp "\"UP\"|\"ESTABLISHED\"" $str ] } {
    	    $fl.txt tag add upTag  $i.0 $j.0
	    continue
	}
	if { [ regexp "\"WARNING\"" $str ] } {
	    $fl.txt tag add warnTag  $i.0 $j.0
	    continue
	}
	if { [ regexp "\"DEGRADED\"" $str ] } {
	    $fl.txt tag add dgrdTag  $i.0 $j.0
	    continue
	}
        if { [ regexp "\"DOWN\"" $str ] } {
    	    $fl.txt tag add errorTag  $i.0 $j.0
	    continue
	}
    }
    $fl.txt configure -state disabled -height $cls -width  [expr $max_s + 2 ]
    set wd [winfo reqwidth  $fl.txt ]
    set h  [winfo reqheight $fl.txt ]
    if { $wd > [expr [winfo screenwidth $w]/2] } { set wd [expr [winfo screenwidth $w]/2 ] }
    if { $h >  [expr [winfo screenheight $w]/2] } { set h [expr [winfo screenheight $w]/2 ] }
    set x [expr {[winfo screenwidth $w]/2 - $wd/2  \
        - [winfo vrootx [winfo parent $w]]}]
    set y [expr {[winfo screenheight $w]/2 - $h/2 \
        - [winfo vrooty [winfo parent $w]]}]
    wm geom $w [format "%sx%s+%s+%s" $wd $h $x $y]
    update
    if { [string equal [wm state $w] withdrawn ] } {
        wm deiconify $w
    }
    raise $w
    focus $fl.txt
    return    
}
#----------------------------------------------
if { [info commands DestroyMain ]=="" } {
    proc DestroyMain {} { destroy . }
}
# Main Loop
#
if { $::netmond_server =="" || $::netmond_port == "" || $::node_name == "" } {
    puts "Some parameters missed"
    exit
}
#
wm withdraw .
ShowNodeState $node_name
#
bind .shNodeState <Destroy>	+DestroyMain
bind .shNodeState <Control-x>	+DestroyMain
bind .shNodeState <F10>		+DestroyMain
bind .shNodeState <Alt-F4>	+DestroyMain
bind .shNodeState <Escape>	+DestroyMain
#
#