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

# $Log: not supported by cvs2svn $
# Revision 1.13  2006/02/28 23:30:01  emery
# Added C:P0 as polluted bpms for all conditions, not
# just for P0's as main steering bpms.
#
# Revision 1.12  2006/02/06 15:46:24  emery
# Added C:P0 bpms to polluted list of bpms.
#
# Revision 1.11  2005/08/03 17:55:14  emery
# Added comments describing the output files and what other procedures
# use them.
#
# Revision 1.10  2005/08/03 16:29:14  emery
# Changed command from setFACL-oagPlus to setfacl using
# configuration file files.acl in the same directory.
# That way, if permissions are to be
# changed we don't have to make a new version of this script.
#
# Revision 1.9  2005/07/07 18:49:03  emery
# Added a setfacl command to allow users permission to
# delete the lightSources.config  userLightSources.config sourcePointPollution.xref for later deletion (when new versions are made).
#
# Revision 1.8  2005/03/16 21:48:42  emery
# Added APSStandardSetup
#
# Revision 1.7  2002/10/04 15:56:01  emery
# Removed rows from polluted bpm file of AffectedBPMS
# has non-existing bpms in boths planes.
#
# Revision 1.6  2001/06/28 03:57:20  emery
# Added BM x-ray bpms as polluted bpms so that the values
# get transfered when the BM light source point is
# steered. This is helpful to a controllaw that uses
# the BM x-ray bpms for orbit correction.
#
# Revision 1.5  2000/12/06 20:30:46  emery
# Fixed bug in lsearch command where the result would always
# make the light source bpms as P1 even if P0 were present and functional.
#
# Revision 1.4  2000/11/02 22:55:28  emery
# Added a required -reuse option to sddsselect.
#
# Revision 1.3  2000/10/31 03:44:41  emery
# Corrected syntax in a sddsxref command.
#
# Revision 1.2  2000/10/31 03:41:13  emery
# Added sddsselect before the sddsxref to select out
# the nonexisting rows. This is to prevent a warning
# message to occur.
#
# Revision 1.1  2000/10/27 02:09:22  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)]
APSDebugPath
APSStandardSetup

# makes files:
# lightSources.config         all forty sector
# userLightSources.config     actual user occupied sectors
# sourcePointPollution.xref   bpm within local steering bumps
#
# lightSources.config         used by APSMpSRSteering.tcl
# userLightSources.config     used by SRxxSteering family of GUI
# sourcePointPollution.xref   used by APSMpSRSteering.tcl
#
# Used by APSMpSRStering.tcl 
# To simplify matters, ID steering is no longer restricted
# to those sectors with beamlines. 
#
# new P0 bpms are detected through the procedure
# APSSRGetInstalledBPMList
#
# The AM and BM steering is no longer restricted
# to those sectors with beamlines. 
#
# added option useDirectoryLinks so that one can update
# the files based not on the bpm installation status
# but on the directory links that may have been
# changed during operations in case a P0 or P1 bpm is not working.
#################################

set args $argv
set useDirectoryLinks 0
APSStrictParseArguments {useDirectoryLinks}

set rootDir /home/helios/oagData/sr/localSteering/
set xrefDir /home/helios/oagData/sr/lattices/scripts

cd $rootDir

set tmpfile1 /tmp/[APSTmpString]
set fid1 [open $tmpfile1 w]

puts $fid1 SDDS1
puts $fid1 "&parameter name=Description type=string &end"
puts $fid1 "&parameter name=NameType type=string &end"
puts $fid1 "&column name=Name type=string &end"
puts $fid1 "&data mode=ascii no_row_counts=1 &end"
puts $fid1 "Light source bpms"
puts $fid1 "MonitorNames"

# make list of sectors which has both P0's working
if [catch {APSSRGetWorkingP0SectorsList} P0SectorList] {
    puts stderr "Error (1) for $argv0 in [pwd]: $P0Sector"
    exit 1
}

proc ReadCurrentLinks {} {
    global idBPM
    set linkDir /home/helios/oagData/sr/localSteering/lattices/default/IDs

    for {set i 1} {$i <= 40} {incr i} {
        set id ${linkDir}/[format %02ld $i]ID
        set idLink [file readlink $id] 
        if {[string range $idLink 4 4] == 1} {
            set idBPM($i) P1
        } else {
            set idBPM($i) P0
        } 
    } 
}

if $useDirectoryLinks {
    ReadCurrentLinks
}

