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

# $Log: not supported by cvs2svn $
# Revision 1.1  2000/12/04 21:09:45  emery
# First installation
#

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

set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds
set sectorList [APSGetSDDSColumn -fileName /home/helios/oagData/sr/BPLDs/sectors.sdds -column Sector]
foreach sector $sectorList {
    set sectorP1 [expr $sector+1]
    exec sddsprocess IDrfBPM.mon.template IDrfBPM.mon.$sector \
      -reedit=column,ControlName,%/<sector>/$sector/%/<sectorP1>/$sectorP1/ \
      -reedit=column,ReadbackName,%/<sector>/$sector/%/<sectorP1>/$sectorP1/ 
    lappend fileList IDrfBPM.mon.$sector
}

set root SRFastLog1
set generation 0
set newFile ""
while 1 {
    set lastFile $newFile
    set newFile [format ${root}.mon-%04ld $generation]
    if ![file exists $newFile] {
        break
    }
    incr generation
}

if [catch {eval exec sddscombine $fileList SRFastLog1Fixed.mon -merge -pipe=out | \
             sddsprocess -pipe \
             -edit=col,rec_name,ControlName,S?/./D | \
             sddssort -column=rec_name -pipe | \
             sddsxref -reuse -nowarnings -pipe $recordFile -match=rec_name | \
             sddsconvert -pipe -delete=col,rec_name | \
             sddssort -pipe -column=ControlName -unique -num | \
             sddsprocess -pipe=in $newFile -print=column,Provider,ca -print=column,ExpectNumeric,y,type=character -print=column,ExpectFieldType,scalar -define=column,ExpectElements,1,type=long} result] {
    puts stderr "Error: $result"
    exit
}

catch {eval file delete $fileList}
puts stderr "New  file: [exec sdds2stream -rows $newFile]"

if [string length $lastFile] {
    puts stderr "Last file: [exec sdds2stream -rows $lastFile]"
}

if [file exists ${root}.mon] {
    file delete ${root}.mon
}
exec ln -s $newFile ${root}.mon
exit
