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

# $Log: not supported by cvs2svn $
# Revision 1.7  2006/05/17 20:18:11  shang
# updated srBPLD.mon.template so that each sector only contains S<sector>B:P1:[xy]liveAI and
# S<sector1>A:P1:[xy]liveAI, where sector1 = sector + 1; modified script to use the updated
# template.
#
# Revision 1.6  2006/03/02 20:56:03  emery
# Use the BPLD sector list instead of all sectors.
# Also moved the test of the extra PVs file till after the
# "cd /home/helios/oagData/logging/srBPLD/" command.
#
# Revision 1.5  2005/11/09 00:17:24  shang
# now sorted by sector and ControlName.
#
# Revision 1.4  2004/06/09 16:41:27  emery
# Changed the list in a foreach loop for more clarity.
#
# Revision 1.3  2003/03/22 03:46:43  emery
# Added removal of ioc_name column.
#
# Revision 1.2  2003/03/22 02:23:44  emery
# Minor formatting change.
#
# Revision 1.1  2003/03/03 20:05:27  soliday
# Creates logger input file for srBPLD.
#
#
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 srBPLD 
set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds
set positionFile /home/helios/oagData/sr/lattices/default/aps.twi
set usage {usage: makeBPLDmonFile [-output <filename> | -install 1]}
set output ""
set install 0
set args $argv
if {[APSStrictParseArguments {output install}] || \
        (![string length $output] && !$install) || \
        ([string length $output] && $install)} {
    puts stderr "Error for $argv0 in [pwd]: $usage"
    exit 1
}

set previousDir [pwd]
cd /home/helios/oagData/logging/srBPLD/

if [catch {file readlink  $root.mon} result] {
    set lastFile ""
} else {
    set lastFile $result
}

if $install {
    #the default is install  (output argument is ignored)
    if ![file exist $lastFile] {
        set lastFile ${root}.mon-0000
    }
    set output [APSNextGenerationedName -name $lastFile -newFile 1]
} else {
    #provide output, without install
    if ![string length $output] {
        puts stderr "output file has to be provided with -install 0"
        exit 1
    }
    if [file exists $output] {
        puts stderr "Error for $argv0 in [pwd]: $output exists"
        exit 1
    }
}

if {[file exists srBPLD.mon.extra]} {
    set fileListKeep srBPLD.mon.extra
} else {
    set fileListKeep ""
}

set sectorList [exec sdds2stream -col=Sector \
                  /home/helios/oagData/sr/BPLDs/sectors.sdds]

set fileList ""
set tmpRoot /tmp/[APSTmpString]
foreach sector $sectorList {
    #puts "working on sector $sector"
    set sector1 [expr $sector + 1]
    if [catch {exec sddsprocess ${root}.mon.template $tmpRoot.${root}.mon.${sector}\
                   -reedit=column,ControlName,%/<sector>/${sector}/%/<sector1>/${sector1}/ \
                   -reedit=column,ReadbackName,%/<sector>/${sector}/%/<sector1>/${sector1}/ }  result] {
        puts stderr "Error: $result"
        cd $previousDir
        exit
    } 
    lappend fileList $tmpRoot.${root}.mon.${sector}
}

if [catch {eval exec sddscombine $fileListKeep $fileList -pipe=out -merge | \
               sddsprocess -pipe -edit=col,rec_name,ControlName,S?/./D \
               {"-description=text=Created by makeBPLDmonFile"} \
               | sddssort -column=rec_name -pipe \
               | sddsxref -reuse -nowarnings -pipe $recordFile -match=rec_name \
               | sddssort -pipe -col=ControlName -numericHigh -unique \
               | sddsconvert -pipe -delete=col,rec_name,ioc_name \
               | sddsprocess -pipe=in $output -print=column,Provider,ca -print=column,ExpectNumeric,y,type=character -print=column,ExpectFieldType,scalar -define=column,ExpectElements,1,type=long} result] {
    puts stderr "Error: $result"
    cd $previousDir
    exit
}

eval file delete $fileList

if {[file exists $lastFile]} {
    if [catch {CompareRequestOrMonitorFiles -lastFile  $lastFile \
                   -newFile $output -install $install -type mon -root $root} result] {
        puts stderr $result
    } else {
        puts "New file - $output is the same as last file - $lastFile."
    }
}

if $install {
    if {[file exists ${root}.mon]} {
        file delete ${root}.mon
    }
    exec ln -s $output ${root}.mon
    
}
cd $previousDir
exit
