#!/bin/sh
# the next line restarts using wish \
exec wish "$0" ${1+"$@"}

proc TkTailFill { text fd filename { actualizar 1}} {
    global tcl_platform TkTailPriv

    if { ![winfo exists $text] } { 
	catch { close $fd }
	return 
    }
    if { $fd == "" } {
	set JustOpen 1
	if { [catch { set fd [open $filename r] } ] } {
	    set fd ""
	} else {
	    bind $text <Destroy> "catch [list close $fd]"
	    $text del 1.0 end
	}
    } else { set JustOpen 0 }

    if { $fd != "" } {
	set textinserted 0
	set aa [gets $fd]
	while { ![fblocked $fd] && ![eof $fd]} {
	    set textinserted 1
	    $text ins end "$aa\n"
	    set aa [gets $fd]
	    if { $tcl_platform(platform) != "windows"} {
		if { ![info exists TkTailPriv(tempstopupdates)] || $TkTailPriv(tempstopupdates) == 0  } {
		    $text see end
		}
	    }
	}
	if { ($tcl_platform(platform) == "windows") && ($textinserted == 1)} {
	    if { ![info exists TkTailPriv(tempstopupdates)] || $TkTailPriv(tempstopupdates) == 0  } {
		$text see end
	    }
	}
	if { $JustOpen && !$textinserted } {
	    close $fd
	    set fd ""
	} else {
	    update idletasks
	}
    }
    if { $actualizar} {
	after 100 [list TkTailFill $text $fd $filename ]
    }
}

proc TkTail { filename { basename "" } { actualizar 1} } {
    global TkTailPriv

    set title "Output for '$filename'"

    set inum 1
    while { [winfo exists ._outputview$inum] } { 
	if { [wm title ._outputview$inum] == $title } {
	    raise ._outputview$inum
	    return
	}
	incr inum 
    }

    set w ._outputview$inum

    if { ![winfo exists $w] } {
	toplevel $w
	wm title $w $title

	font create FixedFont -family Courier -size 10

	text $w.t -width 81 -xscrollcommand "$w.scrollbar#1 set" \
		-yscrollcommand "$w.scrollbar#2 set" -font FixedFont \
		-bd 1

	scrollbar $w.scrollbar#2 -command "$w.t yview" -orient v \
		-bd 1 -elementborderwidth 1
	scrollbar $w.scrollbar#1 -command "$w.t xview" -orient h \
		-bd 1 -elementborderwidth 1

	bind $w.scrollbar#2 <ButtonPress-1> "set TkTailPriv(tempstopupdates) 1"
	bind $w.scrollbar#1 <ButtonPress-1> "set TkTailPriv(tempstopupdates) 1"
	bind $w.scrollbar#2 <ButtonRelease-1> "set TkTailPriv(tempstopupdates) 0"
	bind $w.scrollbar#1 <ButtonRelease-1> "set TkTailPriv(tempstopupdates) 0"

	frame $w.buts
	button $w.close -text "Close" -width 6 -command "destroy $w" -bd 1
	pack $w.close -in $w.buts
	focus $w.close
	grid $w.t -row 1 -column 1 -sticky nesw
	grid $w.scrollbar#2 -row 1 -column 2 -sticky ns
	grid $w.scrollbar#1 -row 2 -column 1 -sticky ew
	grid $w.buts -row 3 -column 1 -rowspan 2 -pady 5
	grid rowconfigure $w 1 -weight 1 
	grid columnconfigure $w 1 -weight 1 
	
	wm withdraw $w
	update idletasks

	set rw [ winfo reqwidth $w]
	set rh [ winfo reqheight $w]
	set sw [ winfo screenwidth $w]
	set sh [ winfo screenheight $w]
	set px [ expr ( $sw - $rw) / 2]
	set py [ expr ( $sh - $rh) / 2]

	wm geometry $w +${px}+${py}
	wm deiconify $w

    }
    TkTailFill $w.t "" $filename $actualizar

    return $w
}


if { $argc != 1} {
    puts "Usage: $argv0 filename"
    exit
}


wm withdraw .

set wtail [ TkTail [ lindex $argv 0]]
tkwait window $wtail

destroy .