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

# $Log: not supported by cvs2svn $
# Revision 1.37  2010/06/10 15:21:33  shang
# due to the non-existent pvs of FPGA bpms, the existing template can not be used for FPGA bpms. Therefore, added special FPGABPM controls templated and FPGA bpms sectors to deal with FPGA bpms.
#
# Revision 1.36  2009/08/04 21:42:42  shang
# fixed a problem in sddscombine that the output file is one of the input files.
#
# Revision 1.35  2009/03/17 21:16:45  shang
# remvoed timing waveforms
#
# Revision 1.34  2009/01/23 22:40:27  shang
# add sorting the request file by ControlName
#
# Revision 1.33  2009/01/23 22:33:41  shang
# changed the isNumerical for waveform pvs from 'y' to 'n', since their values are written as "WaveformPV" in the snapshot file. use 'y' gave errors in SCR operations.
#
# Revision 1.32  2009/01/21 22:15:37  shang
# added more FPGA pvs
#
# Revision 1.31  2009/01/20 15:43:55  emery
# Fixed regexp for beamline determination.
#
# Revision 1.30  2009/01/19 21:18:32  emery
# Undid the last change for inclusion of waveforms using additional
# request file. The correct way to include array PVs is simply to put the PV
# names in the regular request file, which is done here.
#
# Revision 1.29  2009/01/19 17:01:06  emery
# Remove force from file delete command. For some reason it gave an error.
#
# Revision 1.28  2009/01/19 16:54:48  emery
# Added creation of SBPMs.waveform file for saving ControlRam waveforms
# in sectors that have bpm data acquisition controlled by FPGA.
#
# Revision 1.27  2005/11/09 00:17:25  shang
# now sorted by sector and ControlName.
#
# Revision 1.26  2003/10/29 00:24:49  emery
# Removed too-specific reference of narrowband bpms as P0 bpms and now
# filter for narrowband bpms correctedly using ElectronicsType in bpm
# status file.
#
# Revision 1.25  2002/10/02 19:05:43  shang
# replaced burtrb by obtaining pvs from iocRecName.sdds
#
# Revision 1.24  2002/06/20 15:40:47  emery
# Increased the connect pend time for the burtrb command.
#
# Revision 1.23  2002/06/20 13:28:17  borland
# Creates IsReadOnly column
#
# Revision 1.22  2002/05/22 17:20:20  emery
# Adding check whether all bpm PVs exist.
#
# Revision 1.21  2001/08/06 15:55:24  emery
# Corrected which files PVsRemoved and PVsAdded goes as outputs of sddsselect.
#
# Revision 1.20  2001/08/06 15:45:13  emery
# Added processing to create PVsAdded and PVsRemoved files.
#
# Revision 1.19  2001/05/04 17:58:53  emery
# Added processing on P0 request file results.
#
# Revision 1.18  2001/05/04 16:18:11  emery
# Added IsProtected column filtering to request file.
#
# Revision 1.17  2001/05/02 22:04:41  emery
# Use a new column in the template file to remove even-sector
# from PVs that exists only on odd sectors (plus those > 35).
#
# Revision 1.16  2001/04/19 18:24:09  emery
# Fixed a bug where the only one P0 bpms was selected for each sector.
#
# Revision 1.15  2001/04/19 17:58:25  emery
# Forgot a } in previous change.
#
# Revision 1.14  2001/04/19 17:57:11  emery
# Added additional condition on removing the bunch clock generator PV.
#
# Revision 1.13  2001/04/19 17:41:58  emery
# Added clarifying comments and removed extraneous if statement.
#
# Revision 1.12  2001/02/06 14:51:46  emery
# Added processing of new SCDU template.
#
# Revision 1.11  2001/01/29 18:39:59  emery
# Use file /home/helios/oagData/sr/NewTiming/sectors.sdds to
# determine which sectors have new timing PVs.
#
# Revision 1.10  2001/01/29 17:57:52  emery
# Use file /home/helios/oagData/sr/DSPXrayBPMs/sectors.sdds to
# determine which sectors are present with DSP Xray bpm data.
#
# Revision 1.9  2001/01/12 21:13:07  emery
# Removed special sectors list. Added sectors 1 4 13 14 to
# sectors with new Xray bpms PVs.
#
# Revision 1.8  2001/01/11 14:50:23  emery
# Added sectors 27 28 to new timing PVs.
#
# Revision 1.7  2000/10/31 03:46:38  emery
# removed sector 35 from list of sectors that DAS PVs should be removed.
#
# Revision 1.6  2000/10/31 02:21:03  emery
# Added to sector list of Xray bpms. Made
# sector 2 and 3 special in that *gain* PVs were
# removed form request file.
#
# Revision 1.5  2000/10/28 09:05:39  emery
# Fixed case of "gain" in filter.
#
# Revision 1.4  2000/10/28 08:20:25  emery
# Removed debugging puts stderr from last time.
#
# Revision 1.3  2000/10/28 08:18:12  emery
# Fitlered out some new timing PVs from even sectors.
# Removed selected blade gain PVs from ID:P? bpms
# depending on type.
#
# Revision 1.2  2000/10/27 23:38:48  emery
# Remove EF and CD blade PVS from appropriate component files.
#
# Revision 1.1  2000/10/27 02:09:24  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 rootname SBPMs
set usage {usage: makeSBPMReqFile [-output <filename> | -install 1]}
set output ""
set install 0
set args $argv
set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds

