#!/itl/links/generic/bin/wish
# ^^^^^^^^^^^^^^^^^^^^^^ <-- Change this path for location 
#                            of expectk at your site
#
#  Modify the first line to indicate the correct pathname of wish.
#  Some systems will misbehave in confusing ways if the first
#  line of the script file is longer than 32 characters, so beware if the full
#  path name of the wish binary is longer than 27 characters.
#
#  For PADE to run correctly, the PADE_ROOT variable must be set with the
#  pathname of the location of the PADE distribution.
#
#------------------------------------------------------------------------------
#
#  NIST Parallel Applications Development Environment (PADE)
#
#  PADE written and maintained by:
#
#  Robert Lipman
#  National Institute of Standards and Technology
#  Mathematical and Computational Sciences Division
#  Building 820, Room 365
#  Gaithersburg, MD  20899
#
#  (301) 975-3829
#  robert.lipman@nist.gov
#  http://www.itl.nist.gov/div895/pade/pade.html
# 
#  PADE originally written by: Justin Turner
#
#------------------------------------------------------------------------------

set mintcl 8.0
if {[info tclversion] < $mintcl} {
   puts "\nWrong version of Tcl"
   puts "Found Tcl [info tclversion], expecting at least Tcl $mintcl"
   exit
}

set version "1.4.1"
global version
puts "\nWelcome to PADE ($version)"

global pvmmakefile pvmmakelab hostfile hosttemp padefile uname fkeys
global env penv pcmd
global localpath remotepath
global entry1 entry2
global pd pdsave pdopen pdname 
global nwin tmpfil ntmpfil
global ntcol ntrow
global mancmd mandex manfil 
global napp appdex lastcmd lastrep
global prefs nundo oldmode aff_fromw af_index af
global colors fonts apps pause_cmd apps

#------------------------------------------------------------------------------
#  INITIALIZE

set status     0
set nwin       0
set ntmpfil    0
set napp       0
set ntcol      4
set ntrow      16
set localpath  ""
set remotepath ""
set padefile   ""
set hostfile   ""
set hostfilelab [file tail $hostfile]
set hosttemp   ""
set pvmmakefile ""
set pvmmakelab  [file tail $pvmmakefile]
set edfile     ""
set entry1     ""
set entry2     ""
set nundo      1
set noweb      1
set pdname {hosts cmnds hslct files fslct}
foreach item $pdname {
   set pd($item) ""
   set pdopen($item) ""
   set pdsave($item,$nundo) ""
}
set oldmode    ""
set aff_fromw  ""
set af_index   ""
set apps       ""
set lastcmd    ""
set lastrep    ""
set fkeys(host) "host:"
set fkeys(cmnd) "compile:"
set fkeys(lfile) "file:"
set fkeys(rfile) "remotefile:"

#------------------------------------------------------------------------------
#  CHECK ENVIRONMENT VARIABLES

puts "Checking environment variables"
set env_name \
   {PADE_ROOT PVM_ROOT PVM_ARCH XPVM_ROOT EDITOR HOME PATH SHELL PWD}
foreach j $env_name {
   set penv($j) ""
   foreach i [array name env] {
      if {($i == $j)} {set penv($j) $env($i)}
   }
   if {$penv($j) == "" && $j != "EDITOR"} {
      puts " $j environment variable not found"
   }
}
if {$penv(PADE_ROOT) == ""} {
   puts "\nPADE can be run only if the PADE_ROOT environment variable is set\n"
   exit
}
set auto_path [linsert $auto_path 0 $env(PADE_ROOT)/pade]

if {$penv(PVM_ROOT) == ""} {puts " PVM_ROOT needs to be set to run PVM"}
if {$penv(PVM_ARCH) == ""} {puts " PVM_ARCH might need to be set to run PVM"}
if {$penv(XPVM_ROOT) == ""} {puts " XPVM_ROOT might need to be set to run XPVM"}
if {$penv(PATH) == ""} {puts " PATH needs to be set to check for commands"}
if {$penv(HOME) == ""} {puts " HOME needs to be set to read preferences file"}

