#!/bin/sh
# \
exec oagwish "$0" "$@"

#$Log: not supported by cvs2svn $
#Revision 1.18  2008/03/27 15:54:09  soliday
#Updated to work with new phoebus file server.
#
#Revision 1.17  2008/01/09 19:21:05  soliday
#/net/helios/usr/local changed to /net/helios/usr/local-sun4u
#
#Revision 1.16  2006/03/16 00:38:12  emery
#Indented.
#
#Revision 1.15  2005/11/14 19:53:17  soliday
#Updated to work on Linux.
#
#Revision 1.14  2005/01/18 17:24:02  soliday
#In OAG Version Control the SDDS and misc. files are not split by helios and
#phoebus servers. When these files are installed on both file systems they
#would look indistinguishable in the OAG Version Control tool. I have modified
#the program to display location of the selected file to reduce confusion.
#
#Revision 1.13  2004/07/26 16:00:32  soliday
#Added the ability to email OAG group members about version changes.
#
#Revision 1.12  2004/07/20 15:25:39  soliday
#Added a new category called Other files.
#
#Revision 1.11  2004/02/13 20:55:58  borland
#Added some update commands so the screen appears and says "working" while
#it is preparing the interface.
#
#Revision 1.10  2004/01/28 20:47:34  soliday
#Modified to work with the updated log files that contain a new column to
#specify the type of file that was cppatched.
#
#Revision 1.9  2004/01/19 22:36:12  soliday
#Improved the method for classifing files as SDDS config files.
#
#Revision 1.8  2004/01/19 22:28:28  soliday
#Added the ability to control the versions of SDDS configuration files.
#
#Revision 1.7  2002/10/09 19:53:46  soliday
#Updated to reflect new logDaemon directories.
#
#Revision 1.6  2002/07/09 16:00:41  shang
#modified to use the log files that are named by dates (saves the searching time)
#
#Revision 1.5  2001/08/06 18:29:48  soliday
#Really fixed the sorting problem now.
#
#Revision 1.4  2001/08/06 18:19:44  soliday
#Fixed sorting bug.
#
#Revision 1.3  2001/08/06 17:40:21  soliday
#Added the ability to sort by time.
#
#Revision 1.2  2001/07/12 20:37:07  soliday
#Fixed a bug so that the temp files are deleted.
#
#Revision 1.1  2001/07/12 20:22:41  soliday
#First version
#
#
#

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSDebugPath

set CVSRevisionAuthor "\$Revision: 1.19 $ \$Author: soliday $"

set appSelection ""
set phoebusLinkFile ""
set heliosLinkFile ""
set phoebusLink ""
set heliosLink ""
set type executable
set sortby name
set lastType $type
set lastSortby $sortby

