#!/bin/sh
# the next line restarts using tclsh -*- tcl -*- \
exec tclsh "$0" "$@"
#
# traceroute --
#
#	This example demonstrates how to re-use standard procedures
#	provided by the Tnm library. Results are returned from the
#	library procedure as set of Tnm map events. Appropriate
#	bindings make convert the results into the correct format.
#
# Copyright (c) 1995-1996 Technical University of Braunschweig.
# Copyright (c) 1996-1997 University of Twente.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# @(#) $Id: traceroute,v 1.4 1998/02/20 14:34:10 schoenw Exp $

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

namespace import Tnm::*

# usage --
#
#	This procedure is used to display usage information if we
#	encounter invalid or missing arguments.
#
# Arguments:
#	-	None.
# Results:
#       -	Results are written to stdout.

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

# Check the command line arguments.

set retries 3
set maxttl 32

if {$argc == 0} usage

while {[llength $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

# Create a Tnm map with a node on it. Set up some bindings that pick
# up the results of every trace step and write them to stdout. Exit
# the process if we receive an event that we are done.

set node [[map create] create node -name $argv]
$node bind TnmMap:TraceRoute:Value { puts "%A" }
$node bind TnmMap:TraceRoute:Done { exit }
if [catch {TnmMap::TraceRoute $node} msg] {
    puts stderr "$argv0: $msg ($argv)"
    exit 2
}