if {[APSStrictParseArguments {output install}] || \
      (![string length $output] && !$install) || \
      ([string length $output] && $install)} {
    puts stderr "Error (0) for $argv0 in [pwd]: $usage"
    exit 1
}

cd /home/helios/oagData/SCR/requestFiles/SBPMfiles
if !$install {
    if [file exists $output] {
        puts stderr "Error (1) for $argv0 in [pwd]: $output exists"
        exit 1
    }
} else {
    set pwd [pwd]
    cd ..
    set lastFile [lindex [lsort [glob $rootname.req-????]] end]
    set output ../[APSNextGenerationedName -name $lastFile -newFile 1]
    cd $pwd
}

set dataDir /home/helios/oagData/sr/BPMStatus

set newTimingSectors [exec sdds2stream -col=Sector \
                        /home/helios/oagData/sr/NewTiming/sectors.sdds]
set DSPXraySectors [exec sdds2stream -col=Sector \
                      /home/helios/oagData/sr/DSPXrayBPMs/sectors.sdds]
set newSCDUSectors [exec sdds2stream -col=Sector \
                        /home/helios/oagData/sr/NewSCDU/sectors.sdds]
#set FPGAbpmSectors [exec sdds2stream -col=Sector \
#                      /home/helios/oagData/sr/FPGAbpm/sectors.sdds]
#fpga bpm sectors are from 1 to 40 now,

for {set sector 1} {$sector<41} {incr sector} {
    # LZ means leading zeroes
    set sectorLZ [format %02ld $sector]
   # if {[lsearch -exact $FPGAbpmSectors $sector]>=0} {
   #     set templateFile FPGABPMControlsTemplate.req
   # } elseif {[lsearch -exact $newTimingSectors $sector]>=0} {
   #     set templateFile SBPMControlsTemplateNewTiming.req
   # } else {
   #     set templateFile SBPMControlsTemplate.req
   # }
    #changed because fpga bpm sectors are from 1 to 40
    set templateFile FPGABPMControlsTemplate.req
    exec sddsprocess $templateFile -pipe=out \
      -edit=col,ControlName,ControlNameTemplate,5%/<sector>/$sector/5%/<sectorLZ>/$sectorLZ/ \
      -edit=col,Beamline,BeamlineTemplate,5%/<sector>/$sector/5%/<sectorLZ>/$sectorLZ/ \
      | sddsconvert -pipe=in S${sectorLZ}BPMControls.req \
      -dele=col,ControlNameTemplate,BeamlineTemplate

    if {-1 < [lsearch -exact $newTimingSectors $sector]} {
        # remove even sector bunch clock PVs where there is no bunch clock
        if { [expr !($sector%2)] && $sector<35 } {
            exec sddsprocess S${sectorLZ}BPMControls.req -noWarning \
              -match=col,IocRelated=y,!
            file delete S${sectorLZ}BPMControls.req~
        }
    }
    lappend fileList  S${sectorLZ}BPMControls.req
}