if {$penv(SHELL) != ""} {
   set pause_cmd $penv(SHELL)
} else {
   set pause_cmd "sleep 3600"
}

#------------------------------------------------------------------------------
#  CHECK COMMANDS

puts "Checking commands"
set cmd_name {pvm pvmd3 xpvm pvmmake targets pvmstat pvmtidy\
man col mail uname}
foreach j $cmd_name {
   set pcmd($j) [FindCmd $j]
}

if {![FindCmd $penv(EDITOR)]} {set penv(EDITOR) ""}

#------------------------------------------------------------------------------
#  SET AND READ PREFERENCES FILE

#  default preferences and values
set prefs(CONFIRM)      "On"
set prefs(WEB)          "netscape"
set prefs(TERM)         "xterm"
set prefs(FILE_PATH)    $penv(HOME)
set prefs(LAST_PVMMAKE) [TimeStamp]

set preffile $penv(HOME)/.paderc
if {[file exists $preffile]} {
   puts "Reading preferences file"
   set f_pref [open "$preffile" r]
   set notdone [gets $f_pref line]
   while {$notdone >= 0} {
      set firstchar [string index $line 0]
      if {$firstchar != "#" && $firstchar != " "} {
         set pref [lrange $line 1 end] 
	 if {$pref != ""} {set prefs([lindex $line 0]) $pref}
	 if {[lindex $line 0] == "TERM"} {
            if {![FindCmd [lindex $line 1]] } {
               set prefs(TERM) "xterm"
            }
	 }
	 if {[lindex $line 0] == "WEB"} {
            if {[FindCmd [lindex $line 1]]} {set noweb 0}
	 }
	 if {[string range $line 0 2] == "APP"} {
	    incr napp
	    set apps [lappend apps [lrange $line 1 end]]
	 }
      }
      set notdone [gets $f_pref line]
   }
   close $f_pref
} else {
   puts "Creating preferences file ~/.paderc"
   SavePrefs
}
   
#------------------------------------------------------------------------------
#  COL COMMAND IS USED FOR LISTING MAN PAGES PROPERLY

set uname ""
if {$pcmd(uname)} {set uname [exec uname]}

if {$pcmd(col)} {
   if {$penv(PVM_ARCH) == "SGI5" || $penv(PVM_ARCH) == "SGIMP" || \
      $penv(PVM_ARCH) == "RS6K" || $penv(PVM_ARCH) == "CRAY" || 
      $uname == "IRIX" || $uname == "AIX"} {
      set colcmd "| col -b -x"
   } else {
      set colcmd "| col -b"
   }
} else {
  set colcmd ""
}

#------------------------------------------------------------------------------
#  SET COLORS

if {[winfo depth .] > 2} { 
   set colors(COLOR_BACK)     #ffe4c4 ;# bisque 
   set colors(COLOR_BACK_ACT) #eed5b7 ;# bisque2 
   set colors(TEXT)           red
} else {
   set colors(COLOR_BACK)     white
   set colors(COLOR_BACK_ACT) black
   set colors(COLOR_FORE)     black
   set colors(COLOR_FORE_ACT) white
   set colors(TEXT)           black
}

#  SET FONTS

set fonts(FONT_BIG)   {helvetica 21 normal}
set fonts(FONT_FIXED) {screen 13 bold}
set fonts(FONT_SLANT) {helvetica 11 bold italic}

#------------------------------------------------------------------------------
#  MAIN WINDOW

wm title .    "PADE - Release $version"
wm iconname . "PADE $version"
wm geometry . +110+98
wm iconbitmap . @$env(PADE_ROOT)/pade/pade_icon.xbm

frame .f1 -bd 1 -relief raised
frame .f1.l
frame .f1.r
frame .f2 -bd 1 -relief raised
frame .f3 -bd 1 -relief raised
frame .f4 -bd 1 -relief raised
frame .f4.l
frame .f4.r
frame .f5 -bd 1 -relief raised

