#!/bin/sh  
# \
exec oagtclsh "$0" "$@"
#
# $Log: not supported by cvs2svn $
# Revision 1.3  2003/10/27 22:50:54  soliday
# Updated to oagtclsh.
#
# Revision 1.2  1996/10/18 19:57:52  borland
# Added APSStandardSetup call.
#
# Revision 1.1  1996/06/26  21:10:13  borland
# First version per N. Sereno.
#
#

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

# Tcl script to check the storage ring BPM local bus addresses.  If there are address errors,
# the script sends the error list via e-mail to E. Kahana, and J. Crissup as well as notifying
# them by e-mail page.  Finally, the script deletes the BPM .sdds and .txt list files and pager
# files created.

set BPMLabelList [list {A:P1} {A:P2} {A:P3} {A:P4} {B:P5} {B:P4} {B:P3} {B:P2} {B:P1}]
set BPMLabelCommand "-list=A:P1,A:P2,A:P3,A:P4,B:P5,B:P4,B:P3,B:P2,B:P1"
set BPMcmd "exec cavget -list=S -range=beg=1,end=40 $BPMLabelCommand -list=:raddr_li.VAL -label -pend=10 \"-delim= \""

catch $BPMcmd SRBPMBusAddressesAndValue
set BPMLocalBusErrorList ""
set listIndex 0
set badBPMCount 0

cd /home/helios/oagData/sr/checkScripts

for {set sector 1} {$sector <= 40} {incr sector 1} {
        set bpmAddressIndex 0
        foreach bpmLabel $BPMLabelList {
                set SRBPMName [join "S $sector $bpmLabel" ""]
                if {($SRBPMName == "S1A:P1") || ($SRBPMName == "S1B:P1")} {
                    incr bpmAddressIndex
                    incr listIndex 2
                    continue
                }
                set SRBPMAddressValue [lindex $SRBPMBusAddressesAndValue [expr $listIndex + 1]]
                if {$SRBPMAddressValue!=$bpmAddressIndex} {
                        incr badBPMCount 1
                        if {$badBPMCount=="1"} {
                                set badBPMFile "/tmp/SRBadBPM[APSTmpString].sdds"
                                set badBPMFileID [open $badBPMFile w+]
                                puts $badBPMFileID "SDDS1\n&description\ntext = \"SR Bad BPM List\",  contents = \"SR Bad BPMs vs Sector\", &end\n&column\nname = SRSector,  symbol = SRSector,  description = SRSector,  type = string, &end\n&column\nname = BPM, symbol = BPM, description = SRBPM, type=string, &end\n&column\nname = BPMIOCName, symbol = BPMIOCName, description = BPMIOCName, type=string, &end\n&column\nname = BusAddress, symbol = BusAddress, description = BusAddress, type=string, &end\n&column\nname = DetectedBusAddress, symbol = DetectedBusAddress, description = DetectedBusAddress, type=string, &end\n&data\nmode = ascii, no_row_counts=1 &end"
                        }
                        if {$sector<=34} {
                                if {fmod($sector,2)=="1" && fmod([expr $sector + 1],2)=="0" && $sector<=34} {
                                        set IOCNumber $sector
                                } else {set IOCNumber [expr $sector - 1]}
                                        set SRBPMIOCName "iocs${IOCNumber}bpm"
                                } elseif {$sector>34 && $sector<=41} {
                                        set SRBPMIOCName "iocs${sector}bpm"
                                }
                        puts $badBPMFileID "$sector $SRBPMName $SRBPMIOCName $bpmAddressIndex $SRBPMAddressValue"
                }
                incr bpmAddressIndex
                incr listIndex 2
        }
}

if {$badBPMCount!=0} {       
        close $badBPMFileID
        set createBadBPMFileCmd "sddsprocess -pipe=out ${badBPMFile} \"-print=param,ErrorMessage,List of BPMs with local bus address errors,type=string\" \"-define=param,BadBPMCount,$badBPMCount,type=long\" | sddsprintout -pipe=in -param -col > /tmp/SRBPMDisplayFile.txt"
        set createBadBPLDBPMFileCmd "sddsselect -pipe=out ${badBPMFile} BPLDNames.sdds -match=BPM=BPLDBPMNames | sddsprocess -pipe=in,out \"-print=param,ErrorMessage,List of BPLD BPMs with local bus address errors.  These errors must be fixed now.,type=string\" -define=param,BadBPLDBPMCount,n_rows,type=long | sddsprintout -pipe=in -param -col > /tmp/SRBPLDBPMDisplayFile.txt"
        eval exec $createBadBPMFileCmd
        eval exec $createBadBPLDBPMFileCmd
        set pagerMessageFileID [open /tmp/pagerMessage.txt w+]              
        puts $pagerMessageFileID "SR BPM local bus address errors occurred.  Check your e-mail for a list of BPMs with \
                                  errors."
        close $pagerMessageFileID

#        catch {exec mail kahana@aps.anl.gov < /tmp/SRBPMDisplayFile.txt }
        catch {exec mail soliday@aps.anl.gov < /tmp/SRBPLDBPMDisplayFile.txt }

        catch {exec rm /tmp/pagerMessage.txt /tmp/SRBPMDisplayFile.txt /tmp/SRBPLDBPMDisplayFile.txt \
                 $badBPMFile}
        
        exit 1
}
exit 1
