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

# $Log: not supported by cvs2svn $
# Revision 1.14  2005/12/19 22:36:07  shang
# sort the ControlName with numeric high and added checking added and removed PVs.
#
# Revision 1.13  2005/11/09 00:17:25  shang
# now sorted by sector and ControlName.
#
# Revision 1.12  2005/08/08 19:26:45  emery
# Changed wildcard for rf x setpoint to include all rf bpms.
#
# Revision 1.11  2003/03/22 03:17:55  emery
# Added removal of ioc_name
#
# Revision 1.10  2002/10/02 18:58:30  shang
# replaced burtrb by obtaining pvs from iocRecName.sdds
#
# Revision 1.9  2002/05/31 18:07:57  soliday
# Fixed a bug from the last change.
#
# Revision 1.8  2002/05/31 17:53:15  emery
# Set tolerance for offset to 0.01 mm
#
# Revision 1.7  2001/11/14 16:34:22  emery
# Removed DP* PVs.
#
# Revision 1.6  2001/10/26 16:10:50  emery
# Added datapool AdjustP PVs.
#
# Revision 1.5  2001/08/15 08:28:00  emery
# Added RF x setpoint PVs for bpms.
#
# Revision 1.4  2001/01/29 17:57:53  emery
# Use file /home/helios/oagData/sr/DSPXrayBPMs/sectors.sdds to
# determine which sectors are present with DSP Xray bpm data.
#
# Revision 1.3  2001/01/12 21:12:13  emery
# Added 1 2 3 4 13 14 to sectorList according to AOD-DIA instructions.
#
# Revision 1.2  2000/10/31 00:11:50  emery
# Added sector 33 to Xray bpm offsets.
#
# 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

#cd /home/helios/oagData/SCR/requestFiles
set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds
set rootname SROrbitSetPt
set usage {usage: makeOrbitSetPtReqFile [-output <filename> | -install 1] [-mode {SR|normal}]}
set output ""
set install 0
set mode normal
set args $argv
if {[APSStrictParseArguments {output install mode}] || \
      (![string length $output] && !$install) || \
      ([string length $output] && $install)} {
    puts stderr "Error for $argv0 in [pwd]: $usage"
    exit 1
}
if [file exist $rootname.req] {
    set lastFile [file readlink $rootname.req]
} else {
    set lastFile ""
}
if !$install {
    if [file exists $output] {
        puts stderr "Error for $argv0 in [pwd]: $output exists"
        exit 1
    }
} else {
    if ![string length $lastFile] {
        set lastFile $rootname.req-0000
    }
    set output [APSNextGenerationedName -name $lastFile -separator - -newFile 1]
}

# Use BPMStatus file
set dataDir /home/helios/oagData/sr/BPMStatus
if [catch {exec sddsprocess $dataDir/config.sdds xSetpoints \
             -filter=col,NonexistentH,0,0 \
             -print=col,Beamline,Setpoint \
             -print=col,Category,x \
             -define=col,Tolerance,0.0,type=double \
             "-print=col,Description,x setpoint of %s,DeviceName" \
             -print=col,IsProtected,n,type=character \
             -edit=column,ControlName,DeviceName,ei/:ms:x:SetpointAO/ 
    exec sddsprocess xSetpoints xRFSetpoints \
             -match=col,DeviceName=S*\[AB\]:P? \
             -reprint=col,Beamline,Setpoint \
             -reprint=col,Category,xRF \
             "-reprint=col,Description,x RF-frequency setpoint of %s,DeviceName" \
             -reprint=column,ControlName,%s:ms:x:RfSetpointP,DeviceName
    exec sddsprocess $dataDir/config.sdds ySetpoints \
             -filter=col,NonexistentV,0,0 \
             -print=col,Beamline,Setpoint \
             -print=col,Category,y \
             -define=col,Tolerance,0.0,type=double \
             "-print=col,Description,y setpoint of %s,DeviceName" \
             -print=col,IsProtected,n,type=character \
             -edit=column,ControlName,DeviceName,ei/:ms:y:SetpointAO/ 
    exec sddsprocess $dataDir/config.sdds xOffsets.rf \
               -filter=col,NonexistentH,0,0 \
               -match=col,DeviceType=Xray*,! \
               -print=col,Beamline,Offset \
               -print=col,Category,x \
               -define=col,Tolerance,0.010,type=double \
               "-print=col,Description,x offset of %s,DeviceName" \
               -print=col,IsProtected,n,type=character \
               -edit=column,ControlName,DeviceName,ei/:ms:x:OffsetAO/ 
    exec sddsprocess $dataDir/config.sdds yOffsets.rf \
               -filter=col,NonexistentV,0,0 \
               -match=col,DeviceType=Xray*,! \
               -print=col,Beamline,Offset \
               -print=col,Category,y \
               -define=col,Tolerance,0.010,type=double \
               "-print=col,Description,y offset of %s,DeviceName" \
               -print=col,IsProtected,n,type=character \
               -edit=column,ControlName,DeviceName,ei/:ms:y:OffsetAO/ 
    exec sddsprocess $dataDir/config.sdds xOffsets.xray \
               -filter=col,NonexistentH,0,0 \
               -match=col,DeviceType=Xray* \
               -print=col,Beamline,Offset \
               -print=col,Category,x \
               -define=col,Tolerance,0.010,type=double \
               "-print=col,Description,x offset of %s,DeviceName" \
               -print=col,IsProtected,y,type=character \
               -edit=column,ControlName,DeviceName,ei/:ms:x:OffsetAO/ 
    exec sddsprocess $dataDir/config.sdds yOffsets.xray \
               -filter=col,NonexistentV,0,0 \
               -match=col,DeviceType=Xray* \
               -print=col,Beamline,Offset \
               -print=col,Category,y \
               -define=col,Tolerance,0.010,type=double \
               "-print=col,Description,y offset of %s,DeviceName" \
               -print=col,IsProtected,y,type=character \
               -edit=column,ControlName,DeviceName,ei/:ms:y:OffsetAO/ 
    exec sddscombine xSetpoints ySetpoints \
        xOffsets.rf yOffsets.rf \
        xOffsets.xray yOffsets.xray \
             xRFSetpoints -pipe=out \
             -merge \
             | sddsconvert -pipe \
             -retain=col,ControlName,Beamline,Category,Description,Tolerance,IsProtected \
             | sddsprocess -pipe=in bpmSetpoints \
             -print=col,ControlType,pv \
             -print=col,IsNumerical,y,type=character \
             -print=col,OpsIntervention,n,type=character \
             -print=col,IsReadOnly,n,type=character
         } result] {
    puts stderr "Error for $argv0 in [pwd]: $result"
    catch {file delete xSetpoints ySetpoints xOffsets yOffsets \
             xRFSetpoints}
    exit 1
}
# Add Xray bpm blade offsets. 
set tmpXoffset /tmp/[APSTmpString]
set fileList bpmSetpoints
# These are sectors where blade PVs exist and PVs connected to a real signal.
set DSPXraySectors [exec sdds2stream -col=Sector \
                      /home/helios/oagData/sr/DSPXrayBPMs/sectors.sdds]

