#!/afs/ece/usr/tcl/bin/wish -f
# The next line is executed by most shells, but not Tcl \
wish $0 $*

# This program takes Elsbeth parameters, and calls Elsbeth with them. Stays
# active until all Elsbeth windows with these parameters are closed, then quits.

wm withdraw .
set me [tk appname]

# Find an active Elsbeth session, or start one
set new 1
foreach app [winfo interps] {
  if {$app == $me} {continue}
  if {![string match "elsbet*" $app]} {continue}
  if {[catch {send $app winfo children .} windows]} {continue}
  set new 0 ; break
}
if $new {eval exec "[file dirname [info script]]/elsbeth" -a $argv ; exit}

# Send everything to old app, quit when old app destroys new windows
set pwd [send $app "pwd"]
send $app "cd [pwd]"
send $app els_parse_configs $argc \{$argv\}
send $app "cd $pwd"

foreach new_window [lrange [send $app winfo children .] [llength $windows] end] {
  frame $new_window
  send $app bind "$new_window.t" <Destroy> \{catch \{send \{$me\} destroy $new_window\}\}
}
while {[winfo children .] != ""} {
 tkwait window [lindex [winfo children .] 0]
}
exit