label .f1.l.nistbit -bitmap @$env(PADE_ROOT)/pade/nist.xbm
label .f1.l.padebit -bitmap @$env(PADE_ROOT)/pade/pade.xbm
label .f1.r.label1 -text "Parallel Applications"
label .f1.r.label2 -text "Development Environment"

pack  .f1.r.label1 .f1.r.label2 -side top -padx 2m -pady 2m
pack  .f1.l.nistbit .f1.l.padebit -side left -padx 2m -pady 2m -expand 1
pack  .f1.l .f1.r -side left -padx 2m -pady 2m -fill x -expand 1

label .f3.msg1 -text "     Host list"
label .f3.msg2 -text "     Host commands"
pack  .f3.msg1 .f3.msg2 -expand 1 -pady 3m -side left

#-------------------------------------------------------------------------------
#  scroll box

listbox .f4.l.listbox -yscroll [list .f4.l.yscroll set] \
   -xscroll [list .f4.l.xscroll set] -relief sunken -exportselection no
  
scrollbar .f4.l.yscroll -relief sunken \
   -command [list ScrollBoth [list .f4.l.listbox .f4.r.listbox]]

scrollbar .f4.l.xscroll -relief sunken -command [list .f4.l.listbox xview] \
   -orient horiz

listbox .f4.r.listbox -yscroll [list .f4.l.yscroll set] \
   -xscroll [list .f4.r.xscroll set] -relief sunken -exportselection no
  
scrollbar .f4.r.xscroll -relief sunken -command [list .f4.r.listbox xview] \
   -orient horiz

pack .f4.l.xscroll -side bottom -fill x
pack .f4.l.yscroll -side left -fill y
pack .f4.l.listbox -expand 1 -fill both -ipady 12m

pack .f4.r.xscroll -side bottom -fill x
pack .f4.r.listbox -expand 1 -fill both -ipady 12m
pack .f4.l .f4.r -side left -expand 1 -fill both

bind .f4.r.listbox <Any-ButtonPress> {break}
bind .f4.r.listbox <Any-ButtonRelease> {break}
bind .f4.r.listbox <B1-Motion> {break}
bind .f4.l.listbox <B2-Motion> {break}
bind .f4.r.listbox <B2-Motion> {break}

#------------------------------------------------------------------------------

text	.f5.text -height 3 -fg $colors(TEXT) -width 1 -state disabled -relief flat
pack    .f5.text -fill x -expand 1

TextWrite .f5.text "Welcome to PADE ($version)"

#------------------------------------------------------------------------------
#  FILE

menubutton .f2.file -text "File" -menu .f2.file.menu
menu .f2.file.menu

.f2.file.menu add command -label "New" -acc "Ctrl-n" -command {PadeNew}

.f2.file.menu add command -label "Open..." -acc "Ctrl-o" -command {PadeOpen}

.f2.file.menu add command -label "Save" -acc "Ctrl-s" -command {PadeSave}


.f2.file.menu add command -label "Save As..." -command {
   SavePadeFile 1
   PvmmakeButtons
}

.f2.file.menu add separator
.f2.file.menu add cascade -label "Preferences" -menu .f2.file.menu.prefs
menu .f2.file.menu.prefs -tearoff 0

PrefsMenu

.f2.file.menu add separator
.f2.file.menu add command -label "Exit" -acc "Ctrl-q" -command {PadeExit}

#------------------------------------------------------------------------------
#  Setup

menubutton .f2.setup -text "Setup" -menu .f2.setup.menu
menu .f2.setup.menu

Setup

#-------------------------------------------------------------------------------
#  UNDO

button .f2.undo -text "Undo" -relief flat -command {
   .f4.l.listbox selection clear 0 end
   .f4.r.listbox selection clear 0 end
   TextWrite .f5.text ""
   if {$nundo > 0} {
      DecrUndo
      HostListboxWrite
      FilesListboxWrite
      GraphUpdate
      UpdateButtons
   }
}

