#!/usr/local/bin/wish
global env gv

set gv(version)		"Log View (Version 1.1 14may96)"
set gv(program)		logview
set gv(iconname)	[string toupper $gv(program)]
set gv(top)		.$gv(program)
set gv(author)		"John van Gulik"

proc errmess { pw {msg ""} } {
	global env gv
	pack [label .errmess -text $msg] -side top -expand true -fill both
	pack [button .ok -text "OK" -command "destroy .ok"] -side top
	tkwait window .ok
}

##############################################################################
#####	Set the required environment variables
##############################################################################

if {[info exists env(HOME)] && $env(HOME)=="/"} {set env(HOME) "/root"}

if {$tcl_platform(platform)=="windows"} {
	if {![info exists env(TKAPPS)]} {
		set env(TKAPPS) /TKAPPS
	}
	if {![file isdirectory $env(TKAPPS)]} {
		errmess . "Directory $env(TKAPPS) not found"
		exit 1
	}
} elseif {$tcl_platform(platform)=="unix"} {
	if {![info exists env(TKAPPS)]} {
		if {[file isdirectory $env(HOME)/TKAPPS]} {
			set env(TKAPPS) $env(HOME)/TKAPPS
		} elseif {[file isdirectory /opt/TKAPPS]} {
			set env(TKAPPS) /opt/TKAPPS
		} elseif {[file isdirectory /apps/TKAPPS]} {
			set env(TKAPPS) /apps/TKAPPS
		} else {
			set env(TKAPPS) /usr/local/TKAPPS
		}
	}
	if {![file isdirectory $env(TKAPPS)]} {
		errmess . "Directory $env(TKAPPS) not found"
		exit 1
	}
} else {
	errmess . "This application is not supported under $tcl_platform(platform)"
	exit 1
}

set auto_path [linsert $auto_path 0 $env(TKAPPS)/lib]

wm withdraw .

standard_startup $gv(program) $gv(version)
if {[winfo exists .startup]} {destroy .startup}
if {[info exists gv(startup.id)]} {image delete $gv(startup.id)}

##############################################################################
#####	Set required variables and options
##############################################################################

set gv(status)		""
set gv(filename)	""
set gv(filter)		""

##############################################################################
#####	SUBROUTINES
##############################################################################

proc fileselect { filename } {
	global env gv

	set gv(filename) $filename
}

proc filegrep { } {
	global env gv

	if {[glob -nocomplain $gv(filename)]==""} {return}

	$gv(top).view.list delete 0 end
	foreach file [lsort [glob -nocomplain $gv(filename)]] {
		set gv(status) "...searching file $file"
		update idletasks
		foreach line [f_grep $gv(filter) $file] {
			if {"x$file"!="x$gv(filename)"} {
				set line "$file:[expand $line]"
			} else {
				set line [expand $line]
			}
			$gv(top).view.list insert end $line
		}
	}
	set gv(status) "Done"
}

##############################################################################
#####	MAIN PROGRAM
##############################################################################

toplevel	$gv(top)
wm iconname	$gv(top) $gv(iconname)
wm iconbitmap	$gv(top) @$env(TKAPPS)/bitmaps/$gv(program).xbm
wm iconmask	$gv(top) @$env(TKAPPS)/bitmaps/$gv(program).msk
wm title	$gv(top) $gv(version)
wm minsize	$gv(top) 100 100
wm protocol	$gv(top) WM_DELETE_WINDOW exit

pack [label $gv(top).help] \
	-side bottom -fill x
bind_help $gv(top).help $gv(top).help \
	[lang "Help text" "Texte d'aide"]

pack [frame $gv(top).b1] \
	-side top -fill x
pack [menubutton $gv(top).b1.file -menu $gv(top).b1.file.m \
	-text [lang "File..." "Fichier..."] ] \
        -side left -fill y
pack [label $gv(top).b1.status -textvariable gv(status)] \
	-side left -expand true -fill x
pack [button $gv(top).b1.iconify -text [lang "Iconify" "Minimiser"] \
	-command "wm iconify $gv(top)"] \
        -side left
pack [button $gv(top).b1.exit -text [lang "Exit" "Abandonner"] \
	-command "exit"] \
	-side left

bind_help $gv(top).b1.file $gv(top).help \
        [lang "Standard file menu" "Menu standard du fichier"]
bind_help $gv(top).b1.status $gv(top).help \
        [lang "Status messages" "Messages d'tat"]
bind_help $gv(top).b1.iconify $gv(top).help \
        [lang "Iconify window" "Minimiser cette fentre"]
bind_help $gv(top).b1.exit $gv(top).help \
        [lang "Close this window" "Fermer cette fentre"]

menu $gv(top).b1.file.m
$gv(top).b1.file.m add command -label [lang "Help" "Aide"] \
	-command "help $env(TKAPPS)/help/$gv(program)"
$gv(top).b1.file.m add command -label [lang "Fonts/Colors" "Fontes/Couleurs"] \
	-command "background tkcustom $gv(program)"
$gv(top).b1.file.m add command -label [lang "Iconify" "Minimiser"] \
	-command "wm iconify $gv(top)"
$gv(top).b1.file.m add command -label [lang "Exit" "Abandonner"] \
	-command "exit"

pack [frame $gv(top).b2] \
	-side top -fill x
pack [button $gv(top).b2.select -text [lang "Select File:" "FRENCH"] \
	-command "prompt -directory \[pwd\] -command \"set gv(filename)\"" ] \
	-side left 
pack [entry $gv(top).b2.file -relief sunken -textvariable gv(filename)] \
	-side left -expand true -fill x 
pack [button $gv(top).b2.fbut -text [lang "Filter:" "FRENCH"] \
	-command "filegrep"] \
	-side left 
pack [entry $gv(top).b2.filter -relief sunken -textvariable gv(filter)] \
	-side left -expand true -fill x 

bind $gv(top).b2.file <Tab> {focus $gv(top).b2.filter}
bind $gv(top).b2.file <Shift-Tab> {focus $gv(top).b2.filter}
bind $gv(top).b2.file <Return> {filegrep}

bind $gv(top).b2.filter <Tab> {focus $gv(top).b2.file}
bind $gv(top).b2.filter <Shift-Tab> {focus $gv(top).b2.file}
bind $gv(top).b2.filter <Return> {filegrep}

pack [frame $gv(top).view] \
	-side top -expand true -fill both 
pack [scrollbar $gv(top).view.yscroll -command "$gv(top).view.list yview"] \
	-side left -fill y
pack [scrollbar $gv(top).view.xscroll -command "$gv(top).view.list xview" \
	-orient horizontal] \
	-side bottom -fill x
pack [listbox $gv(top).view.list -bd 2 -relief sunken -width 80 -height 30 \
	-yscroll "$gv(top).view.yscroll set" \
	-xscroll "$gv(top).view.xscroll set" ] \
	-expand true -fill both 
 