for {set sector 1} {$sector < 41} {incr sector} {
    set sector1 [expr $sector + 1]
    if [expr $sector1 == 41] {
        set sector1 1
    }
    if !$useDirectoryLinks {
        if {[lsearch -exact $P0SectorList ${sector}] != -1} {
            puts $fid1 "S${sector}B:P0"
            puts $fid1 "S${sector1}A:P0"
        } else {
            puts $fid1 "S${sector}B:P1"
            puts $fid1 "S${sector1}A:P1"
        }
    } else {
        puts $fid1 "S${sector}B:$idBPM($sector)"
        puts $fid1 "S${sector1}A:$idBPM($sector)"
    }
    puts $fid1 "S${sector}B:P4"
    puts $fid1 "S${sector}B:P3"
    puts $fid1 "S${sector}A:P3"
    puts $fid1 "S${sector}A:P4"
}

close $fid1

APSArchiveGenerationedCopy -name lightSources.config -remove 1
if [catch {exec sddsselect $tmpfile1 $xrefDir/SRBPMPosition.xref -pipe=out \
             -match=Name=BPMName -noWarning \
             |  sddsxref -pipe $xrefDir/SRBPMPosition.xref \
             -match=Name=BPMName -take=s \
             | sddssort -pipe=in -column=s lightSources.config \
         } result ] {
    puts stderr $result
    exit
}
# need to give special permission to users for later deletion
#exec setfacl -f files.acl lightSources.config

set tmpfile1 /tmp/[APSTmpString]
set fid1 [open $tmpfile1 w]

puts $fid1 SDDS1
puts $fid1 "&parameter name=Description type=string &end"
puts $fid1 "&parameter name=NameType type=string &end"
puts $fid1 "&column name=Name type=string &end"
puts $fid1 "&data mode=ascii no_row_counts=1 &end"
puts $fid1 "Light source bpms"
puts $fid1 "MonitorNames"

set sectorList [APSGetSDDSColumn -column Sector \
                  -fileName /home/helios/oagData/sr/IDs/sectors.sdds]

foreach sector $sectorList {
    set sector1 [expr $sector + 1]
    if [expr $sector1 == 41] {
        set sector1 1
    }
    if !$useDirectoryLinks {
        if {[lsearch -exact $P0SectorList ${sector}] != -1} {
            puts $fid1 "S${sector}B:P0"
            puts $fid1 "S${sector}C:P0"
            puts $fid1 "S${sector1}A:P0"
        } else {
            puts $fid1 "S${sector}B:P1"
            puts $fid1 "S${sector}C:P0"
            puts $fid1 "S${sector1}A:P1"
        }
    } else {
        puts $fid1 "S${sector}B:$idBPM($sector)"
        puts $fid1 "S${sector}C:P0"
        puts $fid1 "S${sector1}A:$idBPM($sector)"
    }
}

set BMsectorList [APSGetSDDSColumn -column Sector \
                  -fileName /home/helios/oagData/sr/BMs/sectors.sdds]

foreach sector $BMsectorList {
    puts $fid1 "S${sector}B:P4"
    puts $fid1 "S${sector}B:P3"
}
set AMsectorList [APSGetSDDSColumn -column Sector \
                  -fileName /home/helios/oagData/sr/AMs/sectors.sdds]
foreach sector $AMsectorList {
    puts $fid1 "S${sector}A:P3"
    puts $fid1 "S${sector}A:P4"
}

close $fid1

APSArchiveGenerationedCopy -name userLightSources.config -remove 1
if [catch {exec sddsselect $tmpfile1 $xrefDir/SRBPMPosition.xref -pipe=out \
             -match=Name=BPMName -noWarning \
             | sddsxref -pipe $xrefDir/SRBPMPosition.xref \
             -match=Name=BPMName -take=s \
             | sddsprocess -pipe -define=column,Flag,1,type=short \
             | sddssort -pipe=in -column=s userLightSources.config \
         } result ] {
    puts stderr $result
    exit
}
#exec setfacl -f files.acl userLightSources.config

set CUdataDir /home/helios/oagData/sr/XAXSTF
if [catch {exec sddsprocess $CUdataDir/sectors.sdds -pipe=out \
             -filter=col,CUflag,1,1 \
             | sdds2stream -pipe -col=Sector} CUlist] {
    return -code error "$CUlist"
}


APSAddToTmpFileList -ID LSFiles -fileList "$tmpfile1"

# make polluted bpm file
#################################
set tmpfile2 /tmp/[APSTmpString]
set fid2 [open $tmpfile2 w]

puts $fid2 SDDS1
puts $fid2 "&parameter name=Description type=string &end"
puts $fid2 "&column name=SourcePointBPM type=string &end"
puts $fid2 "&column name=AffectedBPM type=string &end"
puts $fid2 "&data mode=ascii no_row_counts=1 &end"
puts $fid2 "bpms affected by steering of Light source bpms"

