#!/usr/bin/wish

set filenames [list $argv]
set filename [lindex $filenames 0]

set w .test 

catch {destroy $w}
toplevel $w

if {[winfo depth $w] > 1} {
    set bold "-background #43ce80 -relief raised -borderwidth 1"
    set normal "-background {} -relief flat"
} else {
    set bold "-foreground white -background black"
    set normal "-foreground {} -background {}"
}
catch {destroy $w}


proc displayTSGPlot {filename tracenum from to} {
       puts stdout $tracenum;
       catch { [exec tcptrace -S -o$tracenum $filename ]}
       catch { [exec xplot -x ${from}2${to}_tsg.xpl ${to}2${from}_tsg.xpl "&"] };
}

proc displayRTTPlot {filename tracenum from to} {
       puts stdout $tracenum;
       catch { [exec tcptrace -G -o$tracenum $filename ]}
       catch { [exec xplot -x ${from}2${to}_rtt.xpl ${to}2${from}_rtt.xpl "&"] };
}

proc displayTPUTPlot {filename tracenum from to} {
       puts stdout $tracenum;
       catch { [exec tcptrace -T -o$tracenum $filename ]}
       catch { [exec xplot -x ${from}2${to}_tput.xpl ${to}2${from}_tput.xpl "&"] };
}


proc CreateTcpWindow {} {
    global w bold normal
    global filename
    variable test;
    variable test2;
    variable test3;
    variable tcpcount;
    set tcpcount 1

    catch {destroy $w}
    toplevel $w
    
    text $w.text -yscrollcommand "$w.scroll set" -setgrid true \
	-width 120 -height 40 -wrap word
    scrollbar $w.scroll -command "$w.text yview"
    pack $w.scroll -side right -fill y
    pack $w.text -expand yes -fill both

    $w.text insert end "Click left for a Time Sequence Graph\nClick Right for a Round Trip Time Graph\nClick Midle for a Thruput Graph\n"

#    frame .tcplist
    set test {}
    catch { set test [exec tcptrace $filename ]}
    set test2 [split $test \n]
    set test2 [lrange $test2 7 end]
    foreach test3 $test2 {

# Caculate the name of the TCP stream (from)2(to)_xxx.xpl
      set beginbracket [string first "(" $test3] 
      incr beginbracket 1
      set endbracket [string first ")" $test3]
      incr endbracket -1
      set name [string range $test3 $beginbracket $endbracket]
      set two [string first "2" $name]
      incr two -1
      set from [string range $name 0 $two]
      incr two 2
      set to [string range $name $two end]

      $w.text insert end "${test3}" "d${tcpcount}"
      $w.text insert end \n
      $w.text tag bind "d${tcpcount}" <Any-Enter> "$w.text tag configure d${tcpcount} $bold"
      $w.text tag bind "d${tcpcount}" <Any-Leave> "$w.text tag configure d${tcpcount} $normal"

      $w.text tag bind "d${tcpcount}" <1> "displayTSGPlot $filename $tcpcount $from $to;"
      $w.text tag bind "d${tcpcount}" <2> "displayTPUTPlot $filename $tcpcount $from $to;"
      $w.text tag bind "d${tcpcount}" <3> "displayRTTPlot $filename $tcpcount $from $to;"
      incr tcpcount 1
    }

    button $w.refresh -text Refresh  \
       -command "CreateTcpWindow"
    pack $w.refresh 

    $w.text mark set insert 0.0
    $w.text configure -state disabled
}

button .delete -text Exit  \
    -command "destroy .;"
pack .delete 

CreateTcpWindow 
