#!/usr/bin/wish -f

# DFISH - Daniel's File Selection Hack

if { $argc == 2 } {
set glob_mask *
} elseif { $argc == 3 } {
set glob_mask [lindex $argv 2]
} else {
puts {usage: dfish path command [glob_mask]}
exit 0
}

set path [lindex $argv 0]
set command [lindex $argv 1]
set files {}

if { ! [file isdirectory $path] } {
puts stderr "dfish: error: $path : not a directory"
exit 1
}

wm title . "$command"

listbox .b -listvariable files -yscrollcommand [list .s set]
bind .b <Double-Button-1> {act}
scrollbar .s -command [list .b yview]
button .r -text "Refresh" -command {refresh}

proc act {} {
global .b path command
set f [.b get [.b curselection]]
set f "$path/$f"
set cmdline "$command "
eval exec $cmdline \"$f\" &   
}

proc refresh {} {
global files path glob_mask
set files [lsort [glob -tails -directory $path $glob_mask]]
}

pack .r -side bottom
pack .s -side right -fill y
pack .b -side top -fill both -expand 1
refresh