#-------------------------------------------------------------------------------
#  SORT ALL

button .f2.sort -text "Sort" -relief flat -command {SortAll .}

#-------------------------------------------------------------------------------
#  GRAPHICAL DISPLAY

button .f2.graph -text "Graph" -relief flat -command {
   .f4.l.listbox selection clear 0 end
   TextWrite .f5.text ""
   GraphWin
   HostListboxWrite
   UpdateButtons
}

#------------------------------------------------------------------------------
#  PVM 

menubutton .f2.pvm -text "PVM" -menu .f2.pvm.menu
menu .f2.pvm.menu

#------------------------------------------------------------------------------
#  PVM CONSOLE

.f2.pvm.menu add command -label "PVM console" -command {
   TextWrite .f5.text ""
   if {$hostfile == ""} {
       set pick [Dialog .d {PADE Question} \
          {No hostfile has been set.  The PVM console (pvm)\
          can be started with or without a hostfile.} questhead 1 \
          {Edit Hostfile} {Set Hostfile and Start} {Start Without Hostfile} \
          {Cancel}]
      if {$pick==0} {
         EditFile $hostfile
         set hosttemp $edfile
	 TextWrite .f5.text \
	    "PVM console -> Edit Hostfile does not start the PVM console"
      } elseif {$pick==1} {
         set hosttemp [fileselect "Pick a hostfile for PVM" $prefs(FILE_PATH)/]
      } elseif {$pick==2} {
         set hosttemp ""
      }
   } else {
      set pick 2
      set hosttemp $hostfile
   }
   if {[SetHostFile]} {
      if {($hostfile == "" || [file isfile $hostfile]) && ($pick==1 || $pick==2)} {
	 exec $prefs(TERM) -geometry 80x30+170+218 -sl 1000 \
             -title "PVM console-[file tail $hostfile]" -e pvm $hostfile &
      }
   }
}

#------------------------------------------------------------------------------
#  PVMD3 DAEMON

.f2.pvm.menu add command -label "PVM daemon" -command {
   TextWrite .f5.text ""
   if {$hostfile == ""} {
      set pick [Dialog .d {PADE Question} \
         {No hostfile has been set.  The PVM daemon (pvmd3)\
         can be started with or without a hostfile.} questhead 1 \
         {Edit Hostfile} {Set Hostfile and Start} {Start Without Hostfile} \
         {Cancel}]
      if {$pick==0} {
         EditFile $hostfile
         set hosttemp $edfile
	 TextWrite .f5.text \
	    "PVM daemon -> Edit Hostfile does not start the PVM daemon"
      } elseif {$pick==1} {
         set hosttemp [fileselect "Pick a hostfile for PVMD3" $prefs(FILE_PATH)/]
      } elseif {$pick==2} {
         set hosttemp ""
      }
   } else {
      set pick 2
      set hosttemp $hostfile
   }
   if {[SetHostFile] || $hostfile == ""} {
      if {($hostfile == "" || [file isfile $hostfile]) && ($pick==1 || $pick==2)} {
	 TextWrite .f5.text "PVMD3 output is sent to the shell window"
	 exec pvmd3 $hostfile &
      }   
   }   
}

#------------------------------------------------------------------------------
#  XPVM

.f2.pvm.menu add command -label "xpvm" -command {
   TextWrite .f5.text ""
   set exist [file exists ~/.xpvm_hosts]
   if {$exist} {
      set pick 1
   } else {
      set pick [Dialog .d {PADE Question} \
         {No ~/.xpvm_hosts file exists.  XPVM can be started\
         with or without a ~/.xpvm_hosts file} questhead 1 \
         {Edit ~/.xpvm_hosts} {Start Without ~/.xpvm_hosts} {Cancel}]
      if {$pick==0} {
         if {$env(HOME) != ""} {
            EditFile $env(HOME)/.xpvm_hosts
         } else {
            EditFile ".xpvm_hosts"
         }
      }
   }
   if {$pick==1} {
      TextWrite .f5.text "Some XPVM output is sent to the shell window"
      exec xpvm &
   }
}

