#!/bin/sh  
# \
exec oagwish "$0" "$@"
wm withdraw .

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: wsStandardize -endpoint <snapshot> -filter <wildcard> -cycles <integer> -period <seconds> -lowerLimit <amps> -upperLimit <amps>"
set endpoint ""
set filter *
set cycles 3
set period 120
set lowerLimit 0
set upperLimit 0
set args $argv
if {[APSStrictParseArguments {endpoint filter cycles period lowerLimit upperLimit}] || \
   ![string length $endpoint] || ![string length $filter] || $cycles<0 || $period<0 || \
   [expr $lowerLimit>=$upperLimit]} {
    puts stderr $usage
    exit 1
}
if ![file exists $endpoint] {
    return -code error "not found: $endpoint"
}

set tmpRoot /tmp/[APSTmpString]
exec sddsprocess $endpoint $tmpRoot.filtered -match=column,ControlName=$filter
exec sddsprocess $tmpRoot.filtered -reprint=column,ValueString,$lowerLimit $tmpRoot.0
exec sddsprocess $tmpRoot.filtered -reprint=column,ValueString,$upperLimit $tmpRoot.1

set optionList -rampTo=$tmpRoot.0,steps=120,pause=[expr $period/2./60]
for {set cycle 0} {$cycle<$cycles} {incr cycle} {
   lappend optionList -rampTo=$tmpRoot.1,steps=120,pause=[expr $period/2./60]
   lappend optionList -rampTo=$tmpRoot.0,steps=120,pause=[expr $period/2./60]
}
lappend optionList -rampTo=$tmpRoot.filtered,steps=120,pause=[expr $period/2./60]

set done 0
APSExecLog .exec -unixCommand "sddscaramp $optionList" \
   -width 120 -callback "set done 1" -cancelCallback "set done 2" -abortCallback "set done 2"
while {!$done} {
   APSWaitWithUpdate -updateInterval 1 -waitSeconds 10 -abortVariable done
}
exit