foreach sector $DSPXraySectors {
    set sectorLZ [format %02ld $sector]
    exec sddsprocess SBPMControlsTemplateXray.req -pipe=out \
      -edit=col,ControlName,ControlNameTemplate,5%/<sector>/$sector/5%/<sectorLZ>/$sectorLZ/ \
      -edit=col,Beamline,BeamlineTemplate,5%/<sector>/$sector/5%/<sectorLZ>/$sectorLZ/ \
      | sddsconvert -pipe=in S${sectorLZ}BPMControlsXray.req \
      -dele=col,ControlNameTemplate,BeamlineTemplate
    # some processing for the ID bpms
    set bpmData [exec sddsprocess $dataDir/config.sdds -pipe=out \
                   -match=col,DeviceName=S${sector}ID:P*,DeviceName=S${sector}BM:P*,| \
               | sdds2stream -col=DeviceName,DeviceType -pipe]
    set matchOption ""
    foreach {bpm type} $bpmData {
        switch -exact $type {
            "Xray ABCD" {
            # remove PV related to E and F blades
                append matchOption " -match=col,ControlName=${bpm}:\\\[EF\\\]:*,!"
                append matchOption " -match=col,ControlName=${bpm}:\\\[HV\\\]gain*,!"
            }
            "Xray ABEF" {
            # remove PV related to C and D blades
                append matchOption " -match=col,ControlName=${bpm}:\\\[CD\\\]:*,!"
                append matchOption " -match=col,ControlName=${bpm}:gain*,!"
            }
        }
    }
    eval exec sddsprocess S${sectorLZ}BPMControlsXray.req -noWarning $matchOption
    file delete S${sectorLZ}BPMControlsXray.req~
    lappend fileList S${sectorLZ}BPMControlsXray.req
}

foreach sector $DSPXraySectors {
    # DAS PVs exists only in odd sectors, so remove the DAS PV for even sectors
    if {0==[expr $sector%2]} {
        set sectorLZ [format %02ld $sector]
        exec sddsprocess S${sectorLZ}BPMControlsXray.req -noWarning \
          -match=col,IocRelated=y,!
    }
}

foreach sector $newSCDUSectors {
    set sectorLZ [format %02ld $sector]
    set templateFile SBPMControlsTemplateNewSCDU.req
    exec sddsprocess $templateFile -pipe=out \
      -edit=col,ControlName,ControlNameTemplate,5%/<sector>/$sector/5%/<sectorLZ>/$sectorLZ/ \
      -edit=col,Beamline,BeamlineTemplate,5%/<sector>/$sector/5%/<sectorLZ>/$sectorLZ/ \
      | sddsconvert -pipe=in S${sectorLZ}BPMControlsNewSCDU.req \
      -dele=col,ControlNameTemplate,BeamlineTemplate
    lappend fileList  S${sectorLZ}BPMControlsNewSCDU.req
}

# Make a file with Narrowband BPM sector controls
# We should no longer refer to narrowband bpms by P0 part of device name
# but by the electronics type data.
set tmpNB /tmp/[APSTmpString]
if [catch {exec sddsprocess $dataDir/config.sdds $tmpNB.0 \
             -match=col,ElectronicsType=Narrowband \
             -filter=col,NonexistentH,1,1,NonexistentV,1,1,&,! \
             -filter=col,OkForLoggingV,1,1,OkForLoggingH,1,1,| \
         } result] {
    puts stderr "Error (2) for $argv0 in [pwd]: $result"
    catch {eval file delete $fileList}
    exit 1
}

if [catch {exec sddsprocess $tmpNB.0 -pipe=out \
             -scan=column,Sector,DeviceName,S%ld,type=long \
             | sddssort -column=Sector -unique -pipe=in $tmpNB.1
    exec sddsselect NbBPMs-master.req $tmpNB.1 $tmpNB \
             -equate=Sector -reuse=row} result] {
    puts stderr "Error (3) for $argv0 in [pwd]: $result"
    catch {eval file delete $fileList}
    exit 1
}
lappend fileList $tmpNB 

# make a file with Nb BPM individual controls
if [catch {exec sddsselect NbBPMs-master.req $tmpNB.0 $tmpNB.select \
                -match=BPMName=DeviceName -reuse -nowarning} result] {
    puts stderr "Error (3.5) for $argv0 in [pwd]: $result"
    catch {eval file delete $fileList}
    exit 1
}

lappend fileList $tmpNB.select