foreach sector $DSPXraySectors {
    if [catch {exec sddsprocess /home/helios/oagData/SCR/requestFiles/IDXrayOffset.req.template \
                 $tmpXoffset.req.$sector \
                 -reedit=column,ControlName,%/<sector>/$sector/} result] {
        puts stderr "Error for $argv0 in [pwd]: $result"
        catch {eval file delete $fileList}
        exit 1
    }
    lappend fileList $tmpXoffset.req.$sector
}

set tmpOut /tmp/[APSTmpString]
if [string compare $mode SR]==0 {
    set extraCmd [list sddsprocess -pipe -reprint=column,Category,BPM -reprint=column,Beamline,SR]
} else {
    set extraCmd cat
}
if [catch {eval exec sddscombine $fileList -merge -pipe=out \
             | $extraCmd > $output.1
    eval file delete $fileList} result] {
    puts stderr "Error for $argv0 in [pwd]: $result"
    catch {eval file delete $fileList}
    exit 1
}

# remove PVs that don't connect
if [catch {exec sddsprocess $output.1 -pipe=out \
             -edit=col,rec_name,ControlName,S?/./D \
             | sddssort -column=rec_name -pipe \
             | sddsxref -reuse -nowarnings -pipe $recordFile -match=rec_name \
             | sddsconvert -pipe -delete=col,rec_name,ioc_name \
             | sddssort -pipe=in -numericHigh -col=ControlName -unique $output \
         } result] {
    puts stderr "error: $result"
    exit 1
}
file delete $output.1
if [string length $lastFile] {
    puts "New  file: [exec sdds2stream -rows $output]"
    puts "Last file: [exec sdds2stream -rows $lastFile]"
    exec sddsselect $output $lastFile -invert -match=ControlName $rootname.added -reuse
    exec sddsselect $lastFile $output -invert -match=ControlName $rootname.removed -reuse
    puts "PVs added ($rootname.added): [exec sdds2stream -rows $rootname.added]"
    puts "PVs removed ($rootname.removed): [exec sdds2stream -rows $rootname.removed]"
}
if $install {
    if [file exists ${rootname}.req] {
        if [catch {file delete ${rootname}.req} result] {
            puts stderr "Error for $argv0 in [pwd]: $result"
            exit 1  
        }
    }
    catch {exec chmod -w $rootname.req}
    if [catch {exec ln -s $output ${rootname}.req} result] {
        puts stderr "Error for $argv0 in [pwd]: $result"
        exit 1
    }
    if [catch {APSMakeSCRCategoryFiles -input ${rootname}.req} result] {
        puts stderr "Error for $argv0 in $pwd: $result"
        exit 1
    }
}