proc appSelection {item doubleClick} {
    if {$doubleClick == 0} {
        global appSelection tmpFile phoebusVersionWidget heliosVersionWidget 
        global appData notesWidget makeOfficalWidget type env
        if {$item == $appSelection} {
            return
        }
        $makeOfficalWidget configure -state disabled
        $notesWidget delete 1.0 end
        $phoebusVersionWidget delete 1.0 end
        $heliosVersionWidget delete 1.0 end
        set appSelection $item
        if {[catch {exec sddsprocess $tmpFile ${tmpFile}2 \
                      -match=column,applicationName=$appSelection} results]} {
            APSSetVarAndUpdate status $results
            return
        }
        if {[catch {sdds load ${tmpFile}2 appData} results]} {
            APSSetVarAndUpdate status $results
            return
        }
        file delete ${tmpFile}2
        global phoebusLinkFile heliosLinkFile phoebusLink heliosLink
        set phoebusLinkFile ""
        set heliosLinkFile ""
        if {($type == "SDDS files") || ($type == "Other files")} {
            set newVersion [lindex [lindex $appData(Column.newVersion) 0] end]
            set heliosLink [file join [file dirname $newVersion] $item]
            if {[file exists $heliosLink]} {
                if {[file type $heliosLink] == "link"} {
                    set heliosLinkFile [file tail [file readlink $heliosLink]]
                }
            }
        } else {
            if {[file exists /net/phoebus/usr/local-sun4u/oag/bin_patch/$env(HOST_ARCH)/$item]} {
                set phoebusLink /net/phoebus/usr/local-sun4u/oag/bin_patch/$env(HOST_ARCH)/$item
            } elseif {[file exists /net/phoebus/usr/local-sun4u/oag/apps/bin/$env(HOST_ARCH)/$item]} {
                set phoebusLink /net/phoebus/usr/local-sun4u/oag/apps/bin/$env(HOST_ARCH)/$item
            } elseif {[file exists /net/phoebus/usr/local-sun4u/oag/lib_patch/$env(HOST_ARCH)/$item]} {
                set phoebusLink /net/phoebus/usr/local-sun4u/oag/lib_patch/$env(HOST_ARCH)/$item
            } elseif {[file exists /net/phoebus/usr/local-sun4u/oag/apps/lib/$env(HOST_ARCH)/$item]} {
                set phoebusLink /net/phoebus/usr/local-sun4u/oag/apps/lib/$env(HOST_ARCH)/$item
            }
            if {[file exists $phoebusLink]} {
                if {[file type $phoebusLink] == "link"} {
                    set phoebusLinkFile [file tail [file readlink $phoebusLink]]
                }
            }
            
            if {[file exists /net/helios/usr/local-sun4u/oag/bin_patch/$env(HOST_ARCH)/$item]} {
                set heliosLink /net/helios/usr/local-sun4u/oag/bin_patch/$env(HOST_ARCH)/$item
            } elseif {[file exists /net/helios/usr/local-sun4u/oag/apps/bin/$env(HOST_ARCH)/$item]} {
                set heliosLink /net/helios/usr/local-sun4u/oag/apps/bin/$env(HOST_ARCH)/$item
            } elseif {[file exists /net/helios/usr/local-sun4u/oag/lib_patch/$env(HOST_ARCH)/$item]} {
                set heliosLink /net/helios/usr/local-sun4u/oag/lib_patch/$env(HOST_ARCH)/$item
            } elseif {[file exists /net/helios/usr/local-sun4u/oag/apps/lib/$env(HOST_ARCH)/$item]} {
                set heliosLink /net/helios/usr/local-sun4u/oag/apps/lib/$env(HOST_ARCH)/$item
            }
            if {[file exists $heliosLink]} {
                if {[file type $heliosLink] == "link"} {
                    set heliosLinkFile [file tail [file readlink $heliosLink]]
                }
            }
        }

        foreach newVersion [lindex $appData(Column.newVersion) 0] {
            if {![file exists $newVersion]} {
                continue
            }
            if {[string last . $newVersion] > [string last - $newVersion]} {
                set separator "."
            } else {
                set separator "-"
            }
            set name [file tail $newVersion]
            if {($type == "SDDS files") || ($type == "Other files")} {
                if {$name == $heliosLinkFile} {
                    $heliosVersionWidget insert end $name\n green
                } else {
                    $heliosVersionWidget insert end $name\n normal
                }
            } else {
                if {[string range $newVersion 0 11] == "/net/phoebus"} {
                    if {$name == $phoebusLinkFile} {
                        $phoebusVersionWidget insert end $name\n green
                    } else {
                        $phoebusVersionWidget insert end $name\n normal
                    }
                }
                if {[string range $newVersion 0 10] == "/net/helios"} {
                    if {$name == $heliosLinkFile} {
                        $heliosVersionWidget insert end $name\n green
                    } else {
                        $heliosVersionWidget insert end $name\n normal
                    }
                }
            }
        }
    }
}

