#!/usr/bin/wish8.0
# jtail - GUI equivalent to "tail -f"
# mostly stolen from Jan Wieck <wieck@sapserv.debis.de>
#
## begin boiler_header

if {[info exists env(JSTOOLS_LIB)]} {
  set jstools_library $env(JSTOOLS_LIB)
  set jstools_pkg [file join $env(JSTOOLS_LIB) pkg]
} else {
  set jstools_library /usr/lib/jstools
  set jstools_pkg [file join $jstools_library pkg]
}

# add the jstools library to the library search path:

set auto_path  [concat  [list $jstools_pkg]  [list $jstools_library]  $auto_path]

# check for ~/.tk and prepend it to the auto_path if it exists.
# that way the user can override and customise the jstools libraries.

if {[file isdirectory ~/.tk]} then {
  set auto_path [concat [list [glob ~/.tk]] $auto_path]
}

## end boiler_header

wm withdraw .

j:jstools_init jtail

set t [j:more]

bind $t <Destroy> {exit 0}

fconfigure stdin -blocking no -buffering line
fileevent  stdin readable [list data_from_stdin $t]

######################################################################

proc data_from_stdin {w} {
  $w configure -state normal
  if {[gets stdin line] < 0} {
    close stdin
    set loc [$w index end-1char]
    $w insert end "<EOF>"
    $w tag add eof $loc end-1char
    $w tag configure eof -font 5x7
  } else {
    $w insert end "$line\n"
  }
  $w configure -state disabled
}

