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

#
# $Log: not supported by cvs2svn $
# Revision 1.4  2002/01/26 19:10:25  borland
# Now uses LTPBPM group for LTP BPMs.  Previous, the PV table referenced
# LinacBPM, which didn't work for other applications.
#
# Revision 1.3  2001/11/03 15:30:33  borland
# Now allows Linac or LTP BPMs to be controlled.
#
# Revision 1.2  2000/10/27 15:55:54  borland
# Improved handling of set/read/other operatinos.
#
# Revision 1.1  2000/10/27 15:32:55  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: borland $"


set args $argv
set beamline ""
APSStrictParseArguments {beamline}

if {$beamline == "LTP"} {
    set name "LTP BPM Utility"
    set group LTPBPM
    set BPMDeviceFile /home/helios/oagData/deviceConfig/par/LTPBPMOps.pvTable
} elseif {$beamline == "linac" || $beamline==""} {
    set name "Linac BPM Utility"
    set group LinacBPM
    set beamlineDir $beamline
    set BPMDeviceFile /home/helios/oagData/deviceConfig/linac/BPMOps.pvTable
} else {
    puts "Usage: LinacPSUtil -beamline <beamline>\n\nValid beamlines include linac and LTP\n"
    exit
}

APSApplication . -name "$name" \
  -overview "Allows executing ganged operations on $beamline BPMs."

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

proc setStatus {text} {
    APSSetVarAndUpdate status $text
}

setStatus "Working..."
update


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

foreach dev $devList {
    set $dev 0
}

APSCheckButtonFrame .bpm -parent .userFrame -label "BPM 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 group
        
    set supportedDevList [APSDevSend -releaseLinks 0  -group $group -operation $operation]
    set APSDevSendDebug 0

    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."
    }

    if {[string match Set* $operation]} {
        APSDevSend -releaseLinks 0  -group $group -operation $operation -deviceList $devToDo -value $value -timeout 10 -cavput $useCavput
    } elseif {[string match Turn* $operation] || \
            [string match Start* $operation] || [string match Do* $operation]} {
        APSDevSend -releaseLinks 0  -group $group -operation $operation -deviceList $devToDo -timeout 10 -cavput $useCavput
    } else {
        set resultList [APSDevSend -releaseLinks 0  -group $group -operation $operation -deviceList $devToDo -timeout 30 -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
    }
}

APSLinkToDevicePVs -fileList $BPMDeviceFile
setStatus "Ready."
update