set versionSelection ""
proc versionSelection {versionWidgetIndex widget} {
    set showNotes 1
    global heliosVersionWidget phoebusVersionWidget versionSelection 
    global appData notesWidget makeOfficalWidget tmpFile type
    set server $widget
    if {$widget == "helios"} {
        set widget $heliosVersionWidget
        set widgetAlt $phoebusVersionWidget
        set mountPoint /net/helios/
        set mountPointAlt /net/phoebus
    } else {
        set widget $phoebusVersionWidget
        set widgetAlt $heliosVersionWidget
        set mountPoint /net/phoebus
        set mountPointAlt /net/helios/
    }
    set first [$widget index "$versionWidgetIndex linestart"]
    set last [$widget index "$versionWidgetIndex lineend + 1 chars"]
    set versionSelection [$widget get $first "$versionWidgetIndex lineend"]
    if {![string compare $versionSelection ""]} {
        return
    }
    if {($type == "SDDS files") || ($type == "Other files")} {
        set n [expr {$first - 1}]
        set nIdentical 0
        while {$n >= 1} {
            if {[$widget get $n "$n lineend"] == $versionSelection} {
                incr nIdentical
            }
            set n [expr {$n - 1}]
        }
    }

    $widget tag remove selected 0.0 $first
    $widget tag add selected $first $last
    $widget tag remove selected $last end
    set versionSelectionFirst $first
    set versionSelectionLast $last
    
    global heliosVersionSelection phoebusVersionSelection
    set heliosVersionSelection ""
    set phoebusVersionSelection ""
    set index 0
    set i 0
    if {($type == "SDDS files") || ($type == "Other files")} {
        foreach newVersion [lindex $appData(Column.newVersion) 0] \
          time [lindex $appData(Column.Time) 0] {
              if {[file tail $newVersion] == $versionSelection} {
                  if {$nIdentical > 0} {
                      incr nIdentical -1
                  } else {
                      set versionTime $time
                      if {$server == "helios"} {
                          set heliosVersionSelection $newVersion
                      } else {
                          set phoebusVersionSelection $newVersion
                      }
                      break
                  }
              }
              incr index
          }
    } else {
        foreach newVersion [lindex $appData(Column.newVersion) 0] \
          time [lindex $appData(Column.Time) 0] {
              if {[file tail $newVersion] == $versionSelection} {
                  if {[string range $newVersion 0 11] == $mountPoint} {
                      set versionTime $time
                      if {$server == "helios"} {
                          set heliosVersionSelection $newVersion
                      } else {
                          set phoebusVersionSelection $newVersion
                      }
                      break
                  }
              }
              incr index
          }
        foreach newVersion [lindex $appData(Column.newVersion) 0] \
          time [lindex $appData(Column.Time) 0] {
              if {[string range $newVersion 0 11] == $mountPointAlt} {
                  incr i
                  if {[expr {abs($time - $versionTime)}] < 10} {
                      if {$server == "helios"} {
                          set phoebusVersionSelection $newVersion
                      } else {
                          set heliosVersionSelection $newVersion
                      }
                      break
                  }
              }
          }
    }

    if {$i == 0} {
        $widgetAlt tag remove selected 0.0 end
        if {($type != "SDDS files") && ($type != "Other files")} {
            bell
            APSSetVarAndUpdate status "Corresponding version not found"
        }
    } else {
        set first [$widgetAlt index "$i.0 linestart"]
        set last [$widgetAlt index "$i.0 lineend + 1 chars"]
        $widgetAlt tag remove selected 0.0 $first
        $widgetAlt tag add selected $first $last
        $widgetAlt tag remove selected $last end
    }

    $notesWidget delete 1.0 end
    $notesWidget insert end "Version Date: [clock format [expr int($versionTime)]]\n"
    $notesWidget insert end "Committed By: [lindex [lindex $appData(Column.user) 0] $index]\n"
    $notesWidget insert end "Previous Version: [lindex [lindex $appData(Column.previousVersion) 0] $index]\n"
    if {($type == "SDDS files") || ($type == "Other files")} {
        $notesWidget insert end "Selected Version: $heliosVersionSelection\n"
    }
    set comment [string trim [string trim [lindex [lindex $appData(Column.comment) 0] $index]] \b]
    $notesWidget insert end "Comment: $comment\n"
    
    $makeOfficalWidget configure -state normal
    
    if {[llength $heliosVersionSelection]} {
        if {[catch {exec sddsprocess /net/helios/usr/local-sun4u/oag/oagVersionControl.log ${tmpFile}2 \
                      "-match=column,NewVersion=[file tail $heliosVersionSelection],ReplacedVersion=[file tail $heliosVersionSelection],|" -noWarnings} results]} {
            APSSetVarAndUpdate status $results
            return
        }
        if {[catch {sdds load ${tmpFile}2 versionLog} results]} {
            APSSetVarAndUpdate status $results
            return
        }
        file delete ${tmpFile}2
        
        set rows [llength [lindex $versionLog(Column.Time) 0]]
        if {$rows > 0} {
            $notesWidget insert end "Version Log:\n"
            for {set i [expr {$rows - 1}]} {$i >= 0} {incr i -1} {
                $notesWidget insert end "  [clock format [lindex [lindex $versionLog(Column.Time) 0] $i]], ([lindex [lindex $versionLog(Column.User) 0] $i]), [lindex [lindex $versionLog(Column.NewVersion) 0] $i] replaced [lindex [lindex $versionLog(Column.ReplacedVersion) 0] $i]\n"
                $notesWidget insert end "    Reason: [lindex [lindex $versionLog(Column.Comment) 0] $i]\n"
            }
        }
    }
}