for {set sector 1} {$sector < 41} {incr sector} {
    set sector1 [expr $sector + 1]
    if [expr $sector1 == 41] {
        set sector1 1
    }
    if !$useDirectoryLinks {
        if {[lsearch -exact $P0SectorList ${sector}] != -1} {
            # if P0 is steered then P1 and P2 are polluted bpms
            puts $fid2 "S${sector}B:P0 S${sector}B:P2"
            puts $fid2 "S${sector}B:P0 S${sector}B:P1"
            puts $fid2 "S${sector}B:P0 S${sector}C:P0"
            puts $fid2 "S${sector}B:P0 S${sector1}A:P1"
            puts $fid2 "S${sector}B:P0 S${sector1}A:P2"
            puts $fid2 "S${sector1}A:P0 S${sector}B:P2"
            puts $fid2 "S${sector1}A:P0 S${sector}B:P1"
            puts $fid2 "S${sector1}A:P0 S${sector}C:P0"
            puts $fid2 "S${sector1}A:P0 S${sector1}A:P1"
            puts $fid2 "S${sector1}A:P0 S${sector1}A:P2"
            # we are now wanting to use xray bpm's setpoint for after
            # a steering. This will obviate the need of removal of
            # the xray bpms and redo a gap scan.
            if {-1 < [lsearch $sectorList $sector]} {
                if {-1 < [lsearch $CUlist $sector]} {
                    puts $fid2 "S${sector}B:P0 S${sector}ID:P1"
                    puts $fid2 "S${sector1}A:P0 S${sector}ID:P2"
                } else {
                    puts $fid2 "S${sector}B:P0 S${sector}ID:P1"
                    puts $fid2 "S${sector}B:P0 S${sector}ID:P2"
                    puts $fid2 "S${sector1}A:P0 S${sector}ID:P1"
                    puts $fid2 "S${sector1}A:P0 S${sector}ID:P2"
                }
            }
        } else {
            # if P1 is steered then P2 are polluted bpms,
            # and probably P0 if they are installed.
            puts $fid2 "S${sector}B:P1 S${sector}B:P2"
            puts $fid2 "S${sector}B:P1 S${sector}B:P0"
            puts $fid2 "S${sector}B:P1 S${sector}C:P0"
            puts $fid2 "S${sector}B:P1 S${sector1}A:P0"
            puts $fid2 "S${sector}B:P1 S${sector1}A:P2"
            puts $fid2 "S${sector1}A:P1 S${sector}B:P2"
            puts $fid2 "S${sector1}A:P1 S${sector}B:P0"
            puts $fid2 "S${sector1}A:P1 S${sector}C:P0"
            puts $fid2 "S${sector1}A:P1 S${sector1}A:P0"
            puts $fid2 "S${sector1}A:P1 S${sector1}A:P2"
            if {-1 < [lsearch $sectorList $sector]} {
               if {-1 < [lsearch $CUlist $sector]} {
                   puts $fid2 "S${sector}B:P1 S${sector}ID:P1"
                   puts $fid2 "S${sector1}A:P1 S${sector}ID:P2"
               } else {
                   puts $fid2 "S${sector}B:P1 S${sector}ID:P1"
                   puts $fid2 "S${sector}B:P1 S${sector}ID:P2"
                   puts $fid2 "S${sector1}A:P1 S${sector}ID:P1"
                   puts $fid2 "S${sector1}A:P1 S${sector}ID:P2"
               }
            }
        }
    } else {
        if {$idBPM($sector) == "P1"} {
            puts $fid2 "S${sector}B:$idBPM($sector) S${sector}B:P0"
            puts $fid2 "S${sector}B:$idBPM($sector) S${sector}C:P0"
            puts $fid2 "S${sector}B:$idBPM($sector) S${sector1}A:P0"
            puts $fid2 "S${sector1}A:$idBPM($sector) S${sector}B:P0"
            puts $fid2 "S${sector1}A:$idBPM($sector) S${sector}C:P0"
            puts $fid2 "S${sector1}A:$idBPM($sector) S${sector1}A:P0"
        } else {
            puts $fid2 "S${sector}B:$idBPM($sector) S${sector}B:P1"
            puts $fid2 "S${sector}B:$idBPM($sector) S${sector}C:P0"
            puts $fid2 "S${sector}B:$idBPM($sector) S${sector1}A:P1"
            puts $fid2 "S${sector1}A:$idBPM($sector) S${sector}B:P1"
            puts $fid2 "S${sector1}A:$idBPM($sector) S${sector}C:P0"
            puts $fid2 "S${sector1}A:$idBPM($sector) S${sector1}A:P1"
        }
        puts $fid2 "S${sector}B:$idBPM($sector) S${sector}B:P2"
        puts $fid2 "S${sector}B:$idBPM($sector) S${sector1}A:P2"
        puts $fid2 "S${sector1}A:$idBPM($sector) S${sector}B:P2"
        puts $fid2 "S${sector1}A:$idBPM($sector) S${sector1}A:P2"
        if {-1 < [lsearch $sectorList $sector]} {
            if {-1 < [lsearch $CUlist $sector]} { 
                puts $fid2 "S${sector}B:$idBPM($sector) S${sector}ID:P1"
                puts $fid2 "S${sector1}A:$idBPM($sector) S${sector}ID:P2"
            } else {
                puts $fid2 "S${sector}B:$idBPM($sector) S${sector}ID:P1"
                puts $fid2 "S${sector}B:$idBPM($sector) S${sector}ID:P2"
                puts $fid2 "S${sector1}A:$idBPM($sector) S${sector}ID:P1"
                puts $fid2 "S${sector1}A:$idBPM($sector) S${sector}ID:P2"
            }
        }
    }
    puts $fid2 "S${sector}B:P4 S${sector}A:P2"
    puts $fid2 "S${sector}B:P4 S${sector}A:P3"
    puts $fid2 "S${sector}B:P4 S${sector}A:P4"
    puts $fid2 "S${sector}B:P4 S${sector}B:P5"
    puts $fid2 "S${sector}B:P4 S${sector}B:P2"
    puts $fid2 "S${sector}B:P3 S${sector}A:P2"
    puts $fid2 "S${sector}B:P3 S${sector}A:P3"
    puts $fid2 "S${sector}B:P3 S${sector}A:P4"
    puts $fid2 "S${sector}B:P3 S${sector}B:P5"
    puts $fid2 "S${sector}B:P3 S${sector}B:P2"
    if {-1 < [lsearch $BMsectorList $sector]} {
        puts $fid2 "S${sector}B:P4 S${sector}BM:P1"
        puts $fid2 "S${sector}B:P4 S${sector}BM:P2"
        puts $fid2 "S${sector}B:P3 S${sector}BM:P1"
        puts $fid2 "S${sector}B:P3 S${sector}BM:P2"
    }
    
    puts $fid2 "S${sector}A:P4 S${sector}A:P2"
    puts $fid2 "S${sector}A:P4 S${sector}B:P5"
    puts $fid2 "S${sector}A:P4 S${sector}B:P4"
    puts $fid2 "S${sector}A:P4 S${sector}B:P3"
    puts $fid2 "S${sector}A:P4 S${sector}B:P2"
    puts $fid2 "S${sector}A:P3 S${sector}A:P2"
    puts $fid2 "S${sector}A:P3 S${sector}B:P5"
    puts $fid2 "S${sector}A:P3 S${sector}B:P4"
    puts $fid2 "S${sector}A:P3 S${sector}B:P3"
    puts $fid2 "S${sector}A:P3 S${sector}B:P2"
}

