#!/usr/local/bin/wish8.4
#
# This programm need UCD-SNMP package installed
#
# Usage: TkSnmpTable.tcl hostname snmp_get_community table_pattern ...
# On exit returns list of selected interfaces
#
# Example: ./TkSnmpName.tcl cisco public interfaces.ifTable.ifEntry &
#  Shows interfaces table
# 
package require Tktable 
package require BLT 
#
set snmptable_prog "/usr/local/bin/snmptable  -Cf + -v 1 -c "
#
set host	  [ lindex $argv 0 ]
set community	  [ lindex $argv 1 ]
set tbl_pattern   [ lrange $argv 2 end ]
#
set showSnmpTable_exitFlag  0
set snmp_exitFlag 0
set tmpSnmp	""
set errSnmp	""
#
array set ::snmpArray [list]
#
wm withdraw .
#
proc CleanValue { val } {
    regsub -all {Counter32: |Gauge32: |Timeticks: \(\d+\) |OID: |Hex: |IpAddress: } $val {} rez
    return $rez
}
proc ShowSnmpTable {  } {
    set w .showSnmpTable
    set wt $w.tableFrame
    set wb $w.buttonFrame

    if {[ winfo exists $w ]} then  {
	if { [string compare [wm state $w] withdrawn] } {
	    raise $w ;
	    return $iflist
	} else {
	    wm deiconify $w	
	}    
    } else {
	toplevel $w -class Dialog
	wm transient $w [winfo toplevel [winfo parent $w]]
	wm title $w "HOST: $::host   TABLE:  $::tbl_pattern"
	wm withdraw $w
#
	grid rowconfig $w 0 -weight 1 -minsize 0
	grid columnconfig $w 0 -weight 1 -minsize 0
	frame $wt	
#
	grid rowconfig	$wt  0 -weight 1 -minsize 0
	grid columnconfig   $wt  0 -weight 1 -minsize 0
	table $wt.tbl -rows 1  -cols 8 \
	    -variable ::snmpArray \
	    -titlerows 1 -titlecols 1 \
	    -yscrollcommand "$wt.sy set "  \
	    -xscrollcommand "$wt.sx set "  \
	    -roworigin 0 -colorigin 0 \
	    -width 600 -height 400 \
	    -selectmode normal \
	    -flashmode 0 \
	    -background #f700f700f700 \
	    -borderwidth 2 \
	    -selecttype row	\
	    -state disabled  \
	    -resizeborders both \
	    -anchor w
	scrollbar $wt.sy -command "$wt.tbl yview"
	scrollbar $wt.sx -orient horiz -command "$wt.tbl xview"
	grid $wt.tbl -in $wt \
	    -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
	grid $wt.sy -in $wt -column 1 -row 0 \
	    -columnspan 1 -rowspan 1 -sticky news
	grid $wt.sx -in $wt -column 0 -row 1 \
	    -columnspan 1 -rowspan 1 -sticky news
	$wt.tbl tag config title -bg #0000bf00ff00 -relief groove
	$wt.tbl tag config sel -fg #f700f700f700 -bg #48003d008b00 
	grid $wt -in $w -row 0 -column 0 -columnspan 1 -rowspan 1 -sticky news
	
	frame $wb
	button $wb.ok -text "Ok" -command "set ::showSnmpTable_exitFlag 1"
#	button $wb.cancel -text "Cancel" -command "set ::showSnmpTable_exitFlag 2"
#	pack $wb.cancel $wb.ok -side right -padx 5 -pady 5
	pack            $wb.ok -side right -padx 5 -pady 5
	grid $wb -in $w -row 1 -column 0 -columnspan 1 -rowspan 1 -sticky news

	update idletasks
#	set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
#	    - [winfo vrootx [winfo parent $w]]}]
#	set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
#	    - [winfo vrooty [winfo parent $w]]}]
	set x [expr {[winfo screenwidth $w]/2 - 310 \
	    - [winfo vrootx [winfo parent $w]]}]
	set y [expr {[winfo screenheight $w]/2 - 220 \
	    - [winfo vrooty [winfo parent $w]]}]

	wm geometry  $w  620x440+$x+$y
	wm deiconify $w	
	bind $w <Destroy> "set ::showSnmpTable_exitFlag 2"
    }
#
# The real work
#
    set localCursor [ $w cget -cursor ]
    $w configure -cursor watch
#
    array set headers [list]
    array set indexes [list]
    array set colswidth [ list 0 3 ]
    set hind 0
    set lind 0
    regsub -all {\.} $::tbl_pattern {\.} pat2
    set pat2 "$pat2\."
    catch { eval  blt::bgexec ::snmp_exitFlag -output ::tmpSnmp -error ::errSnmp \
	$::snmptable_prog  $::community $::host $::tbl_pattern
    }
    if { [string match $::tmpSnmp "" ] } {
	tk_dialog .snmpError "SNMPget Error" \
	    "Can't retriev SNMP data from \"$::host\": $::errSnmp" \
	    error 0 "Approve" 
    } else {
	foreach str [ split $::tmpSnmp "\n" ] {
	    if ![ regexp -- {\+} $str] { continue }
	    set fields [ split $str "+" ]
	    set cind 0
	    foreach fld $fields {
		set ::snmpArray($lind,$cind) $fld
		incr cind		
	    }
	    incr lind
	    if { $cind > $hind } { set hind $cind }
	    
	}
	eval $wt.tbl width [ array get colswidth ]
	$wt.tbl configure -rows $lind -cols $hind		
   }   
#
    $w configure -cursor $localCursor
#
    set oldFocus [focus]
    set oldGrab [grab current $w]
    if {[string compare $oldGrab ""]} {
	set grabStatus [grab status $oldGrab]
    }
    grab $w
    focus $wb.ok
#
    tkwait variable ::showSnmpTable_exitFlag

    catch {focus $oldFocus}
    if { [winfo exists $w] } {
	grab release $w
	wm withdraw $w
    }
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }    
    return  
}
#
#
# Main loop
#
if { $host =="" || $community == "" || $tbl_pattern == "" } {
    puts "Some parameters missed"
    exit
}
#
bind . <Alt-F4> { destroy . }
bind . <Escape> { destroy . }
#
ShowSnmpTable
#
exit