set tmpfile /tmp/[APSTmpString]
if [catch {eval exec sddscombine $fileList [glob -nocomplain SBPMControlsExtra*.req] \
            -retain=column,ControlType,ControlName,Beamline,Category,IsNumerical,IsProtected,Tolerance \
             -merge -pipe=out \
             | sddsprocess -pipe -reprint=column,IsReadOnly,n \
             | sddssort -pipe=in -col=Beamline -column=ControlName -unique $tmpfile} result] {
    puts stderr "Error (6) for $argv0 in [pwd]: $result"
   # catch {eval file delete $fileList}
    puts stderr $fileList
    exit 1
}

# determine which sectors need a waveform PV for control ram.
if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out \
             -match=col,ElectronicsType=FPGA \
             -filter=col,NonexistentH,1,1,NonexistentV,1,1,&,! \
             -filter=col,OkForLoggingV,1,1,OkForLoggingH,1,1,| \
             -edit=col,SectorPart,DeviceName,s/:/bD \
             | sddssort -pipe -unique \
             -col=SectorPart \
             | sdds2stream -pipe -col=SectorPart} sectorPartList] {
    puts stderr "Error (8) for $argv0 in [pwd]: $sectorPartList"
    exit 1
}

#add other pvs for FPGA 
if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out -nowarnings \
             -match=col,ElectronicsType=FPGA \
             -filter=col,NonexistentH,1,1,NonexistentV,1,1,&,! \
             -filter=col,OkForLoggingV,1,1,OkForLoggingH,1,1,| \
             -scan=col,Sector,DeviceName,S%ld,type=long \
             | sddssort -pipe -unique \
             -col=Sector \
             | sdds2stream -pipe -col=Sector} sectorList] {
    puts stderr "Error (9) for $argv0 in [pwd]: $sectorList"
    exit 1
}
if [llength $sectorList] {
    set fileList ""
    set index 0
    foreach sector $sectorList {
	if [catch {exec replaceText /home/helios/oagData/SCR/requestFiles/SBPMs.req.template \
		       "-orig=<sector>,<sectorf>" -repl=$sector,[format %02ld $sector] $tmpfile.${index} } result] {
	    puts stderr "Error (10) for $argv0 in [pwd]: $result"
	    exit 1
	}
	lappend fileList $tmpfile.$index
	incr index
    } 
    if [catch {eval exec sddscombine $fileList $tmpfile $tmpfile.1a -merge -over} result] {
	 puts stderr "Error (11) for $argv0 in [pwd]: $result"
	exit 1
    }
    exec mv $tmpfile.1a $tmpfile
}

#Add in S27ID and S35ID GRID-XBPMs
if {[catch {exec sddscombine $tmpfile S27ID-SCR.req S35ID-SCR.req $tmpfile.c -merge} result ]} {
    puts stderr "Error for $argv0 in [pwd]: $result"
    exit 1
}
exec mv $tmpfile.c $tmpfile

# check whether all PVs exist
if [catch {exec sddsprocess $tmpfile -pipe=out \
             -edit=col,rec_name,ControlName,S?/./D \
             | tee /tmp/tests \
             | sddsxref -pipe $recordFile -match=rec_name -reuse -nowarning  \
             | sddssort -pipe -col=ControlName -numericHigh -unique \
             | sddsconvert -pipe=in -delete=col,rec_name $output \
         } result] {
    puts stderr "Error (7) for $argv0 in [pwd]: $result"
    exit 1
}

if $install {
    set pwd [pwd]
    cd ..
    if [file exists ${rootname}.req] {
        if [catch {file delete ${rootname}.req} result] {
            puts stderr "Error (7) for $argv0 in $pwd: $result"
            exit 1  
        }
    }
    catch {exec chmod -w $rootname.req}
    if [catch {exec ln -s [file tail $output] ${rootname}.req} result] {
        puts stderr "Error (8) for $argv0 in $pwd: $result"
        exit 1
    }
    if [catch {APSMakeSCRCategoryFiles -input ${rootname}.req} result] {
        puts stderr "Error (9) for $argv0 in $pwd: $result"
        exit 1
    }
    exec sddsselect $lastFile $rootname.req -invert PVsRemoved \
      -match=ControlName
    exec sddsselect $rootname.req $lastFile -invert PVsAdded \
      -match=ControlName
}

exit 0
