#! /usr/local/bin/wish

#
#  Format:
#
#    installTools [home]
#
#  or
#
#    installTools [arch ...]
#
#  The first form adds commands to the user's ~/.geomview file that
#  make the Stage tools available in the Geomview modules selector of
#  the Geomview main panel.
#  
#  The second form makes a .geomview-StageTools file in the
#  Geomview/modules/<arch> directories for each architecture specified
#  on the command line.  This file is a Geomview initialization file
#  that will be read automatically at startup and will make the Stage
#  tools available in the Geomview modules selector for all Geomview
#  users.  If no architectures are specified, then a .geomview-StageTools
#  file is made in each subdirectory of Geomview/modules.  The
#  directory containing the installTools command must be a
#  subdirectory of the Geomview/modules/tcl directory for this second
#  form of the command.
#
#  The list of modules to be installed is given below.  Comment out
#  any modules you don't want to appear in the Geomview modules selector.
#  Note that StageDoor must be present if you want StageManager.
#

wm withdraw .
update idletasks


################################################################
#
#  comment out any modules you don't want installed
#

set module(CenterStage)  "CenterStage"
set module(StageStills)  "StageStills"
set module(StageManager) "StageManager"
set module(StageDoor)	 "module/StageDoor/StageDoor geomview"
set module(StageHand)    "StageHand"

#
################################################################


cd [file dir $argv0]

proc main {argv} {
  global argv0
  if {$argv == "home"} {InstallHome} else {
    set pwd [exec pwd]
    if ![regexp {/Geomview/modules/tcl$} [file dir $pwd]] {
      set stage [file tail $pwd]
      puts stderr \
        "Error:  the '$stage' directory should be in 'Geomview/modules/tcl'"
      puts stderr "  or use '$argv0 home' to install a private copy."
      exit 1
    }
    InstallArch $pwd $argv
  }
}

proc InstallHome {} {
  global env
  set file "$env(HOME)/.geomview"
  if [file exists $file] {puts "Appending to '$file'"} \
    else {puts "Installing '$file'"}
  set init [open $file a]
  puts $init [geomview-StageTools [exec pwd]]
  close $init
}

proc InstallArch {pwd list} {
  set stage {$GEOMROOT/modules/tcl/}
  append stage [file tail $pwd]
  set progn [geomview-StageTools $stage]
  if {$list == ""} {
    foreach dir [lsort [glob ../../*]] {
      if {[file isdirectory $dir] && $dir != "../../tcl"} \
        {lappend list [file tail $dir]}
    }
  }
  foreach dir $list {
    if {$dir == "tcl"} {
      puts "Warning:  can't install in the 'tcl' directory"
    } elseif [file isdirectory ../../$dir] {
      puts "Installing in '$dir'"
      set init [open "../../$dir/.geomview-StageTools" w]
      puts $init $progn
      close $init
    } else {
      puts "Warning:  directory 'Geomview/modules/$dir' doesn't exist"
    }
  }
}

proc geomview-StageTools {dir} {
  global module
  set progn "(progn"
  foreach cmd [lsort [array names module]] \
    {lappend progn "  (emodule-define \"$cmd\" \"$dir/$module($cmd)\")"}
  if {[string index $dir 0] == "/"} {lappend progn "  (emodule-sort)"}
  lappend progn ")"
  return [join $progn \n]
}

main $argv
exit
