#!/etc/fw/cisco/expect -f
#
# Copyright (c) 1993-1996 CheckPoint Software Technologies Ltd.
# All Rights Reserved
#
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF CHECKPOINT SOFTWARE
# TECHNOLOGIES LTD. The copyright notice above does not evidence any actual
# or intended publication of such source code.
#
# $Header: /fw/cvs/fw-1/cisco/fwciscoget.exp,v 1.7.2.1 1996/04/11 09:36:04 yoav Exp $
#

#
# Fetch configuration information from the specified cisco router
#
# args: ciscoget router passwd enablepasswd filename
#
set localhost [exec hostname]

set router [lindex $argv 0]
set password [lindex $argv 1]
set enablepass [lindex $argv 2]
set filename [lindex $argv 3]
set confdir [lindex $argv 4]

proc timedout {args} {
	send_error "Expect timed out"
	exit
}

source fwciscologin

if {[llength $argv] < 5} {
	send_error "Invalid number of arguments\n"
	exit 1
}

spawn telnet $router
login $enablepass
send "write network\r"

expect {*Remote\ host*\?\ } {} \
	timeout		{timedout waiting for Remote host prompt}

send ${localhost}\r

expect {*write*?*} {} \
	timeout		{timedout waiting for filename prompt}

if {[file exists ${confdir}/${filename}]} {
	if {[file exists ${confdir}/${filename}.old]} {
		if {[file isfile ${confdir}/${filename}.old] == 0} {
		      send_error "${confdir}/${filename}.old: Not a plain file\n"
		      exit 1
		}
		system rm -f  ${confdir}/${filename}.old
	}
	system mv -f ${confdir}/${filename} ${confdir}/${filename}.old
}
system touch ${confdir}/${filename}
system chmod 666 ${confdir}/${filename}


send ${filename}\r
expect {*Write\ file*\[confirm\]*} {} \
	timeout		{timed out waiting for confirm}

send "Y"
expect	{*Failed*#}	{set failed 1; send_error "\nTFTP Write failed\n"} \
	{*OK*#}		{set failed 0; send_error "\nTFTP Write succeeded\n"}

send QUIT\r
exit $failed
