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



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 condDir /home/helios/OAG/oagData/controlFiles/Linac/conditioning
set args $argv

APSStrictParseArguments {fileRoot plotOnly}

set rgCorrQuadList [exec sddsprocess -pipe=out ${condDir}/linacConditioning.snap \
    "-match=col,ControlName=L1:*:*.E,ControlName=L1:*:SC*.E,||" \
    | sdds2stream -pipe=in -col=ControlName]

set rgCorrQuadLimitList [exec sddsprocess -pipe=out ${condDir}/linacConditioning.snap \
    "-match=col,ControlName=L1:*:*.E,ControlName=L1:RG*:SC*.E,||" \
    | sdds2stream -pipe=in -col=ValueString]



set index 0
foreach mag $rgCorrQuadList {
    set pvNameCQ [lindex [split $mag .] 0]
    set pvNameCQ [exec editstring $pvNameCQ -editcommand=%/:StdSU// ]
    set pvNameCQ [exec editstring $pvNameCQ -editcommand=%/:Std1SU// ]
    lappend setList $pvNameCQ
    incr index
    
}





set status "Ready"

proc MakeConfigMenuAndStatus {} {

        APSApplication . -name "setLinacRFGMagnetDrive" -version "1.0" \
                         -overview "This is an application to set the High and Low drives for Linac power supplies"

        

        APSScrolledStatus .status -parent .userFrame -textVariable status \
                          -height 8 -width 65 -packOption "-fill both -expand true"
}

proc setStatus {text} {
    APSSetVarAndUpdate status $text
}




proc MakeCheckbuttonAndExecuteFrame {widget args} {

        global rgCorrQuadList setList checkButton checkButtonCount
        set limitPerRow [expr [llength $setList]/4]
        set parent ""
        APSParseArguments {parent}


        APSFrame $widget -parent $parent \
	            -label "Magnet drive selection" \
                   -packOption {-side left -expand 1 -fill x} \
                   -contextHelp ""

        set w $parent$widget.frame
 
    

APSCheckButtonFrame .cb -parent $w -buttonList $setList\
      -variableList $setList  -allNone 1 -toggle 1\
      -label "Power Supplies"  -limitPerRow $limitPerRow




APSButton .transferButton -parent $w \
                   -text "Set HOPR Limit" \
                   -command SendHOPRLimits

		   
               	   
APSButton .transferButton2 -parent $w \
                   -text "Set LOPR Limit" \
                   -command SendLOPRLimits
                  
set value 0
APSLabeledEntry .value -parent $w -label "Value (for set operations): " \
    -textVariable value

     
}




proc SendHOPRLimits {} {
    global operation rgCorrQuadList APSDevSendDebug useCavput setList rgCorrQuadLimitList value
      
foreach dev $setList {
   global $dev 
    if {[set $dev] && [lsearch -exact $setList $dev]!=-1} {
            lappend devToDo $dev
	    
	    exec caput $dev:CurrentAO.DRVH $value
	    }
    }
    if [llength $devToDo]==0 {
        return -code error "No such operation."
    }
#exec caput $dev:CurrentAO.FLNK $dev:CnditVal
setStatus "New high drive limits for $devToDo loaded"
update

}
proc SendLOPRLimits {} {
    global operation rgCorrQuadList APSDevSendDebug useCavput setList rgCorrQuadLimitList value
      puts $value  
foreach dev $setList {
   global $dev 
    if {[set $dev] && [lsearch -exact $setList $dev]!=-1} {
            lappend devToDo $dev
	    exec caput $dev:CurrentAO.DRVL $value
	    }
    }
    if [llength $devToDo]==0 {
        return -code error "No such operation."
    }
setStatus "New low drive limits for $devToDo loaded"
update

}

# Build Application

MakeConfigMenuAndStatus
MakeCheckbuttonAndExecuteFrame .cbFrame -parent .userFrame
