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

#
#$Log: not supported by cvs2svn $
#Revision 1.5  2004/04/14 19:55:58  emery
#Changed sleep commands to after. Changed shell command.
#Replace rm and mv with file commands.
#
#Revision 1.4  1997/06/04 19:39:27  emery
#Changed variable fileName to filename
#
# Revision 1.3  1996/05/10  19:46:45  borland
# Restored use of usage variable in printing error messages.
#
# Revision 1.2  1996/04/29  19:05:05  emery
# Added "tmp" to the option -unit list.
#
# Revision 1.1  1996/03/03  20:09:54  borland
# 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)]


set usage "hpVecSave -unit {boo | sr} -filename <filename> \[-verbose 1\]"
if [llength $argv]<4 {
    puts stderr $usage
    exit
}

set args $argv
set unit ""
set filename ""
set verbose 0
if [APSStrictParseArguments {unit filename verbose}] {
    puts stderr $usage
    exit
}

set hpvec hpvec$unit

#if [catch {exec ping $hpvec.aps4.anl.gov 5} result] {
#    puts stderr "Error: $result"
#    exit
#}

if $verbose {
    puts -nonewline stderr "Saving state..."
}
# change int to ram: because accessing int device is too slow sometimes.
exec hpSocketSend hpvec${unit}.aps4.anl.gov "MMEMORY:STORE:STATE 1,'RAM:HPSAVE.TMP'"

if $verbose {
    puts stderr "Transferring (wait 18 seconds for VSA internal operation)..."
}
# maybe this isn't long enough
after 18000

if $verbose {
    puts stderr "Connecting by ftp..."
}
set ftpID [open "|ftp -n hpvec${unit}.aps4.anl.gov" "w"]
after 2000

proc ftpSend {ftpID message} {
    puts $ftpID "$message"
}

ftpSend $ftpID "user anonymous PASS"
ftpSend $ftpID "passive off"
ftpSend $ftpID "cd RAM"
ftpSend $ftpID "get HPSAVE.TMP"
ftpSend $ftpID quit
close $ftpID

set timer 10
while {![file exists HPSAVE.TMP] && $timer > 0} {
if $verbose {
    puts stderr "File not yet in nfs..."
}
    after 1000
    incr timer -1
}
after 5000

file rename -force -- HPSAVE.TMP $filename

if $verbose {
    puts stderr "Done."
}

exit
