#!/bin/sh  
# \
  exec oagwish "$0" "$@"
#
#  $Log: not supported by cvs2svn $
#
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)]

#wm geometry . +10+10

set buttonOrientation horizontal
set fontSize regular

set configDir  /home/helios/oagData/sr/BPMStatus
set configFile $configDir/config.sdds

set BPMList {A:P0 A:P1 A:P2 A:P3 A:P4 B:P5 B:P4 B:P3 B:P2 B:P1 B:P0 C:P0 ID:P1 ID:P2 BM:P1 BM:P2}
set missingListH [exec sddsprocess $configFile -pipe=out -filter=col,NonexistentH,1,1 \
                           | sdds2stream -col=DeviceName -pipe]
set missingListV [exec sddsprocess $configFile -pipe=out -filter=col,NonexistentV,1,1 \
                           | sdds2stream -col=DeviceName -pipe]
proc LoadBPMStatusConfig {args} {
    
    global labelList configDir configFile firstRead BPMList missingListH missingListV bpmList
    
    set bpmList [exec sdds2stream -col=DeviceName $configFile]
    SetStatus "Read $configFile..."
    foreach plane {H V} {
        foreach label $labelList {
            SetStatus "Read $label ..."
            set rootname [string tolower $plane]${label} 
            global $rootname ${rootname}CBWidget
            set onList [exec sddsprocess -pipe=out $configFile -filter=col,${label}${plane},1,1 \
                          | sdds2stream -pipe -col=DeviceName]
            
            APSSetSRSectorButtons -mode all-off -rootname ${rootname} \
              -sectorCount 40 -itemList $BPMList
            foreach bpm $onList {
                set ${rootname}($bpm) 1
                [set ${rootname}CBWidget($bpm)] invoke
            }

        }
    }
    SetStatus "done."
    update
}



proc SetStatus {text} {
    global status
    set status $text
    update
}
set labelList {OkForDCOrbitCorrection OkForFastDCOrbitCorrection \
    OkForRTFeedback OkForSteering OkForLogging OkForBeamHistory \
    OkForRMSCalculation}

set configFile $configDir/config.sdds


set status ""
APSApplication . -name SRBPMStatusConfigViewer -version 1 \
  -overview {This application displays the SR BPM status configurations with colored buttons.}
APSScrolledStatus .status -parent .userFrame -textVariable status -width 70 \
    -height 5 -packOption "-fill both -expand true"


set tabList [APSTabFrame .tab -parent .userFrame -label "" \
    -labelList "Horizontal Vertical" -height 500 -width 1100]


foreach widget $tabList plane {H V} {
    set root SRBPMStatus$plane
    set widgetList [APSTabFrame .tabs -parent $widget -width 1100 -height 450 \
                      -labelList $labelList -label ""]
    
    foreach win $widgetList label $labelList {
        set rootname [string tolower $plane]${label}
        set  ${rootname}BadBPMList [set missingList$plane]
        
        APSSRMonitorCheckButtons .smbut -parent $win -rootname $rootname \
            -orientation $buttonOrientation -BP5Only 0  \
            -includeP0s 1 -includeIDs 1 -includeBMs 1 \
            -missingListVar ${rootname}BadBPMList \
            -noLabel 1 -packOption "-side top" 
    }
}

APSLabeledEntry .file -parent .userFrame -label "Config:" -width 80 \
  -textVariable configFile -fileSelectButton 1 -fileSelectPattern config* -fileSelectPath $configDir \
  -contextHelp "SR BPM status configuration file."
APSButton .read -parent .userFrame.file -packOption "-side right" -text "Read" \
  -contextHelp "read selected BPM status configuration." \
  -command LoadBPMStatusConfig

LoadBPMStatusConfig
