#!/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)]
APSStandardSetup
set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds

# Based on makeSRBPMAveInputFile
set rootname dispersion
set usage {usage: makeSRBPMDispMeasInputFile [-output <filename> | -install 1]}
set output ""
set install 0
set args $argv
if {[APSStrictParseArguments {output install}] || \
      (![string length $output] && !$install) || \
      ([string length $output] && $install)} {
    puts stderr "Error for $argv0 in [pwd]: $usage"
    exit 1
}
if !$install {
    if [file exists $output] {
        puts stderr "Error for $argv0 in [pwd]: $output exists"
        exit 1
    }
} else {
    if [catch {glob ${rootname}.mon-????} result ] {
        puts stderr "problem finding files matching ${rootname}.mon-???? in [pwd]"
        puts stderr "Run makeSRBPMDispMeasInputFile -output ${rootname}.mon-0000 and link ${rootname}.mon to it."
        exit 1
    }
    set lastFile [lindex [lsort [glob ${rootname}.mon-????]] end]
    set output [APSNextGenerationedName -name $lastFile -newFile 1]
}

set fid [sdds open $output.tmp w]
sdds defineColumn $fid ControlName -type SDDS_STRING
sdds defineColumn $fid ReadbackName -type SDDS_STRING
sdds defineColumn $fid ReadbackUnits -type SDDS_STRING
sdds writeLayout $fid
sdds startPage $fid [expr 40*28*2*2]

set ControlName ""
set ReadbackName ""
set ReadbackUnits ""
set CNxSuffixList {:x:SampledAdjustedM :x:LowPass1sAdjustedM}
set RNxSuffixList {:x:SampledAdjustedM :x:LowPass1sAdjustedM}
set CNySuffixList {:y:SampledAdjustedM :y:LowPass1sAdjustedM}
set RNySuffixList {:y:SampledAdjustedM :y:LowPass1sAdjustedM}
set UnitsList {microns microns}

# x plane
set tmpRoot /tmp/[APSTmpString]
# The list of bpms is obtained from the bpm status file, thus
# relieving the need to generate it here in duplication of effort.
set dataDir /home/helios/oagData/sr/BPMStatus

if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out \
               -filter=col,NonexistentH,0,0  \
               -match=col,ElectronicsType=LB+ \
               -match=col,DeviceName=*ID*,!,DeviceName=*BM*,!,& \
             | sdds2stream -col=DeviceName -pipe \
         } xBPMs] {
    puts stderr "Error for $argv0 in [pwd]: $xBPMs"
    exit 1
}

if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out \
               -filter=col,NonexistentV,0,0 \
               -match=col,ElectronicsType=LB+ \
               -match=col,DeviceName=*ID*,!,DeviceName=*BM*,!,& \
	     | tee $tmpRoot.y \
             | sdds2stream -col=DeviceName -pipe \
         } yBPMs] {
    puts stderr "Error for $argv0 in [pwd]: $yBPMs"
    exit 1
}

file delete $tmpRoot.x $tmpRoot.y 

foreach bpm $xBPMs {
    set items [llength $CNxSuffixList]
    for {set index 0} {$index<$items} {incr index} {
        lappend ControlName ${bpm}[lindex $CNxSuffixList $index]
        lappend ReadbackName ${bpm}[lindex $RNxSuffixList $index]
        lappend ReadbackUnits [lindex $UnitsList $index]
    }
}

foreach bpm $yBPMs {
    set items [llength $CNySuffixList]
    for {set index 0} {$index<$items} {incr index} {
	    lappend ControlName ${bpm}[lindex $CNySuffixList $index]
	    lappend ReadbackName ${bpm}[lindex $RNySuffixList $index]
	    lappend ReadbackUnits [lindex $UnitsList $index]
    }
}

eval sdds setColumn $fid ControlName $ControlName
eval sdds setColumn $fid ReadbackName $ReadbackName
eval sdds setColumn $fid ReadbackUnits $ReadbackUnits
sdds writePage $fid
sdds close $fid

# remove duplicate control names when combining x and y names
exec sddscombine $output.tmp /home/helios/oagData/sr/dispChrom/inputFiles/extraPVs.mon -pipe=out -merge \
    | sddsprocess -pipe \
    -edit=col,rec_name,ControlName,S?/./D \
    | sddssort -pipe -col=rec_name \
    | sddsxref -reuse -nowarnings -pipe $recordFile -match=rec_name \
    | sddsconvert -pipe -delete=col,rec_name,ioc_name \
    | sddssort -pipe=in $output  \
    -col=ControlName -unique

file delete $output.tmp

if $install {
    if [catch {file delete $rootname.mon
        exec ln -s $output $rootname.mon
        exec sddsselect $lastFile $output removedPVs -invert -match=ControlName
        exec sddsselect $output $lastFile addedPVs -invert -match=ControlName \
             } result] {
        puts stderr "Error for $argv0 in [pwd]: $result"
        exit 1
    }
}

exit
# Local Variables:
# mode: tcl
# End:
