#!/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 args $argv
set plane ""
APSParseArguments {plane}
set plane [string tolower $plane]
switch ${plane} {
    h {
        set planeList H
    }
    v {
        set planeList V
    }
    default {
        set planeList {H V}
    }
}
set buttonOrientation horizontal
set fontSize regular


set HCorList {A:H1 A:H2 A:H3 A:H4 B:H4 B:H3 B:H2 B:H1 C:H1}
set VCorList {A:V1 A:V2 A:V3 A:V4 B:V4 B:V3 B:V2 B:V1 C:V1}
proc LoadCorrStatusConfig {args} {
    set plane ""
    APSParseArguments {plane}
    global labelList HconfigFile VconfigFile ${plane}CorList
    
    set configFile [set ${plane}configFile]
    SetStatus "Read $plane config $configFile ..."
    foreach label $labelList {
        set rootname [string tolower $plane]${label}
        global $rootname ${rootname}CBWidget
        set onList [exec sddsprocess $configFile -pipe=out -filter=col,$label,1,1 \
            | sdds2stream -pipe -col=DeviceName]
        
        APSSetSRSectorButtons -mode all-off -rootname ${rootname} \
            -sectorCount 40 -itemList [set ${plane}CorList]
        foreach corr $onList {
            set ${rootname}($corr) 1
            if [info exist ${rootname}CBWidget($corr)] {
                [set ${rootname}CBWidget($corr)] invoke
            }
        }
    }
    SetStatus "done."
    set firstRead$plane 0
    update
}



proc SetStatus {text} {
    global status
    set status $text
    update
}

set labelList {OkForDCOrbitCorrection OkForFastDCOrbitCorrection \
    OkForRTFeedback OkForSteering}
set HconfigDir /home/helios/oagData/sr/HCorrectorStatus
set VconfigDir  /home/helios/oagData/sr/VCorrectorStatus
set HconfigFile $HconfigDir/config.sdds
set VconfigFile $VconfigDir/config.sdds


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

if [llength $planeList]==2 {
    set tabList [APSTabFrame .tab -parent .userFrame -label "" \
                   -labelList $planeList -height 450 -width 1100]
} else {
    set tabList .userFrame
}

foreach widget $tabList plane $planeList {
    set root SRBPMStatus$plane
    set widgetList [APSTabFrame .tabs -parent $widget -width 1100 -height 320 \
                      -labelList $labelList -label ""]
    set configFile [set ${plane}configFile]
  
    set missingList [exec sddsprocess -pipe=out $configFile -filter=col,Nonexistent,1,1 \
                       | sdds2stream -pipe=in -col=DeviceName]
    foreach win $widgetList label $labelList {
        set rootname [string tolower $plane]${label}
        set ${rootname}BadCORRECTORList $missingList
        APSSRCorrectorCheckButtons .shbut -parent $win\
          -rootname ${rootname} -plane [string tolower $plane] \
          -orientation $buttonOrientation \
          -missingListVar ${rootname}BadCORRECTORList \
          -noLabel 1 -packOption "-side top"
        
    }
    set configDir [set ${plane}configDir]
    APSLabeledEntry .file -parent $widget -label "Config:" -width 80 \
      -textVariable ${plane}configFile -fileSelectButton 1 -fileSelectPattern "config*" \
      -fileSelectPath $configDir \
      -contextHelp "SR $plane plane corrector status configuration file."
    APSButton .read -parent $widget.file -packOption "-side right" -text "Read" \
        -contextHelp "read selected $plane plane corrector status configuration." \
        -command "LoadCorrStatusConfig -plane $plane"
    LoadCorrStatusConfig -plane $plane
}

