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

#
# $Log: not supported by cvs2svn $
# Revision 1.3  2000/06/12 19:34:28  borland
# Now allows use of cavput option
#
# Revision 1.2  2000/05/02 21:25:32  borland
# Now use APSLinkToLINACPVs since it is faster.
#
# Revision 1.1  2000/05/01 20:39:08  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)]
APSDebugPath
set CVSRevisionAuthor "\$Author: soliday $"

APSApplication . -name LinacRFWFUtil \
  -overview "Allows executing ganged operations on linac power supplies."

set status ""
APSScrolledStatus .status -parent .userFrame -textVariable status -width 60

proc setStatus {text} {
    APSSetVarAndUpdate status $text
}

setStatus "Working..."
update

set apsLinacRFWFDeviceFile /home/helios/oagData/deviceConfig/linac/rfWaveformOps.pvTable

set devList [exec sddssort $apsLinacRFWFDeviceFile -pipe=out -column=DeviceName -unique \
        | sdds2stream -pipe -column=DeviceName]
set opList [exec sddssort $apsLinacRFWFDeviceFile -pipe=out -column=Operation -unique \
        | sdds2stream -pipe -column=Operation]

foreach dev $devList {
    set $dev 0
}

APSCheckButtonFrame .dev -parent .userFrame -label "Devices" \
      -buttonList $devList -variableList $devList -orientation horizontal \
      -limitPerRow 8 -allNone 1

set operation [lindex $opList 0]
APSRadioButtonFrame .op -parent .userFrame -label "Operation" \
  -valueList $opList -buttonList $opList -variable operation -orientation horizontal \
  -limitPerRow 4

set value 0
APSLabeledEntry .value -parent .userFrame -label "Value (for set operations): " \
    -textVariable value

set useCavput 1
APSRadioButtonFrame .cavput -parent .userFrame -label "Use cavput" \
    -valueList {1 0} -buttonList {Yes No} -variable useCavput -orientation horizontal \
    -limitPerRow 4 -contextHelp \
    "You may use cavput for output operations.  Try this if you have problems controlling supplies."

APSButton .doIt -parent .userFrame -text "Do It" -command DoIt

proc DoIt {} {
    global operation devList value APSDevSendDebug useCavput
        
    set supportedDevList [APSDevSend   -group rfWaveform -operation $operation]

    set devToDo ""
    foreach dev $devList {
        global $dev
        if {[set $dev] && [lsearch -exact $supportedDevList $dev]!=-1} {
            lappend devToDo $dev
        }
    }
    if [llength $devToDo]==0 {
        return -code error "No such operation."
    }

    set nonReadList [list Turn* Start* Stop* Begin* End*]
    if [string match Set* $operation]==1 {
        APSDevSend   -group rfWaveform -operation $operation -deviceList $devToDo -value $value  -cavput $useCavput
    } elseif {[lsearch -glob $nonReadList $operation]!=-1} {
        APSDevSend   -group rfWaveform -operation $operation -deviceList $devToDo  -cavput $useCavput
    } else {
        set resultList [APSDevSend   -group rfWaveform -operation $operation -deviceList $devToDo  -cavput $useCavput]
        set tmpFile /tmp/[APSTmpString]
        set fid [open $tmpFile w]
        foreach dev $devToDo result $resultList {
            puts $fid [format "%30s %s" $dev $result]
        }
        close $fid
        APSFileDisplayWindow [APSUniqueName .] -fileName $tmpFile -deleteOnClose 1
    }
}

APSLinkToLINACPVs
setStatus "Ready."
update