proc getReason {args} {
    global makeOfficalWidget reason
    set reason ""
    $makeOfficalWidget configure -state disabled
    APSDialogBox .db \
      -name "Enter Reason For Change" \
      -okCommand "MakeOffical"
    APSLabeledEntry .reason \
      -parent .db.userFrame \
      -label "Enter Reason:" \
      -textVariable reason \
      -width 50
}

proc MakeOffical {args} {
    global heliosVersionSelection phoebusVersionSelection
    global phoebusLink heliosLink appSelection reason env
    
    if {![llength $heliosLink]} {
        set heliosLink [file join [file dirname $heliosVersionSelection] $appSelection]
    }
    if {![llength $phoebusLink]} {
        set phoebusLink [file join [file dirname $phoebusVersionSelection] $appSelection]
    }
    set pwd [pwd]
    if {([llength $heliosVersionSelection]) && \
          ([llength $heliosLink])} {
        set replacedVersion [file tail [file readlink $heliosLink]]
        if {[catch {file delete -force $heliosLink} results]} {
            bell
            APSSetVarAndUpdate status "Error: $results"
            return
        }
        cd [file dirname $heliosVersionSelection]
        if {[catch {exec ln -s [file tail $heliosVersionSelection] [file tail $heliosLink]} results]} {
            bell
            APSSetVarAndUpdate status "Error: $results"
            cd $pwd
            return
        }
        if {[file extension $appSelection] == ".tcl"} {
            makeTclIndexFile
        }
        cd $pwd
        
        if {[catch {sdds load /net/helios/usr/local-sun4u/oag/oagVersionControl.log versionLog} results]} {
            APSSetVarAndUpdate status $results
            return
        }
        set versionLog(Column.Time) [list "[lindex $versionLog(Column.Time) 0] [clock seconds]"]
        set versionLog(Column.NewVersion) [list "[lindex $versionLog(Column.NewVersion) 0] [list [file tail $heliosVersionSelection]]"]
        set versionLog(Column.ReplacedVersion) [list "[lindex $versionLog(Column.ReplacedVersion) 0] [list $replacedVersion]"]
        set versionLog(Column.User) [list "[lindex $versionLog(Column.User) 0] [list $env(USER)]"]
        set versionLog(Column.Comment) [list "[lindex $versionLog(Column.Comment) 0] [list $reason]"]
        if {[catch {sdds save /net/helios/usr/local-sun4u/oag/oagVersionControl.log versionLog} results]} {
            APSSetVarAndUpdate status $results
            return
        }
        
        set message "Previous Version: $replacedVersion\n New Version: [file tail $heliosVersionSelection]\n Updated By: $env(USER)\n Comment: $reason\n\n"
        foreach address "soliday borland emery sereno shang" {
            APSSendEMail -address $address -message "$message" -subject "cppatch info (version control)"
        }

        APSSetVarAndUpdate status "$heliosVersionSelection is now offical"
    } else {
        APSSetVarAndUpdate status "No changes for helios"
    }

    if {([llength $phoebusVersionSelection]) && \
          ([llength $phoebusLink])} {
        if {[catch {file delete -force $phoebusLink} results]} {
            bell
            APSSetVarAndUpdate status "Error: $results"
            return
        }
        cd [file dirname $phoebusVersionSelection]
        if {[catch {exec ln -s [file tail $phoebusVersionSelection] [file tail $phoebusLink]} results]} {
            bell
            APSSetVarAndUpdate status "Error: $results"
            cd $pwd
            return
        }
        if {[file extension $appSelection] == ".tcl"} {
            makeTclIndexFile
        }
        cd $pwd
        APSSetVarAndUpdate status "$phoebusVersionSelection is now offical"
    } else {
        APSSetVarAndUpdate status "No changes for phoebus"
    }
    set a $appSelection
    set appSelection ""
    appSelection $a 0
}

