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

if {[info exists env(AQTOOLS_LIB)]} {
  set aq_library $env(AQTOOLS_LIB)
  set aq_pkg [file join $env(AQTOOLS_LIB) pkg]
} else {
  set aq_library /usr/lib/aq
  set aq_pkg [file join $aq_library pkg]
}

# add the aq library to the library search path:

set auto_path  [concat  [list $aq_pkg]  [list $aq_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 aq libraries.

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

## end boiler_header

wm withdraw .

::aq::aq_init aqtail

set t [::aq::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
}

