#!/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 ../scotty ; then
      exec ../scotty -nf $0 $*
    else
      exec scotty -nf $0 $*
    fi
}

##
## Print the ethernet load monitored by an etherd.
##

proc print_ether {host load} {
    set time_ms [lindex [lindex $load 0] 2]
    set bytes   [lindex [lindex $load 1] 2]
    set packets [lindex [lindex $load 2] 2]
    set mbits   [expr {$bytes*8/1000000.0}]
    set time_s  [expr {$time_ms/1000.0}]
    puts [format "%-12s %8.5f MBit/s %8.2f Packets/s %8.2f Bytes/Packet" \
		 $host [expr {$mbits/$time_s}] [expr {$packets/$time_s}] \
		     [expr {$bytes*1.0/$packets}] ]
    flush stdout
}

proc ether {hostnames time} {
    set okhosts ""
    set badhosts ""
    foreach host $hostnames {
	if [catch {sunrpc ether $host open} err] {
	    lappend badhosts $host
	} else {
	    lappend okhosts $host
	}		
    }
    if {$badhosts != ""} {
	puts stderr "Can not connect to $badhosts."
    }
    set hostnames $okhosts
    while {[llength $hostnames] > 0} {
        sleep $time
        foreach host $hostnames {
            set load [sunrpc ether $host]
            print_ether $host $load
        }
    }
}

if {$argc<2} {
    puts stderr "usage: etherload time hostnames"
    exit
}

ether [lrange $argv 1 [expr {$argc-1}]] [lindex $argv 0]

exit