proc makeTclIndexFile {args} {
    APSSetVarAndUpdate status "Making TCL index file"
    if {[catch {file copy -force tclIndex tclIndex.old
        exec chgrp oagmgr tclIndex.old 
        exec chmod g+w tclIndex.old} result]} {
        APSSetVarAndUpdate status " *** Warning [pwd]: $result"
        return
    }
    if {[catch {file delete -force tclIndex} result]} {
        APSSetVarAndUpdate status " *** Error [pwd]: $result"
        return
    }
    if {[catch {auto_mkindex . *.tcl} result]} {
        APSSetVarAndUpdate status " *** Error [pwd]: $result"
        if {[catch {file copy tclIndex.old tclIndex} result]} {
            APSSetVarAndUpdate status " *** Couldn't restore old tclIndex!"
            return
        } else {
            APSSetVarAndUpdate status " *** Old tclIndex restored."
        }
    } else {
        APSSetVarAndUpdate status "  Auto path updated for [pwd]"
    }
    # Change permissions and primary group membership
    if {[catch {exec chmod g+w tclIndex} result]} {
        APSSetVarAndUpdate status " *** Error [pwd]: chmod g+w: $result"
    }
    if {[catch {exec chgrp oagmgr tclIndex} result]} {
        APSSetVarAndUpdate status " *** Error [pwd]: chgrp oagmgr: $result"
    }
}

