#!/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)]
APSDebugPath

# generates slow beam and turn history monitor files for FPGA bpms.
# should be regenerated at each shutdown because
# the list of bpms can change.
set usage {usage: makeSBPMReqFile [-install 1]}
set install 0
set args $argv

APSParseArguments {install}

if [catch {exec sddsprocess /home/helios/oagData/sr/BPMStatus/config.sdds -pipe=out -nowarnings \
             -match=col,ElectronicsType=FPGA \
             -filter=col,NonexistentH,1,1,NonexistentV,1,1,&,! \
             -filter=col,OkForLoggingV,1,1,OkForLoggingH,1,1,| \
             -scan=col,Sector,DeviceName,S%ld,type=long \
             | sddssort -pipe -unique \
             -col=Sector \
             | sdds2stream -pipe -col=Sector} sectorList] {
    puts stderr "Error (1) for $argv0 in [pwd]: $sectorList"
    exit 1
}
set tmpFile /tmp/[APSTmpString]
foreach sector $sectorList {
    if [catch {exec sddsprocess /home/helios/oagData/SCR/requestFiles/SBPMWaveform.config \
                 "-match=par,IOCName=S${sector}A,IOCName=S${sector}B,|" -pipe=out \
                 | sddscombine -pipe -merge \
                 | sdds2stream -pipe -col=BPM } bpmList] {
        puts stderr "Error (2) for $argv0 in [pwd]: $sectorList"
        exit 1
    }
    foreach type {motion turnHistory} {
        foreach bpm $bpmList {
            foreach plane {X Y} {
                foreach nm {position sum} {
                    lappend ${type}PVList $bpm:$type:${plane}$nm
                }
            }
        }
    }
}

set slowOutput [APSNextGenerationedName -name FPGABPMSlowHistory.wmon-0000 -newFile 1 -separator -]
set turnOutput [APSNextGenerationedName -name FPGABPMTurnHistory.wmon-0000 -newFile 1 -separator -]

if [catch {exec sddsmakedataset -col=WaveformPV,type=string -data=[join $motionPVList ,] -pipe=out \
             -par=WaveformLength,type=long -data=2048 \
             | sddsprocess -pipe=in $slowOutput -print=col,WaveformName,%s,WaveformPV 
    exec sddsmakedataset -col=WaveformPV,type=string -data=[join $turnHistoryPVList ,] -pipe=out \
             -par=WaveformLength,type=long -data=262144 \
             | sddsprocess -pipe=in $turnOutput -print=col,WaveformName,%s,WaveformPV } result] {
    puts  stderr "Error (2) for $argv0 in [pwd]: $sectorList"
    exit 1
}

if $install {
    exec rm FPGABPMTurnHistory.wmon
    exec rm FPGABPMSlowHistory.wmon 
    exec ln -s $turnOutput FPGABPMTurnHistory.wmon
    exec ln -s $slowOutput FPGABPMSlowHistory.wmon 
}
set pvList ""
foreach sector $sectorList {
    lappend pvList S${sector}A:TurnHistoryTrigTimeSI
    lappend pvList S${sector}B:TurnHistoryTrigTimeSI
    lappend pvList S${sector}A:MotionTrigTimeSI
    lappend pvList S${sector}B:MotionTrigTimeSI
}
set monOutput [APSNextGenerationedName -name FPGABpms.mon-0000 -newFile 1 -separator -]

if [catch {exec sddsmakedataset $monOutput -col=ControlName,type=string \
             -data=[join $pvList ,] } result] {
    puts stderr $result
}

if $install {
    exec rm FPGABpms.mon
    exec ln -s $monOutput FPGABpms.mon
}

exit 0

