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

# $Log: not supported by cvs2svn $
# Revision 1.6  2006/03/21 21:37:52  soliday
# Updated to improve sorting.
#
# Revision 1.5  2003/05/22 18:23:07  shang
# the narrowband bpms are obtained from the bpm config file now and the P0 comparison
# is removed
#
# Revision 1.4  2002/10/11 18:24:41  emery
# Creates NbBPM type PVs for bpms that are actually connected
# to NbBPM electronics.The variable extraNbBPMList has to be edited.
#
# Revision 1.3  2000/10/30 23:19:00  emery
# Removed *:sum PVs from ID and BM bpms types.
# Forgot to include y standard bpms.
#
# Revision 1.2  2000/10/28 09:53:33  emery
# Corrected problem with sddssort command options.
#
# Revision 1.1  2000/10/27 02:09:25  emery
# First installation.
#

puts "NOT USED"
puts "Run utils/monitoring/makeSRBPMAveLoggerInput instead"
exit

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 root srBPMs
set usage {usage: makeSRBPMsInputFile [-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 {
    set lastFile [lindex [lsort [glob ${root}.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*15*13]

set ControlName ""
set ReadbackName ""
set ReadbackUnits ""
set CNxSuffixList {:ms:x :ms:x:ErrorCC}
set RNxSuffixList {:ms:x :ms:x:ErrorCC}
set CNySuffixList {:ms:y :ms:y:ErrorCC}
set RNySuffixList {:ms:y :ms:y:ErrorCC}
set UnitsList {mm mm}
set P0CNSuffixList {:agc :sum}
set P0RNSuffixList {:agc :sum}
set P0UnitsList {Volts Volts}
set PnCNSuffixList {:sum.RVAL}
set PnRNSuffixList {:sum.RVAL}
set PnUnitsList {""}

set dataDir /home/helios/oagData/sr/BPMStatus
#set extraNbBPMList {S11B:P1 S12A:P1}
# x plane
if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out \
             -filter=col,NonexistentH,0,0,OkForLoggingH,1,1,& \
             | sdds2stream -col=DeviceName -pipe \
         } xBPMs] {
    puts stderr "Error for $argv0 in [pwd]: $xBPMs"
    exit 1
}
# y plane
if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out \
             -filter=col,NonexistentV,0,0,OkForLoggingV,1,1,& \
             | sdds2stream -col=DeviceName -pipe \
         } yBPMs] {
    puts stderr "Error for $argv0 in [pwd]: $yBPMs"
    exit 1
    }
set extraNbBPMList [exec sddsprocess $dataDir/config.sdds \
                      -pipe=out -match=col,ElectronicsType=Narrowband \
                      -noWarnings | sdds2stream -pipe -col=DeviceName]
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]
    }
    if {[lsearch $extraNbBPMList $bpm] > -1} {
        set items [llength $P0CNSuffixList]
        for {set index 0} {$index<$items} {incr index} {
            lappend ControlName ${bpm}[lindex $P0CNSuffixList $index]
            lappend ReadbackName ${bpm}[lindex $P0RNSuffixList $index]
            lappend ReadbackUnits [lindex $P0UnitsList $index]
        }
    } elseif [regexp {BM:P[12]} $bpm] {
        # no additional PVs for BM bpms
    } elseif [regexp {ID:P[12]} $bpm] {
        # no additional PVs for ID bpms
    } elseif [regexp {P[12345]} $bpm] {
        set items [llength $PnCNSuffixList]
        for {set index 0} {$index<$items} {incr index} {
            lappend ControlName ${bpm}[lindex $PnCNSuffixList $index]
            lappend ReadbackName ${bpm}[lindex $PnRNSuffixList $index]
            lappend ReadbackUnits [lindex $PnUnitsList $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]
    }
    if {[lsearch $extraNbBPMList $bpm] > -1} {
        set items [llength $P0CNSuffixList]
        for {set index 0} {$index<$items} {incr index} {
            lappend ControlName ${bpm}[lindex $P0CNSuffixList $index]
            lappend ReadbackName ${bpm}[lindex $P0RNSuffixList $index]
            lappend ReadbackUnits [lindex $P0UnitsList $index]
        }
    } elseif [regexp {BM:P[12]} $bpm] {
        # no additional PVs for BM bpms
    } elseif [regexp {ID:P[12]} $bpm] {
        # no additional PVs for ID bpms
    } elseif [regexp {P[12345]} $bpm] {
        set items [llength $PnCNSuffixList]
        for {set index 0} {$index<$items} {incr index} {
            lappend ControlName ${bpm}[lindex $PnCNSuffixList $index]
            lappend ReadbackName ${bpm}[lindex $PnRNSuffixList $index]
            lappend ReadbackUnits [lindex $PnUnitsList $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 extraPVs.mon -pipe=out -merge \
| sddssort -pipe \
  -col=ControlName -unique -num \
| sddsprocess -pipe \
  -print=column,ControlType,pv -print=column,ControlMode,RO \
  -edit=column,SimpleControlName,ControlName,S?/./K \
| sddsprocess -pipe=in $output.tmp2 -print=column,Provider,ca -print=column,ExpectNumeric,y,type=character -print=column,ExpectFieldType,scalar -define=column,ExpectElements,1,type=long

file delete $output.tmp

exec sddsselect $output.tmp2 /home/helios/oagData/pvdata/iocRecNamesOAG.sdds -pipe=out -match=SimpleControlName=rec_name -nowarning -reuse | sddsconvert -pipe=in $output -delete=column,SimpleControlName

file delete $output.tmp2


set newFile $output
if $install {
    puts stderr "New  file: [exec sdds2stream -rows $newFile]"
    
    if [string length $lastFile] {
        exec sddsselect $newFile $lastFile PVsAdded -match=ControlName -invert
        exec sddsselect $lastFile $newFile PVsRemoved -match=ControlName -invert
        puts stderr "Last file: [exec sdds2stream -rows $lastFile]"
        puts stderr "PVs added: [exec sdds2stream -rows PVsAdded]"
        puts stderr "PVs removed: [exec sdds2stream -rows PVsRemoved]"
    }
    if [file exists ${root}.mon] {
        file delete ${root}.mon
    }
    exec ln -s $newFile $root.mon
}

exit