proc getAppList {args} {
    set group appVersionUpdate.log
    set dirList [glob /net/phoebus/oagmgr/logDaemonData.*]
    foreach dir $dirList {
        append wildcard "${dir}/appVersionUpdate/*.log ${dir}/$group ${dir}/save/$group.????? ${dir}/save/$group.?????.gz "
    }
    set dataDir /home/helios/oagData/logDaemonData/appVersionUpdate
    set fileList [lsort [eval glob /home/helios/oagData/logDaemonData/$group \
                           $dataDir/*.log $wildcard]]
    global tmpFile type
    set tmpFile /tmp/[APSTmpString]
    APSAddToTempFileList $tmpFile ${tmpFile}2 ${tmpFile}3
    set option ""
    if {$type == "executable"} {
        set option "-match=column,applicationName=*.tcl,!,applicationName=*.alog,!,&,applicationName=*.sdds,!,&,applicationName=*.cond,!,&,applicationName=*.mon,!,&,applicationName=*.atrig,!,&,applicationName=*.trigger,!,&,applicationName=*.loc,!,&,applicationName=*.pv,!,&,type=sdds,!,&,type=tcl,!,&,type=ascii,!,&"
    } elseif {$type == "Tcl files"} {
        set option "-match=column,applicationName=*.tcl,type=tcl,|"
    } elseif {$type == "SDDS files"} {
        set option "-match=column,applicationName=*.alog,applicationName=*.sdds,|,applicationName=*.cond,|,applicationName=*.mon,|,applicationName=*.atrig,|,applicationName=*.trigger,|,applicationName=*.loc,|,applicationName=*.pv,|,type=sdds,|"
    } elseif {$type == "Other files"} {
        set option "-match=column,type=ascii"
    }
    set len [llength $fileList]
    set newFileList ""
    for {set i 0} {$i < $len} {incr i 100} {
        APSSetVarAndUpdate status "[format %3.0f [expr {100.0 * $i / $len}]]% of initialization done"
        APSAddToTempFileList ${tmpFile}_$i
        lappend newFileList ${tmpFile}_$i
        if {[catch {eval exec sddscombine [lrange $fileList $i [expr $i + 100]] ${tmpFile}_$i  -merge} results]} {
            APSSetVarAndUpdate status $results
            return
        }
    }
    APSSetVarAndUpdate status "100% of initialization done"
    if {[catch {eval exec sddscombine $newFileList -pipe=out -merge | \
                  sddssort -pipe -column=Time | \
                  tee $tmpFile | \
                  sddsprocess -pipe ${option} | \
                  sddssort -pipe=in ${tmpFile}2 -column=applicationName -unique} results]} {
        APSSetVarAndUpdate status $results
        return
    }
    if {[catch {sdds load ${tmpFile}2 data} results]} {
        APSSetVarAndUpdate status $results
        return
    }
    file delete ${tmpFile}2
    return [lindex $data(Column.applicationName) 0]
}

proc modifyAppList {args} {
    global appWidget tmpFile type makeOfficalWidget notesWidget phoebusVersionWidget heliosVersionWidget lastType appSelection sortby lastSortby

    if {($type == $lastType) && ($sortby == $lastSortby)} {
        return
    }
    set appSelection ""
    set lastType $type
    set lastSortby $sortby
    $makeOfficalWidget configure -state disabled
    $notesWidget delete 1.0 end
    $phoebusVersionWidget delete 1.0 end
    $heliosVersionWidget delete 1.0 end

    $appWidget delete 0 end

    set option ""
    if {$type == "executable"} {
        set option "-match=column,applicationName=*.tcl,!,applicationName=*.alog,!,&,applicationName=*.sdds,!,&,applicationName=*.cond,!,&,applicationName=*.mon,!,&,applicationName=*.atrig,!,&,applicationName=*.trigger,!,&,applicationName=*.loc,!,&,applicationName=*.pv,!,&,type=sdds,!,&,type=tcl,!,&,type=ascii,!,&"
        if {![winfo viewable .userFrame.top.phoebusVersion]} {
            pack .userFrame.top.phoebusVersion -side left -expand true -fill both
            .userFrame.top.heliosVersion.label configure -text "Helios Version"
        }
    } elseif {$type == "Tcl files"} {
        set option "-match=column,applicationName=*.tcl,type=tcl,|"
        if {![winfo viewable .userFrame.top.phoebusVersion]} {
            pack .userFrame.top.phoebusVersion -side left -expand true -fill both
            .userFrame.top.heliosVersion.label configure -text "Helios Version"
        }
    } elseif {$type == "SDDS files"} {
        set option "-match=column,applicationName=*.alog,applicationName=*.sdds,|,applicationName=*.cond,|,applicationName=*.mon,|,applicationName=*.atrig,|,applicationName=*.trigger,|,applicationName=*.loc,|,applicationName=*.pv,|,type=sdds,|"
        pack forget .userFrame.top.phoebusVersion
        .userFrame.top.heliosVersion.label configure -text "File Version"
    } elseif {$type == "Other files"} {
        set option "-match=column,type=ascii"
        pack forget .userFrame.top.phoebusVersion
        .userFrame.top.heliosVersion.label configure -text "File Version"
    }
    if {$sortby == "name"} {
        if {[catch {eval exec sddsprocess $tmpFile \
                      -pipe=out ${option} | \
                      sddssort -pipe=in ${tmpFile}2 \
                      -column=applicationName -unique} results]} {
            APSSetVarAndUpdate status $results
            return
        }
    } else {
        if {[catch {eval exec sddsprocess $tmpFile \
                      -pipe=out ${option} | \
                      sddssort -pipe=in ${tmpFile}2 \
                      -column=Time,decreasing -column=applicationName -unique} results]} {
            APSSetVarAndUpdate status $results
            return
        }
    }
    if {[catch {sdds load ${tmpFile}2 data} results]} {
        APSSetVarAndUpdate status $results
        return
    }
    file delete ${tmpFile}2
    if {$sortby == "name"} {
        eval $appWidget insert 0 [lindex $data(Column.applicationName) 0]
    } else {
        set appList ""
        foreach app [lindex $data(Column.applicationName) 0] {
            if {[lsearch -exact $appList $app] == -1} {
                append appList " $app"
            }
        }
        eval $appWidget insert 0 $appList
        
    }
}

proc tagColors {textWidget} {
    $textWidget tag configure selected -relief raised
    $textWidget tag configure green -background green \
      -borderwidth 2 -spacing1 1 -spacing3 1
    $textWidget tag raise green selected
    $textWidget tag configure yellow -background yellow \
      -borderwidth 2 -spacing1 1 -spacing3 1
    $textWidget tag raise yellow selected
    $textWidget tag configure normal -background Gray85 \
      -borderwidth 2 -spacing1 1 -spacing3 1
    $textWidget tag raise normal selected
}

APSApplication . \
  -name "OAG Version Control" \
  -version $CVSRevisionAuthor \
  -overview "This program will allow you to revert to a previos version of OAG software" 

set status "Working..."
APSScrolledStatus .status \
  -parent .userFrame \
  -textVariable status \
  -width 100 \
  -height 8
APSRepack .userFrame.status \
  -fill x
update

pack [frame .userFrame.top] -expand true -fill both
pack [frame .userFrame.bottom] -expand true -fill both
pack [frame .userFrame.buttonFrame] -fill x

APSFrame .appFrame -parent .userFrame.top \
  -packOption "-side left -expand true -fill both" \
  -label "Application" \
  -contextHelp "Select the application of interest"
APSRadioButtonFrame .type \
  -parent .userFrame.top.appFrame \
  -label "" \
  -buttonList {"executable" "Tcl files" "SDDS files" "Other files"} \
  -valueList {"executable" "Tcl files" "SDDS files" "Other files"} \
  -variable type \
  -orientation horizontal \
  -relief "flat" \
  -limitPerRow 2 \
  -commandList "modifyAppList modifyAppList modifyAppList modifyAppList"
APSRadioButtonFrame .sortby \
  -parent .userFrame.top.appFrame \
  -label "Sort by:" \
  -buttonList {"name" "time"} \
  -valueList "name time" \
  -variable sortby \
  -orientation horizontal \
  -relief "flat" \
  -commandList "modifyAppList modifyAppList"
APSRepack .userFrame.top.appFrame.frame \
  -expand true \
  -fill both

APSFrame .heliosVersion \
  -parent .userFrame.top \
  -packOption "-side left -expand true -fill both" \
  -label "Helios Version" \
  -contextHelp "Select the application of interest"
APSRepack .userFrame.top.heliosVersion.frame \
  -expand true \
  -fill both

APSFrame .phoebusVersion \
  -parent .userFrame.top \
  -packOption "-side left -expand true -fill both" \
  -label "Phoebus Version" \
  -contextHelp "Select the application of interest"
APSRepack .userFrame.top.phoebusVersion.frame \
  -expand true \
  -fill both

APSFrame .releaseNotes \
  -parent .userFrame.bottom \
  -packOption "-side top -expand true -fill both" \
  -label "Release Notes"
APSRepack .userFrame.bottom.releaseNotes.frame \
  -expand true \
  -fill both
update

set appList [getAppList]

APSScrolledList .app \
  -parent .userFrame.top.appFrame.frame \
  -itemList $appList \
  -callback appSelection \
  -height 10 \
  -contextHelp "Select the application of interest"
set appWidget .userFrame.top.appFrame.frame.app.listbox

APSScrolledText .heliosVersion \
  -parent .userFrame.top.heliosVersion.frame \
  -height 10 \
  -width 24 \
  -contextHelp "Select a version to view it's release notes. Also, any operations (such as changing the official version) will apply to the currently selected version."
set heliosVersionWidget .userFrame.top.heliosVersion.frame.heliosVersion.text
$heliosVersionWidget configure -cursor left_ptr

APSScrolledText .phoebusVersion \
  -parent .userFrame.top.phoebusVersion.frame \
  -height 10 \
  -width 24 \
  -contextHelp "Select a version to view it's release notes. Also, any operations (such as changing the official version) will apply to the currently selected version."
set phoebusVersionWidget .userFrame.top.phoebusVersion.frame.phoebusVersion.text
$phoebusVersionWidget configure -cursor left_ptr

APSScrolledText .notes \
  -parent .userFrame.bottom.releaseNotes.frame \
  -height 10 \
  -contextHelp "These are application notes submitted by the programmer who created this application."
set notesWidget .userFrame.bottom.releaseNotes.frame.notes.text

APSButton .makeOffical \
  -parent .userFrame.buttonFrame \
  -text "Make Offical" \
  -command "getReason"
set makeOfficalWidget .userFrame.buttonFrame.makeOffical.button
$makeOfficalWidget configure -state disabled

tagColors $phoebusVersionWidget
tagColors $heliosVersionWidget

bindtags $phoebusVersionWidget [list $phoebusVersionWidget . all]
bindtags $heliosVersionWidget [list $heliosVersionWidget . all]
bindtags $notesWidget [list $notesWidget . all]

bind $phoebusVersionWidget <Button-1> {
    set cur [$phoebusVersionWidget index @%x,%y]
    versionSelection $cur phoebus
}
bind $heliosVersionWidget <Button-1> {
    set cur [$heliosVersionWidget index @%x,%y]
    versionSelection $cur helios
}
set status "Ready."
update
