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

#
#$Log: not supported by cvs2svn $
#Revision 1.11  2012/02/28 20:19:37  shang
#commented out "ping" command since it does not work on linux, had IT installed "ftp" so that hpVecRestore can run on linux.
#
#Revision 1.10  2008/12/12 22:36:06  emery
#Removed command MMEMORY:DEL 'RAM:HPREST.TMP' because
#it wasn't necessary, and sometime produces a warning message
#on the hpvecsr.
#
#Revision 1.9  2006/01/30 04:07:23  emery
#Changed the temp file path to the user's home directory.
#
#Revision 1.8  2006/01/30 03:56:12  emery
#Put more detail in error messages.
#
#Revision 1.7  2004/04/29 22:08:30  emery
#Changed int device to ram device
#because accessing int device is a floopy disk device
#and is slow at times.
#
#Revision 1.6  2004/04/20 23:32:54  emery
#Fixed type with filename variable missing a $ in file command.
#
#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:40:38  emery
#Changed variable fileName to filename
#
# Revision 1.3  1996/05/10  19:46:43  borland
# Restored use of usage variable in printing error messages.
#
# Revision 1.2  1996/04/29  19:05:03  emery
# Added "tmp" to the option -unit list.
#
# Revision 1.1  1996/03/03  20:09:57  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 "hpVecRestore -unit {boo | sr} -filename <filename> \[-verbose 1\]"
if [llength $argv]<4 {
    puts stderr "hpVecRestore -unit {boo | sr | tmp} -filename <filename> \[-verbose 1\]"
    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 for $argv0 in [pwd]: $result"
#    exit
#}

if ![file exists $filename] {
    puts stderr "Error for $argv0 in [pwd]: $filename not found"
    exit
}
# change int to ram: because accessing int device is too slow sometimes.
# Don't need to remove file RAM:HPREST.TMP
#exec hpSocketSend hpvec${unit}.aps4.anl.gov "MMEMORY:DEL 'RAM:HPREST.TMP'"
after 2000

if $verbose {
    puts -nonewline stderr "Transferring file..."
}

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"
# disk ram no longer present. Have to use nvram.
ftpSend $ftpID "cd nvram"

global env
if $verbose {
    puts stderr "$env(HOME)/HPREST.TMP"
}
catch {exec cp $filename $env(HOME)/HPREST.TMP}
#if [catch {file copy -force $filename $env(HOME)/HPREST.TMP} result] {
#    puts stderr "Error for $argv0 in [pwd]: $result"
#}

ftpSend $ftpID "lcd $env(HOME)"
ftpSend $ftpID "put HPREST.TMP"
ftpSend $ftpID quit
close $ftpID

if $verbose {
    puts stderr "Waiting 20 seconds for upload..."
    }
after 20000

if $verbose {
    puts stderr "Restoring state..."
}

exec hpSocketSend hpvec$unit.aps4.anl.gov "MMEMORY:LOAD:STATE 1,'NVRAM:HPREST.TMP'"

if $verbose {
    puts stderr "Done."
}
exit
