# An expect script to remove a directory and share which was
# previously setup for an smbtorture test.
# Copyright Brad Henry <brad@samba.org> 2006
# Released under the GNU GPL version 3 or later.

proc remove_test { remote_prompt sharepath sharename hosts_file_path \
			backup_hosts_filename } {

	set err_str [delete_share $remote_prompt $sharename]
	if { $err_str != "OK" } {
		puts stderr "Error in function delete_share: $err_str."
		puts stderr "Function remove_test will continue."
	}

	set err_str [delete_directory $remote_prompt $sharepath]
	if { $err_str != "OK" } {
		puts stderr "Error in function delete_directory: $err_str."
		puts stderr "Function remove_test will continue."
	}

	# Overwrite the current hosts file with the backup we made during setup.
	set err_str [delete_hosts_entry $remote_prompt $hosts_file_path \
			$backup_hosts_filename]
	if { $err_str != "OK" } {
		puts stderr "Error in function delete_hosts_entry: $err_str."
		puts stderr "Function remove_test will continue."
	}
	return $err_str
}

# read parameters
set remote_host $env(SMBTORTURE_REMOTE_HOST)
set remote_prompt $env(SMBTORTURE_REMOTE_PROMPT)

set username $env(SMBTORTURE_USERNAME)
set password $env(SMBTORTURE_PASSWORD)

set timeout $env(SMBTORTURE_EXPECT_TIMEOUT)

set sharepath $env(SMBTORTURE_REMOTE_SHARE_PATH)
set sharename $env(SMBTORTURE_REMOTE_SHARE_NAME)

set backup_hosts_filename $env(REMOTE_BACKUP_HOSTS_FILENAME)
set hosts_file_path $env(REMOTE_HOSTS_FILE_PATH)

set err_val [spawn $env(SHELL)]
if {$err_val == 0} {
	puts stderr "Expect failed while spawning a shell process."
	exit $err_val
}

set err_str [telnet_login $remote_prompt $remote_host $username $password]
if {$err_str != "OK"} {
	puts stderr "\nFunction telnet_login failed during cleanup."
	puts stderr "Error was: $err_str."
	exit 1
}

set err_str [remove_test $remote_prompt $sharepath $sharename \
		$hosts_file_path $backup_hosts_filename]
if {$err_str != "OK"} {
	puts stderr "\nFunction remove_test failed."
	puts stderr "Error was: $err_str."
	# Log off from the telnet server.
	send "exit\r\n"
	exit 1
}

# Log off from the telnet server.
send "exit\r\n"
exit 0