proc APSMpBoosterSwitchBPMConfigInfo {args} {
    return "setup booster FPGA bpms configs."
}

proc APSMpBoosterSwitchBPMConfigInitDialog {frame} {
    global APSMpBoosterSwitchBPMConfig
    
    pack [frame $frame.f1]
    set APSMpBoosterSwitchBPMConfig(bpmSelection) AverageBPM
    APSRadioButtonFrame .sel -parent $frame.f1 -label "Select booster bpm config:" \
      -buttonList {"AverageBPM" \
                     "xOnly turn by turn" \
                     "xOnly single turn injecton and average" \
                     "xOnly single turn injection" \
                     "xOnly single turn extraction" \
                     "xOnly single turn injection and extraction" \
                     "scope waveform (BPM ADC samples)" \
                     "yOnly turn by turn" \
                     "yOnly single turn injecton and average" \
                     "yOnly single turn injection" \
                     "yOnly single turn extraction" \
                     "yOnly single turn injection and extraction" \
      } \
      -variable APSMpBoosterSwitchBPMConfig(bpmSelection) \
      -valueList {boosterDefault singleTBTX singleTurnInjAveX singleTurnInjX singleTurnExtractX singleTurnInjExtX scopeWaveform \
               singleTBTY singleTurnInjAveY singleTurnInjY singleTurnExtractY singleTurnInjExtY}
    

}
proc APSMpBoosterSwitchBPMConfig {args} {
    set bpmSelection AverageBPM
    APSMpStep init -promptDialog APSMpBoosterSwitchBPMConfigInitDialog \
      -vars {bpmSelection}

    set snapDir /home/helios/oagData/SCR/snapshots/BBPMWaveform
    
    APSMpStep "disable averaging..."
    if [catch {exec cavput -list=B -list=1,2,3,4 -list=C -list=0,2,4,6,8 -list=:singleTurn:Enable=0,:intervalAvg:AutoRestart=0 \
                 -pend=30} result] {
        APSMpReturn error "Error arm booster single turn bpms: $result"
    }
    set scrFile $snapDir/BBPMWaveform-${bpmSelection}.gz
    set filename [file readlink $scrFile]
    set tmpRoot /tmp/[APSTmpString]
    APSAddToTmpFileList -ID booster -fileList $tmpRoot.restore
    if [catch {exec sddsprocess $scrFile $tmpRoot.restore \
                 -match=column,IsReadOnly=+y,! \
                 -match=column,IsProtected=+y,! } result] {
        APSMpReturn error "Error processing $scrFile: $result"
    }
    APSMpStep "Restore config for $bpmSelection..."
    if [catch {exec sddscasr -logFile=$tmpRoot.wf -restore $tmpRoot.restore -waveform=directory=$snapDir,onefile,extension=.waveform.gz,rootname=[file root [file tail $filename]] } result] {
        APSMpReturn error "Error restore config for $bpmSelection: $result"
    }
    APSAddToTmpFileList -ID booster -fileList $tmpRoot.wf
    set fd [open $tmpRoot.wf "r"]
    set errorInRestore 0
    while {![eof $fd]} {
        set text [gets $fd]
        if {[string match -nocase "*error*" $text]} {
            set errorInRestore 1
            break
        }
    }
    close $fd
    if $errorInRestore {
        APSFileDisplayWindow [APSUniqueName .fileDisp] -fileName $tmpRoot.wf \
          -comment "Error output from sddscasr" -deleteOnClose 1 \
          -printCommand "enscript -r"
        APSMpReturn  error "Error in restore booster single turn bpm config!"
    }
    if [regexp {singleTurnInj} $bpmSelection] {
        APSMpStep "restore single turn bpm setpoint for single turn injection controllaw..."
        if [catch {exec sddsprocess /home/helios/oagData/SCR/snapshots/Booster/Booster-SingleTurnPreferred.gz \
                 -pipe=out -match=col,ControlName=*singleTurn*setpointAO \
                 | sddscasr -pipe=in -restore -pend=30 -logFile=$tmpRoot.setpoint  } result] {
            APSMpReturn error "Error restore booster single turn bpm setpolint: $result"
        }
        APSAddToTmpFileList -ID booster -fileList $tmpRoot.setpoint
        set fd [open $tmpRoot.setpoint "r"]
        set errorInRestore 0
        while {![eof $fd]} {
            set text [gets $fd]
            if {[string match -nocase "*error*" $text]} {
                set errorInRestore 1
                break
            }
        }
        close $fd
        if $errorInRestore {
            APSFileDisplayWindow [APSUniqueName .fileDisp] -fileName $tmpRoot.setpoint \
              -comment "Error output from sddscasr" -deleteOnClose 1 \
              -printCommand "enscript -r"
            return -code error "Error in restore booster single turn bpm config!"
        }
        if [regexp {singleTurnInjAve} $bpmSelection] {
            APSMpStep "Arming non-single turn bpms..."
            if [catch {exec cavput -list=B -list=2,3,4 -list=C -list=0,2,4,6,8 -list=:intervalAvg:AutoRestart=1,:turn:gtr:arm=1 \
                         -pend=30} result] {
                APSMpReturn error "Error arm bpms: $result"
            }
            if [catch {exec cavput -list=B4C8,B1C0,B1C2,B1C4,B1C6,B1C8 -list=:intervalAvg:AutoRestart=0,:turn:gtr:arm=0 \
                         -pend=30} result] {
                APSMpReturn error "Error arm bpms: $result"
            }
            APSMpStep "enable single turn bpms..."
            if [catch {exec cavput -list=B4C8,B1C0,B1C2,B1C4,B1C6,B1C8 -list=:singleTurn:Enable=1 -pend=30 } result] {
                APSMpReturn error "Error enabling single turn bpms: $result"
            }
        } else {
            APSMpStep "enable all single turn bpms..."
            if [catch {exec cavput -list=B -list=1,2,3,4 -list=C -list=0,2,4,6,8 \
                         -list=:intervalAvg:AutoRestart=0,:turn:gtr:arm=0  -pend=30 } result] {
                APSMpReturn error "Error disable auto arm for all booster bpms: $result"
            }
            if [catch {exec cavput -list=B -list=1,2,3,4 -list=C -list=0,2,4,6,8 \
                         -list=:singleTurn:AutoRestart=1,:singleTurn:Delay=0,:singleTurn:Enable=1 -pend=30 } result] {
                APSMpReturn error "Error enabling single turn for all booster bpms: $result"
            }
        }
        #arm singlue tur bpms
    } elseif [regexp {singleTurn} $bpmSelection] {
        APSMpStep "enable single turn bpms..."
        if [catch {exec cavput -list=B -list=1,2,3,4 -list=C -list=0,2,4,6,8 \
                     -list=:intervalAvg:AutoRestart=0,:turn:gtr:arm=0  -pend=30 } result] {
            APSMpReturn error "Error disable auto arm for all booster bpms: $result"
        }
        if [catch {exec cavput -list=B -list=1,2,3,4 -list=C -list=0,2,4,6,8 \
                     -list=:singleTurn:AutoRestart=1,:singleTurn:Delay=0,:singleTurn:Enable=1 -pend=30 } result] {
            APSMpReturn error "Error enabling single turn for all booster bpms: $result"
        }
    } elseif [regexp {TBT} $bpmSelection] {
        APSMpStep "setup turn-by-turn BPMs..."
        if [catch {exec cavput -list=B -list=1,2,3,4 -list=C -list=0,2,4,6,8 -list=:intervalAvg:AutoRestart=0,:turn:gtr:arm=1 \
                     -pend=30} result] {
            APSMpReturn error "Error arm bpms: $result"
        }
    } else {
        APSMpStep "setup average BPMs..."
        if [catch {exec cavput -list=B -list=1,2,3,4 -list=C -list=0,2,4,6,8 -list=:intervalAvg:AutoRestart=1,:turn:gtr:arm=1 \
                     -pend=30} result] {
            APSMpReturn error "Error arm bpms: $result"
        }
    }
    
    APSMpStep "done."
    APSMpReturn ok
}

