#!/bin/sh
# Tcl sees the next lines as an assignment to variable `kludge'.
# For sh, the two shifts cancel the effect of the set, and then we
# run scotty on this script.

set kludge { $*
    shift
    shift
    if test -x ../scwish ; then
      exec ../scwish -f $0 ${1+"$@"}
    else
      exec scwish -f $0 ${1+"$@"}
    fi
}

proc addlabel {s list} {
    if {[lsearch [snmp info] $s] < 0} { 
	job kill [job current] 
    }
    $s walk x $list {
	set x [lindex $x 0]
	set name  [mib name [lindex $x 0]]
	set value [lindex $x 2]
	if {[winfo exists .$s.$name]} {
	    .$s.$name configure -text "$name: $value"
	} else {
	    set frame [lindex [split $name .] 0]
	    catch {
		frame .$s.$frame -relief sunken -borderwidth 1
		pack .$s.$frame -fill x -anchor w -side bottom
	    }
	    pack [label .$s.$name -text "$name: $value" -font fixed] -anchor w
	}
    }
    update
}

proc quit {s} {
    destroy .$s
    $s destroy
    if {[winfo children .] == ""} {
	destroy .
    }
}

proc usage {} {
    puts stderr "usage: sshow hosts interval variables"
    exit
}

if {[llength $argv] < 3} {
    usage
}

set hosts [lindex $argv 0]
set time  [lindex $argv 1]
set vars  [join [lrange $argv 2 end]]
if {[catch {expr $time * 1000} time]} { 
    set time 5000; 
}

wm withdraw .

foreach host $hosts {
    set s [snmp session -address $host]
    toplevel .$s
    wm title .$s "sshow $host [expr $time / 1000] $vars"
    foreach arg $vars {
        job create "addlabel $s $arg" $time
    }
    button .$s.quit -text quit -command "quit $s"
    pack .$s.quit -side bottom -fill x
}