#------------------------------------------------------------------------------
#  PVMSTAT

.f2.pvm.menu add separator
.f2.pvm.menu add command -label "pvmstat" -command {
   TextWrite .f5.text "Starting pvmstat"
   . config -cursor {watch red white}
   update idletasks
   catch {exec pvmstat >& /tmp/pade.pvmstat_output}
   . config -cursor {arrow red white}
   ListFile /tmp/pade.pvmstat_output "Output of pvmstat"
   catch [file delete /tmp/pade.pvmstat_output]
}

#------------------------------------------------------------------------------
#  PVMTIDY

.f2.pvm.menu add command -label "pvmtidy" -command {
   TextWrite .f5.text ""
   if {$hostfile == ""} {
      set pick [Dialog .d {PADE Question} \
         {No hostfile has been set.  A Hostfile must be set to run PVMTIDY.} \
         questhead 0 {Set Hostfile} {Edit Hostfile} {Cancel}]
      if {$pick==0} {
         set hosttemp [fileselect "Pick a hostfile for PVMTIDY" $prefs(FILE_PATH)/]
      } elseif {$pick==1} {
         EditFile $hostfile
         set hosttemp $edfile
	 TextWrite .f5.text \
	    "pvmtidy -> Edit Hostfile does not start pvmtidy"
      }
   } else {
      set pick 0
      set hosttemp $hostfile
   }
   if {[SetHostFile]} {
      if {[file isfile $hostfile] && $pick==0} {
         TextWrite .f5.text "Starting pvmtidy"
	 exec $prefs(TERM) -geometry 80x30+170+218 -sl 1000 -title "PVMTIDY" \
             -e sh -c "pvmtidy $hostfile; $pause_cmd" &
      }
   }
}

#------------------------------------------------------------------------------
#  SELECT HOSTFILE

.f2.pvm.menu add separator
.f2.pvm.menu add command -label "Pick Hostfile..." -command {
   TextWrite .f5.text ""
   set hosttemp [fileselect "Pick a hostfile" $prefs(FILE_PATH)/]
   SetHostFile
}

#------------------------------------------------------------------------------
#  EDIT HOSTFILE

.f2.pvm.menu add command -label "Edit Hostfile..." -command {
   TextWrite .f5.text ""
   EditFile $hostfile
   set hosttemp $edfile
   SetHostFile
}

#------------------------------------------------------------------------------
#  SAVE HOST LIST TO HOSTFILE

.f2.pvm.menu add command -label "Write Host list to Hostfile..." -command {
   TextWrite .f5.text ""
   set hosttemp [fileselect "Write Host list to Hostfile" $prefs(FILE_PATH)/ 0]
   if {$hosttemp != "" && ![file isdirectory $hosttemp]} {
      if {[file exists $hosttemp]} {
         set pick [Dialog .d {PADE Question} \
           "Overwrite  $hosttemp ?" questhead 0 \
            {Overwrite} {Cancel}]
         if {$pick} return
      }
      set hostfile $hosttemp
      set f_hostfile [open "$hostfile" w]
      set i 0
      foreach item $pd(hosts) {puts $f_hostfile "$item"}
      close $f_hostfile
      TextWrite .f5.text "Wrote Host list to-$hostfile"
   } elseif {$hosttemp != ""} {
       TextWrite .f5.text "$hosttemp IS NOT A FILE"
   }
   PVMButtons
}

#------------------------------------------------------------------------------
#  LIST HOSTFILE

.f2.pvm.menu add command -label "List Hostfile" -command {
   TextWrite .f5.text ""
   ListFile $hostfile "Current PVM Hostfile  $hostfile"
}

#------------------------------------------------------------------------------
#  PVMMAKE

menubutton .f2.pvmmake -text "PVMmake" -menu .f2.pvmmake.menu
menu .f2.pvmmake.menu

