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

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)]
#

set args $argv
set filename ""
set StartBumpList {0    34.0 56.5 79.0 101.5 124.0 146.5 169.0 191  214.0}
set EndBumpList   {29.0 51.5 74   96.5 119   141.5 164.0 186   209  240.0}
set region 0
APSParseArguments {region filename}

foreach start $StartBumpList end $EndBumpList {
    if [catch {exec cavput -list=B:region$region:CorrBumpStart=$start,B:region$region:CorrBumpStop=$end -pend=10 } result] {
        puts stderr "Error: $result"
        exit 1
    }
    incr region
}

if [catch {exec sddsprocess /home/helios/oagData/SCR/snapshots/BBPMWaveform/BBPMWaveform-boosterDefault.gz -pipe=out \
             -match=col,ControlName=B*:region*StartAO \
             | sddscasr -pipe=in -restore } result] {
    puts stderr "Error setting BPM timing: $result"
    exit 1
}

exit 0

set bpmTimeList  {20.0 40.0 62.5 85.0 107.5 130.0 152.5 175.0 197.5 220.0}
set bpmTimeList {20 41 64 86 109 126 154 176 198 220}
#move bpm timing ahead by 15ms for triggering at booster injection
#set bpmTimeList {5.0 25.0 47.5 70.0 92.5 115.0 137.5 160.0 182.5 205.0}
set iocList [exec sddsprocess  /home/helios/oagData/SCR/requestFiles/SBPMWaveform.config \
    -pipe=out -match=par,Location=Accel -match=par,BPMType=Booster | sdds2stream -pipe -par=IOCName]

for {set i 0} {$i<10} {incr i} {
    if [catch {exec cavput -list=[join $iocList ,] -list=P1:region${i}StartAO=[lindex $bpmTimeList $i] -pend=20} result] {
        puts stderr "Error setting booster bpm timing: $result"
        exit 1
    }
}

puts "done."
exit 0
