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

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 usage "usage: snap2standardize -snapshot <filename> -output <filename> -template <filename>"
set snapshot ""
set output ""
set template ""
set args $argv
if {[APSStrictParseArguments {snapshot output template}]} {
    puts stderr $usage
    exit 1
}

if {![string length $snapshot] || ![string length $output] || ![string length $template]} {
    puts stderr $usage
    exit 1
}

if {![file exists $snapshot] || ![file exists $template]} {
    puts stderr "Either $snapshot or $template does not exist"
    exit 1
}

catch {exec sdds2stream -rows $template} origRows

exec sddsprocess $snapshot -pipe=out -match=column,Category=MainPS -match=column,ControlName=*:PS:SetCurrentC \
     -reedit=column,ControlName,%/:PS:SetCurrentC// -scan=column,Finish,ValueString,%lf -reprint=param,SnapshotSource,$snapshot \
     | sddsconvert -pipe -retain=column,ControlName,Finish,ValueString \
     | sddsxref -pipe=in -nowarning -match=ControlName $template $output  -take=*

catch {exec sdds2stream -rows $output} newRows
if [string compare $newRows $origRows] {
    puts stderr "Template: $origRows"
    puts stderr "New file: $newRows"
}