#------------------------------------------------------------------------------
#  PVMMAKE CONFIG FILE

.f2.pvmmake.menu add command -label "Set Files and Commands to Save" \
   -command {
   TextWrite .f5.text ""
   SelSavePvmmakeFile
   PvmmakeButtons
}

.f2.pvmmake.menu add command -label "Save Config File as..." -command {
   TextWrite .f5.text ""
   SavePvmmakeFile ""
   PvmmakeButtons
}

.f2.pvmmake.menu add separator
.f2.pvmmake.menu add command -label "Pick Config File..." -command {
   TextWrite .f5.text ""
   if {$pcmd(pvmmake)} {
      set pvmmaketmp \
         [fileselect "Pick PVMmake config file" $prefs(FILE_PATH)/]
      if {$pvmmaketmp != "" && ![file isfile $pvmmaketmp]} {
	 TextWrite .f5.text "$pvmmaketmp IS NOT A FILE"
      } elseif {$pvmmaketmp != ""} {
	 set pvmmakefile $pvmmaketmp
      }
      PvmmakeButtons
   }
}

.f2.pvmmake.menu add command -label "Edit Config File..." -command {
   TextWrite .f5.text ""
   EditFile $pvmmakefile
   set pvmmaketmp $edfile
   if {$pvmmaketmp != "" && [file isfile $pvmmaketmp]} {
      set pvmmakefile $pvmmaketmp
   }
   PvmmakeButtons
}

.f2.pvmmake.menu add command -label "List Config File" -command {
   TextWrite .f5.text ""
   ListFile $pvmmakefile "Current Pvmmake Config File  $pvmmakefile"
}

#------------------------------------------------------------------------------
#  RUN PVMMAKE

.f2.pvmmake.menu add separator
.f2.pvmmake.menu add command -label "Run  (in $prefs(TERM))" -command {
   TextWrite .f5.text ""
   if {[file exists $pvmmakefile]} {
      TextWrite .f5.text "Starting PVMmake"
      set prefs(LAST_PVMMAKE) [TimeStamp]
      SavePrefs
      exec $prefs(TERM) -geometry 80x30+170+218 -sl 20000 \
	 -title PVMmake -e sh -c "pvmmake -f $pvmmakefile; $pause_cmd" &
   } else {
      TextWrite .f5.text "No PVMmake configuration file"
   }
}
.f2.pvmmake.menu add command -label "Run  (in PADE window)" -command {
   TextWrite .f5.text ""
   if {[file exists $pvmmakefile]} {
      TextWrite .f5.text "Starting PVMmake"
      . config -cursor {watch red white}
      update idletasks
      set prefs(LAST_PVMMAKE) [TimeStamp]
      SavePrefs
      catch \
         {exec pvmmake -f $pvmmakefile >& /tmp/pade.pvmmake_output}
      . config -cursor {arrow red white}
      ListFile /tmp/pade.pvmmake_output "Output of PVMmake"
      catch [file delete /tmp/pade.pvmmake_output]
   } else {
      TextWrite .f5.text "No PVMmake configuration file"
   }
}

.f2.pvmmake.menu add separator
.f2.pvmmake.menu add command \
   -label "Save Config File and Run  (in $prefs(TERM))" -command {
   TextWrite .f5.text ""
   if {[file exists $pvmmakefile]} {
      SavePvmmakeFile $pvmmakefile
      TextWrite .f5.text "Starting PVMmake"
      set prefs(LAST_PVMMAKE) [TimeStamp]
      SavePrefs
      exec $prefs(TERM) -geometry 80x30+170+218 -sl 20000 \
	 -title PVMmake -e sh -c "pvmmake -f $pvmmakefile; $pause_cmd" &
   } else {
      TextWrite .f5.text "No PVMmake configuration file"
   }
}
.f2.pvmmake.menu add command \
   -label "Save Config File and Run  (in PADE window)" -command {
   TextWrite .f5.text ""
   if {[file exists $pvmmakefile]} {
      SavePvmmakeFile $pvmmakefile
      TextWrite .f5.text "Starting PVMmake"
      . config -cursor {watch red white}
      update idletasks
      set prefs(LAST_PVMMAKE) [TimeStamp]
      SavePrefs
      catch \
         {exec pvmmake -f $pvmmakefile >& /tmp/pade.pvmmake_output}
      . config -cursor {arrow red white}
      ListFile /tmp/pade.pvmmake_output "Output of PVMmake"
      catch [file delete /tmp/pade.pvmmake_output]
   } else {
      TextWrite .f5.text "No PVMmake configuration file"
   }
}