close $fid2

# remove any bpms that don't exist
set statusDir /home/helios/oagData/sr/BPMStatus
exec sddsxref $tmpfile2 $statusDir/config.sdds -pipe=out \
    -match=SourcePointBPM=DeviceName -reuse \
    -take=NonexistentH,NonexistentV \
| sddsprocess -pipe \
    -filter=col,NonexistentH,0,0,NonexistentV,0,0,| \
| sddsconvert -pipe=in $tmpfile2.1 \
    -dele=col,NonexistentH,NonexistentV

exec sddsxref $tmpfile2.1 $statusDir/config.sdds -pipe=out \
    -match=AffectedBPM=DeviceName -reuse \
    -take=NonexistentH,NonexistentV \
| sddsprocess -pipe \
    -filter=col,NonexistentH,0,0,NonexistentV,0,0,| \
| sddsconvert -pipe=in $tmpfile2.2 \
    -dele=col,NonexistentH,NonexistentV

#APSAddToTmpFileList -ID LSFiles -fileList "$tmpfile2 $tmpfile2.1 $tmpfile2.2" 

APSArchiveGenerationedCopy -name sourcePointPollution.xref -remove 1
if [catch {exec sddsselect $tmpfile2.2 $xrefDir/SRBPMPosition.xref -pipe=out \
             -match=SourcePointBPM=BPMName -noWarning -reuse \
             | sddsxref -pipe $xrefDir/SRBPMPosition.xref \
             -match=SourcePointBPM=BPMName -take=s -reuse \
             | sddssort -pipe=in -column=s sourcePointPollution.xref \
         } result ] {
    puts stderr $result
    exit
}
#exec setfacl -f files.acl  sourcePointPollution.xref

exit
