#!/bin/sh
# the next line restarts using tclsh -*- tcl -*- \
exec tclsh "$0" "$@"

package require Tnm 2.2
package require TnmMap $tnm(version)

proc usage {} {
    puts stderr {usage: traceroute [-m max_ttl] [-w time] hostname}
    exit
}

##
## Parse the command line options.
##

set retries 3
set maxttl 32

if {$argc==0} usage

while {$argv!=""} {
    set argc [llength $argv]
    switch -- [lindex $argv 0] {
	{-m} {
	    if {$argc == 1} usage
	    set maxttl [lindex $argv 1]
	    set argv [lrange $argv 2 end]
	}
	{-w} {
	    if {$argc == 1} usage
	    icmp -timeout [lindex $argv 1]
	    set argv [lrange $argv 2 end]
	}
	{default} break
    }
}

if {[llength $argv] != 1} { usage } else {
    set m [map create]
    set n [$m create node -name $argv]
    $n bind TnmMapTraceRoute:Value { puts "%A" }
    $n bind TnmMapTraceRoute:Done { exit }
    TnmMapTraceRoute $n
}