#------------------------------------------------------------------------------
#  USER-DEFINED APPS

menubutton .f2.apps -text "Apps" -menu .f2.apps.menu
menu .f2.apps.menu -tearoff 0

#------------------------------------------------------------------------------
#  ADD APP

.f2.apps.menu add command -label "Add..." -command {
   TextWrite .f5.text ""
   EntryBox 170 218 {Enter application command:} "" 
   if {$entry1 != ""} {
      incr napp
      set apps [lappend apps $entry1]
      SavePrefs
      PadeButtons
      .f2.apps.menu.run add command -label $entry1 -command {AppRunButton}
      .f2.apps.menu.mod add command -label $entry1 -command {AppModButton}
      .f2.apps.menu.del add command -label $entry1 -command {AppDelButton}
   }
}

#------------------------------------------------------------------------------
#  MODIFY APP

.f2.apps.menu add cascade -label "Modify" -menu .f2.apps.menu.mod
menu .f2.apps.menu.mod -tearoff 0

DoLoop i 1 [llength $apps] {
   .f2.apps.menu.mod add command -label [lindex $apps [expr $i-1]] \
      -command {AppModButton}
}

#------------------------------------------------------------------------------
#  DELETE APP

.f2.apps.menu add cascade -label "Delete" -menu .f2.apps.menu.del
menu .f2.apps.menu.del -tearoff 0

DoLoop i 1 [llength $apps] {
   .f2.apps.menu.del add command -label [lindex $apps [expr $i-1]] \
      -command {AppDelButton}
}

#------------------------------------------------------------------------------
#  RUN APP

.f2.apps.menu add separator
.f2.apps.menu add cascade -label "Run  (in $prefs(TERM))" -menu .f2.apps.menu.run
menu .f2.apps.menu.run

DoLoop i 1 [llength $apps] {
   .f2.apps.menu.run add command -label [lindex $apps [expr $i-1]] \
      -command {AppRunButton}
}

#------------------------------------------------------------------------------
#  HELP

menubutton .f2.help -text "Help" -menu .f2.help.menu
menu .f2.help.menu -tearoff 0

.f2.help.menu add cascade -label "Web" -menu .f2.help.menu.web
if {$noweb} {.f2.help.menu entryconfigure 0 -state disabled}

.f2.help.menu add cascade -label "Man Pages" -menu .f2.help.menu.man

.f2.help.menu add command -label "xman" -command {exec xman &}
if {![FindCmd xman]} {.f2.help.menu entryconfigure 2 -state disabled}

.f2.help.menu add command -label "On PADE" -command {
   TextWrite .f5.text \
"File:Open/Save PADE file  Setup:Input host/file  Undo:Undo last change
Sort:Alphabetize host/file Graph:Visual input host/file  PVM:Start PVM
PVMmake:Save PVMmake config file, Run PVMmake     Apps:Run applications"
}

.f2.help.menu add command -label "Usage License" -command {
   Dialog .d  {PADE Message} \
         {The PADE software and its documentation have been produced as part\
         of work done by the U.S. Government, and are not subject to U.S.\
         copyright.  The mention of specific products, trademarks, or brand\
         names in PADE is for purposes of identification only.  Such mention\
         is not to be interpreted in any way as an endorsement or\
         certification of such products or brands by the National Institute\
         of Standards and Technology.  Report bugs to: robert.lipman@nist.gov} info 0 {OK} 
}

