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

# $Log: not supported by cvs2svn $
# Revision 1.4  2010/01/08 22:22:48  shang
# modified to removed the lab FPGA bpms from the configuration file.
#
# Revision 1.3  2009/05/26 15:19:57  shang
# added displaying of number of deleted/added PVs.
#
# Revision 1.2  2009/03/18 19:55:03  shang
# modified to be able to generate the request file for the first time.
#
# Revision 1.1  2009/03/18 19:41:21  shang
# first version
#
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 BBPMWaveform
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
if !$install {
    if [file exists $output] {
        puts stderr "Error (1) for $argv0 in [pwd]: $output exists"
        exit 1
    }
} else {
    set lastFile [lindex [lsort [glob -nocomplain $rootname.req-????]] end]
    if ![llength $lastFile] {
        set lastFile $rootname.req-0000
    }
    set output [APSNextGenerationedName -name $lastFile -newFile 1]
}

#set iocList [exec sdds2stream -par=IOCName SBPMWaveform.config]
set tmpRoot /tmp/[APSTmpString]
if [catch {exec sddsprocess SBPMWaveform.config $tmpRoot.config -match=par,Location=Accel -match=par,BPMType=Booster } result] {
    puts stderr $result
    exit 1
}
set iocList [exec sdds2stream -par=IOCName $tmpRoot.config]
#temporarily do it, only have two sectors now
#set iocList {B1C8 B2C0 B2C2 B2C4 B2C6 B2C8}
puts $iocList
set fileList ""
set page 1
#puts $iocList
foreach ioc $iocList {
    set sector $ioc
    if [catch {exec replaceText BBPMWaveformIOC.req.template \
                 -orig=<ioc>,<sector> -repl=$ioc,$sector $tmpRoot.$ioc } result] {
        puts stderr "Error(2) - $ioc: $result"
        exit 1
    }
    lappend fileList $tmpRoot.$ioc
    if [catch {exec replaceText BBPMWaveformTime.req.template \
                 -orig=<bpm>,<sector> -repl=${ioc}P1,$sector $tmpRoot.$ioc.time } result] {
        puts stderr "Error(2) - $ioc: $result"
        exit 1
    }
    lappend fileList $tmpRoot.$ioc.time
    if [catch {exec sddsprocess SBPMWaveform.config -pipe=out -match=par,IOCName=$ioc \
                 | sdds2stream -pipe -col=BPM } bpmList] {
        puts stderr $result
        exit 1
    }
    set ${ioc}BpmList $bpmList
    foreach bpm [set ${ioc}BpmList] {
        if [catch {exec replaceText BBPMWaveform.req.template $tmpRoot.$ioc.$bpm \
                     -orig=<bpm>,<sector> -repl=$bpm,$sector } result] {
            puts stderr "Error(3) - $ioc: $result"
            exit 1
        }
        puts $tmpRoot.$ioc.$bpm
        lappend fileList $tmpRoot.$ioc.$bpm
    }
}
if {0} {
if [catch {eval exec sddscombine $fileList -pipe=out -merge \
             | sddsprocess -pipe -edit=col,rec_name,ControlName,B?/./D \
             | sddssort -pipe -col=ControlName -numericHigh -unique \
             | sddsxref -pipe $recordFile -match=rec_name -reuse -nowarning \
             | sddsconvert -pipe=in $output -del=col,rec_name  } result] {
    puts stderr "Error(4): $result"
    exit 1
}
}
if [catch {eval exec sddscombine $fileList -merge -pipe=out \
             | sddssort -pipe=in -col=ControlName -numericHigh -unique $output } result] {
    puts stderr "Error(4): $result"
    exit 1
}
if $install {
    if [file exists ${rootname}.req] {
	if [catch {exec sddsselect $rootname.req $output -match=ControlName $rootname.removed  -invert
	    exec sddsselect $output $rootname.req -match=ControlName $rootname.added -invert } result] {
	    puts stderr "Error(7a): $result"
	    exit 1
	}
	puts "PVs removed ($rootname.removed): [exec sdds2stream -rows=bar $rootname.removed]"
	puts "PVs added ($rootname.added): [exec sdds2stream -rows=bar $rootname.added]"
        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
    }
    
}

exit
