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

# $Log: not supported by cvs2svn $
# Revision 1.6  2005/11/09 00:17:25  shang
# now sorted by sector and ControlName.
#
# Revision 1.5  2004/06/09 16:31:23  emery
# Set Beamline column to S$sector when in normal mode.
#
# Revision 1.4  2003/03/22 03:24:26  emery
# Added removal of ioc_name column, and removed definition
# of column ReadbackName.
#
# Revision 1.3  2002/10/02 18:53:42  shang
# replaced obtaining pvs using burtrb by from iocRecNames.sdds
#
# Revision 1.2  2001/06/27 21:32:20  emery
# Corrected the usage message.
#
# Revision 1.1  2000/10/27 02:09:20  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 root BPLDs
set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds
set positionFile /home/helios/oagData/sr/lattices/default/aps.twi
set usage {usage: makeBPLDreqFile [-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
}

cd /home/helios/oagData/SCR/requestFiles
if [catch {file readlink  $root.req} result] {
    set lastFile ""
} else {
    set lastFile $result
}

if !$install {
    if ![string length $output] {
        puts stderr "Error for $argv0 in [pwd]: output should be provided with -install 0."
        exit 1
    }
    if [file exists $output] {
        puts stderr "Error for $argv0 in [pwd]: $output exists"
        exit 1
    }
} else {
    if ![file exist $lastFile] {
        set lastFile $root.req-0000
    }
    set output [APSNextGenerationedName -name $lastFile -newFile 1]
}


set sectorList [exec sdds2stream -col=Sector \
                  /home/helios/oagData/sr/BPLDs/sectors.sdds]
set tmpRoot /tmp/[APSTmpString]
foreach sector $sectorList {
    set sector1 [expr $sector + 1]
    switch $mode {
        SR {
            if [catch {exec sddsprocess ${root}.req.template $tmpRoot.${root}.req.$sector \
                           -reedit=column,ControlName,%/<sector>/$sector/%/<sector1>/$sector1/ \
                           -reprint=column,Beamline,SR } result] {
                puts stderr "Error: $result"
                exit
            }
            set beamline SR
        }
        normal -
        default {
            puts "Working on sector $sector"
            if [catch {exec sddsprocess ${root}.req.template $tmpRoot.${root}.req.$sector \
                           -reedit=column,ControlName,%/<sector>/$sector/%/<sector1>/$sector1/ \
                           -reedit=column,Beamline,%/<sector>/ID$sector/ } result] {
                puts stderr "Error: $result"
                exit
            }
            set beamline S$sector
        }
    }
    lappend fileList $tmpRoot.${root}.req.$sector
}
 
if [catch {eval exec sddscombine $fileList -merge -pipe=out \
               | sddsprocess -pipe \
               -edit=col,rec_name,ControlName,S?/./D \
               | sddssort -column=rec_name -pipe \
               | sddsxref -reuse -nowarnings -pipe $recordFile -match=rec_name \
               | sddssort -pipe -col=ControlName -numericHigh -unique \
               | sddsconvert -pipe=in $output -delete=col,rec_name,ioc_name} result] {
    puts stderr "Error: $result"
    exit
}

eval file delete $fileList
# In mode SR, the script is normally run from another script, so we
# don't want printouts.
if {![string match $mode SR]} {
    if [file exist $lastFile] {
        if [catch {CompareRequestOrMonitorFiles -lastFile  $lastFile \
                       -newFile $output -root $root -install $install -type req} result] {
            puts stderr $result
        } else {
            puts "New file - $output is the same as last file - $lastFile."
        }
    }
}

if $install {
    if [file exists ${root}.req] {
        if [catch {file delete ${root}.req} result] {
            puts stderr "Error for $argv0 in [pwd]: $result"
            exit 1  
        }
    }
    catch {exec chmod -w $root.req}
    if [catch {exec ln -s $output ${root}.req} result] {
        puts stderr "Error for $argv0 in [pwd]: $result"
        exit 1
    }
    if [catch {APSMakeSCRCategoryFiles -input ${root}.req} result] {
        puts stderr "Error for $argv0 in $pwd: $result"
        exit 1
    }
}
exit
