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

# $Log: not supported by cvs2svn $
# Revision 1.5  2005/07/11 17:10:33  emery
# Changed string test and added comment.
#
# Revision 1.4  2003/03/22 03:36:48  emery
# Removed creation of column ReadbackName
#
# Revision 1.3  2003/03/22 02:55:44  emery
# Added removal of column ioc_name.
#
# Revision 1.2  2002/10/02 18:57:35  shang
# replaced burtrb by obtaining pvs from iocRecName.sdds
#
# Revision 1.1  2000/10/28 09:11:47  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 IDBM
set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds

set usage {usage: makeIDBMFile [-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 !$install {
    if [file exists $output] {
        puts stderr "Error for $argv0 in [pwd]: $output exists"
        exit 1
    }
    set lastFile ""
} else {
    set lastFile [lindex [lsort [glob $root.req-????]] end]
    set output [APSNextGenerationedName -name $lastFile -newFile 1]
}

for {set sector 1} {$sector<41} {incr sector} {
# In mode SR, the script is normally run from another script, so we
# don't want printouts.
    if {![string match $mode SR]} {
        puts stderr "Working on sector $sector"
    }
    set sectorf [format %02ld $sector]
    exec sddsprocess ${root}.req.template ${root}.req.$sector \
      -reedit=column,ControlName,%/<sector>/$sector/%/<sectorf>/$sectorf/ \
      -reedit=column,Beamline,%/<sector>/S$sector/%/<sectorf>/$sectorf/
    lappend fileList ${root}.req.$sector
    scan $sector %ld sector
}


if [catch {eval exec sddscombine $fileList IDBM.other.req -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 \
             | sddsconvert -pipe \
             -delete=col,rec_name,ioc_name \
             | sddssort -pipe=in $output -col=Beamline -column=ControlName -unique \
         } 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]} {
    puts stderr "New  file: [exec sdds2stream -rows $output]"
    if [string length $lastFile] {
        puts stderr "Last file: [exec sdds2stream -rows $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
