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

#
# $Log: not supported by cvs2svn $
# Revision 1.7  2009/06/12 15:22:18  shang
# fixed a bug in reading current control mode with file readlink
#
# Revision 1.6  2008/06/20 19:57:54  borland
# File generation now uses APSExecLog and puts messages in status dialog.
#
# Revision 1.5  2005/11/30 23:58:58  emery
# Added frame to do editing of the controllaw templated files.
# Removed reference to the old controllaw that used mod. Anode
# to control the rf gap voltage. Moved a cd command to within
# UpdateControllawMode. Use variable directoryList for
# possible future list of controllaw base directories that are affected
# by the RF waveguide mode switch
#
# Revision 1.4  2000/05/03 18:48:14  emery
# Using links to public/modAnode and public/mode.
#
# Revision 1.3  1997/12/30 18:21:57  emery
# A directory link change for SR gap voltage controllaw was
# added.
#
# Revision 1.2  1997/07/25 19:04:02  borland
# Added a variable after a catch statement to hold the error return.
#
# Revision 1.1  1997/07/09 22:44:57  borland
# 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)]
set CVSRevisionAuthor "\$Revision: 1.8 $ \$Author: shang $"

APSApplication . -name "SRSetRFControllawMode" -version $CVSRevisionAuthor \
  -overview "Allows reconfiguring rf controllaw for the storage ring when the rf mode is changed.\n\nAllows editing of template files used in generating the controllaw files for all possible rf mode switch positions.\n\nIn general the script can reconfigure all controllaws related to maintaining rf gap voltages. We presently maintain only one, srrfErr, which uses the AGC loop controllaw. The editing is presently configured to edit just the srrfErr (/home/helios/oagData/controllaw/SRgapVoltage) templates."

set status "Ready."

# There used to be two controllaw directories affected by mode changes
# Now there is one.
set directoryList [list /home/helios/oagData/controllaw/SRgapVoltage]

if {[pv linkw WGSMode S6:WGS:modeSelect] || \
      [pv umon WGSMode]} {
    global errorCode
    APSAlertBox [APSUniqueName .] \
      -errorMessage "Unable to connect to S6:WGS:modeSelect: $errorCode"
    exit 1
}

proc UpdateControllawMode {args} {
    global ControllawMode 
# have to read two level of link
    APSParseArguments {directory}
    cd $directory
    set ControllawMode [file tail [file readlink $directory/public/mode]]
# This apparently causes an infinite loop of checking the link
# This script will update the variable ControllawMode
    after 2000 UpdateControllawMode -directory $directory
}

# This will run continuously
UpdateControllawMode -directory [lindex $directoryList 0]

proc ChangeControllawMode {newMode} {
    global ControllawMode directoryList
    if [string compare $newMode $ControllawMode]==0 {
        APSSetVarAndUpdate status "Mode is already $newMode---no change made."
        return
    }
    foreach baseDirectory $directoryList {
        cd $baseDirectory
        if {[catch {exec rm public/mode 
            exec ln -s ../$newMode public/mode} result]} {
            APSSetVarAndUpdate status "Couldn't set mode\n$result"
	    set ControllawMode $newMode
        } else {
            APSSetVarAndUpdate status "Mode changed to $newMode."
            APSSetVarAndUpdate status "DON'T FORGET TO STOP AND RESTART CONTROLLAWS."
        }
    }
}

set status Working...
APSScrolledStatus .status -parent .userFrame -textVariable status \
    -width 50

APSFrame .modeSwitch -parent .userFrame \
    -packOption "-side top"
set w .userFrame.modeSwitch.frame
APSLabeledOutput .wgsMode -parent $w -label "Present mode rf: " \
  -contextHelp "The presently selected rf switch mode." -textVariable WGSMode

APSLabeledOutput .controlMode -parent $w -label "Present controllaw mode: " \
  -contextHelp "The presently selected controllaw mode." \
  -textVariable ControllawMode

set modeList [concat [lsort [glob mode?]] [lsort [glob mode?? ]]]
foreach item $modeList {
    APSButton .$item -parent $w -text "->$item" \
      -packOption "-side left" \
      -command "ChangeControllawMode $item" \
      -contextHelp "Changes the controllaw mode to $item.  N.B.: you must stop and restart the controllaws to complete this!"
}

#Presently there is only one controllaw directory for rf gap control
set baseDir /home/helios/oagData/controllaw/SRgapVoltage/scripts
set test3637Template $baseDir/rf3637Err.template.tests
set def3637Template $baseDir/rf3637Err.template.def
set matrix3637Template $baseDir/rf3637Err.template.matrix
set test3840Template $baseDir/rf3840Err.template.tests
set def3840Template $baseDir/rf3840Err.template.def
set matrix3840Template $baseDir/rf3840Err.template.matrix

APSFrame .fileConfiguration -parent .userFrame \
  -label "Template editing and file generation" \
    -packOption "-side top"
set w .userFrame.fileConfiguration.frame
set fileList {tests def matrix}
set systemList {rf3637 rf3840}
APSFrameGrid .grid -parent $w -xList [concat label $fileList] -yList [concat label $systemList] \
  -width 100

foreach file $fileList {
    label $w.grid.$file.label.label -text $file
    pack $w.grid.$file.label.label -side left -fill both -expand 1
    foreach system $systemList {
        APSButton .edit -parent $w.grid.$file.$system \
          -text "Edit..." \
          -packOption "-side left -fill both -expand 1" \
          -command "exec sddsedit $baseDir/${system}Err.template.$file &" \
          -contextHelp "Edits the file pointed to by link ${system}.template.$file. Remember to make a save so that a new version of the file is saved and the link updated. Press the generate new files button afterwards."
    }
}
foreach system $systemList  {
    label $w.grid.label.$system.label -text $system 
    pack $w.grid.label.$system.label -side left -fill both -expand 1
}

proc GenerateFiles {} {
    global baseDir status generateDone
    set dir [pwd]
    cd $baseDir
    set status "Generating files..."
    update
    set generateDone 0
    APSExecLog .gen -unixCommand "$baseDir/generateFiles" -callback "set generateDone 1" \
        -cancelCallback "set generateDone 2" -abortCallback "set generateDone 2"
    tkwait variable generateDone
    if {$generateDone==1} {
        set status "Completed file generation."
    } else {
        set status "File generation aborted or canceled"
    }
    update 
    cd $dir
}

APSButton .generate -parent $w \
  -text "Generate files" \
  -packOption "-side left -fill both -expand 1" \
  -command "GenerateFiles" \
  -contextHelp "Generates new files based on edits done in this GUI. Files for all rf systems and all modes are regenerated."

set status Ready.
update