#------------------------------------------------------------------------------
#  START UP WEB PAGES

menu .f2.help.menu.web -tearoff 0

set html_list \
   {{PADE Home} http://www.itl.nist.gov/div895/pade/pade.html \
    {PADE Manual (local)} \
       file://localhost/$env(PADE_ROOT)/pade_doc/pade_man/pade_man.html \
    {PVM Home} http://www.epm.ornl.gov/pvm/pvm_home.html \
    {Parallel Tools Consortium} \
       http://www.ptools.org/ \
    {MPI (Message Passing Interface)} \
       http://www.mcs.anl.gov/Projects/mpi/index.html \
    {NIST Parallel Research} \
       http://www.itl.nist.gov/div895/sasg/parallel/index.html \
    }

set nhtml -1
for {set i 0} {$i < [llength $html_list]} {incr i 2} {
   incr nhtml
   set htmllab [string trim [lindex $html_list $i]]
   set htmllink($nhtml) [string trim [lindex $html_list [expr $i+1]]]
   if {$htmllab != ""} {
      .f2.help.menu.web add command -label $htmllab -command {
         set htmldex [.f2.help.menu.web index active]
         TextWrite .f5.text ""
	 eval exec $prefs(WEB) $htmllink($htmldex) &
      }
   } else {
      .f2.help.menu.web add separator
   }
}

#------------------------------------------------------------------------------
#  LIST MAN PAGES

menu .f2.help.menu.man -tearoff 0

set manpage {pvm_intro {pvm (console)} {pvmd3 (daemon)}}

set nmanpage -1
foreach manlab $manpage {
   set manlab [string trim $manlab]
   incr nmanpage
   if {$manlab != ""} {
      set blank [string first { } $manlab]
      if {$blank==-1} {
	 set mancmd($nmanpage) $manlab
      } else {
	 set mancmd($nmanpage) [string range $manlab 0 $blank]
      }
      .f2.help.menu.man add command -label $manlab -command {
         set mandex [.f2.help.menu.man index active]
         set manfil [string trim /tmp/pade.man.$mancmd($mandex)] 
	 TextWrite .f5.text ""
	 . config -cursor {watch red white}
	 update idletasks
	 eval exec man $mancmd($mandex) $colcmd >& $manfil
	 . config -cursor {arrow red white}
	 ListFile $manfil "man $mancmd($mandex)"
	 catch [file delete $manfil]
      }
   } else {
      .f2.help.menu.man add separator
   }
}

#-------------------------------------------------------------------------------

pack .f2.file .f2.setup -side left -padx 0.25m
pack .f2.undo .f2.sort .f2.graph -side left -ipadx 0.1m -padx 0.1m
pack .f2.pvm .f2.pvmmake .f2.apps -side left -padx 0.25m
pack .f2.help -side right

pack .f1 .f2 .f3 .f4 .f5 -side top -fill x
. config -cursor {arrow red white}

#  configure colors and text fonts 

ConfigColorText .
.f2 configure -background grey85
.f1.r.label1 configure -font $fonts(FONT_BIG)
.f1.r.label2 configure -font $fonts(FONT_BIG)

#-------------------------------------------------------------------------------

#  bind double click to host list

bind .f4.l.listbox <Double-1> {
   set index8 ""
   set index8 [.f4.l.listbox curselection]

   if {$index8 != ""} {
      TextWrite .f5.text ""
      .f4.r.listbox selection clear 0 end
      AddFiles $index8
      .f4.l.listbox selection set $index8 $index8
   }
}

PadeShortcuts

#  set min window size

update idletasks
wm minsize . [winfo width .] [winfo height .]

#  open PADE file on command line

if {$argc} {
   set padefile $argv
   OpenPadeFile 0
   if {$padefile != ""} {HostListboxWrite}
}

#  configure buttons 

UpdateButtons
