#!/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 args $argv

set command0 ":WAVEFORM:DATA?"
set command1 "*LRN?"
set command2 "*IDN?"
set instName ""
set filename ""
set ring PAR
APSParseArguments { filename ring instName }

set errorMessage "Usage: $argv0 -filename <configFile> \[-ring <SR|PAR> || -instName <ID address>]" 

if {[string length $filename] == 0 } {
    puts stderr $errorMessage
    exit 1
}

if ![string length $instName] {
    switch $ring {
        SR {
            set instName "164.54.2.169"
            # set instName "164.54.0.253"
	    set instName "10.6.56.45"
        }
        PAR {
            set instName "164.54.2.252" 
	    #par scope ID address changed to 10.6.56.31 10/5/2023 HS
	    set instName "10.6.56.31"
        }
        default {
            puts stderr "saveHP9000State: invalid ring ($ring) provided."
            exit 1
        }
    }
}

puts [exec date]
set instrumentName $instName
set PeerPort 5025

if {[catch {socket $instrumentName $PeerPort} sid]} {
    puts "Error: Socket Could not be created: $sid"
    exit
}

fconfigure $sid -buffering line
set command $command1
puts "restore file:  scopeState"
set fh [open $filename r]
while  { [gets $fh string]>0 } {
      catch {puts $sid $string}
}
close $fh
close $sid
#temporarily use this pv ID01:GenericString8  to remember the setup file
#if [catch {exec cavput -list=ID01:GenericString8=[file tail $filename] -pend=10} result] {
#    puts "Error recording setup file name: $result"
#}
exit
