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

# $Log: not supported by cvs2svn $
# Revision 1.1  2004/05/22 19:04:55  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 XrayTranslation
set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds

set usage {usage: makeSRXrayBPMreqFile [-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 -nocomplain $root.req-????]] end]
    if ![string length $lastFile] {
        set lastFile $root.req-0000
    }
    set output [APSNextGenerationedName -name $lastFile -newFile 1]
}

set tmpRoot /tmp/APSTmpString

foreach trans {1 2 3 4 5} {
    if [catch {exec sddsprocess /home/helios/oagData/SCR/requestFiles/XrayTranslation.req.1 \
                   $tmpRoot.$trans -reedit=col,Category,%/1/$trans// \
                   -reedit=col,ControlName,%/:1:/:${trans}:/ } result] {
        puts stderr $result
        exit 1
    }
    lappend fileList $tmpRoot.$trans
}

if [catch {eval exec sddscombine $fileList -merge -pipe=out  \
               | sddsprocess -pipe \
               -edit=col,rec_name,ControlName,S?/./D \
               | sddssort  -pipe \
               -column=rec_name -pipe \
               | sddsxref -pipe $recordFile \
               -reuse -nowarnings  -match=rec_name  \
               | sddsconvert -pipe -delete=col,rec_name,ioc_name \
               | sddssort -pipe=in $output \
               -column=ControlName -unique } result] {
    puts stderr "error: $result"
    exit
}
    
eval file delete $fileList


if [string compare $mode SR]!=0 {
    puts stderr "New  file: [exec sdds2stream -rows $output]"
    if [string length $lastFile] {
        puts stderr "Last file: [exec sdds2stream -rows $lastFile]"
        if [catch {exec sddsselect $output $lastFile -match=ControlName -invert $tmpRoot.add} result] {
            puts stderr $result
            exit 1
        }
        puts stderr "[exec sdds2stream -rows=bar $tmpRoot.add] PVs added."
        if [catch {exec sddsselect $lastFile $output -match=ControlName -invert $tmpRoot.removed} result] {
            puts stderr $result
            exit 1
        }
        puts stderr "[exec sdds2stream -rows=bar $tmpRoot.removed] PVs removed."
        exec rm $tmpRoot.add
        exec rm $tmpRoot.removed
    }
}
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
