#! /usr/local/bin/wish

wm withdraw .
update idletasks

set filelist {
  lib/shell.tcl
  module/BackStage/BackStage
  module/CenterStage/CenterStage
  module/OpenDoor/OpenDoor
  module/StageDoor/StageDoor
  module/StageManager/StageManager
  module/StageStills/StageStills
  module/StageHand/StageHand
  installTools
  fix
}

proc which {name} {
  global env
  if [info exists env(PATH)] {
    foreach dir [split $env(PATH) :] \
	{if [file executable "$dir/$name"] {return "$dir/$name"}}
  }
  return ""
}

set file [open $argv0 r]
gets $file wish
close $file
regsub {#! *} $wish {} wish
if ![file executable $wish] {
  set wish [which wish]
  if {$wish == ""} {
    puts -nonewline stdout "location of 'wish' command: "
    flush stdout
    gets stdin wish
  }
  regsub "\[ \n\]" $wish {} wish
}

puts "------------------------------------------------------"
puts "Using command: $wish\n"

foreach name $filelist {
  puts "Fixing: $name"
  if [catch {
    set file [open $name r]
    set lines [lrange [split [read $file] \n] 1 end]
    close $file
    set file [open $name w]
    puts $file "\#! $wish"
    puts -nonewline $file [join $lines \n]
    close $file
  } message] {puts stderr "Warning:  can't fix file '$name' ($message)"}
}

puts "\n------------------------------------------------------"

exit
