# An expect script to create a temporary file, map a share, copy the file to the share,
# and compare the contents of the two files.
# Copyright Brad Henry <brad@samba.org> 2006
# Released under the GNU GPL v2 or later.

proc run_test { remote_prompt tmp_filename share_drive host_drive buildhost_ip buildhost_share username domain password } {

	# Create the temp file on the windows host and connect to the samba share.
	set host_tmpfile "$host_drive\\$tmp_filename"
	set err_str [create_tmp_file $remote_prompt $host_tmpfile]
	if { $err_str != "OK" } {
		return $err_str
	}

	set buildhost_sharepoint "\\\\$buildhost_ip\\$buildhost_share"
	set err_str [map_share $remote_prompt $share_drive $buildhost_sharepoint $username $domain $password]
	if { $err_str != "OK" } {
		return $err_str
	}

	# Copy the temp file to the share and compare its contents with the original.
	set share_tmpfile "$share_drive\\$tmp_filename"
	set xcopy_options ""
	set err_str [xcopy_file $remote_prompt $host_tmpfile $share_tmpfile $xcopy_options]
	if { $err_str != "OK" } {
		return $err_str
	}

	set err_str [compare_files $remote_prompt $host_tmpfile $share_tmpfile]
	if { $err_str != "OK" } {
		return $err_str
	}

	# Remove files and unmap share.
	set err_str [delete_file $remote_prompt $share_tmpfile]
	if { $err_str != "OK" } {
		return $err_str
	}
	set err_str [delete_file $remote_prompt $host_tmpfile]
	if { $err_str != "OK" } {
		return $err_str
	}

	set err_str [unmap_share $remote_prompt $share_drive]
	if {$err_str != "OK" } {
		return $err_str
	}

	return $err_str
}

# Read parameters.
set remote_prompt $env(SMBTORTURE_REMOTE_PROMPT)
set remote_host $env(SMBTORTURE_REMOTE_HOST)
set username $env(SMBTORTURE_USERNAME)
set password $env(SMBTORTURE_PASSWORD)
set timeout $env(SMBTORTURE_EXPECT_TIMEOUT)

set tmp_filename $env(SMBTORTURE_TMP_FILENAME)

set share_drive $env(SMBTORTURE_REMOTE_DRIVE_LETTER)
set host_drive "%HOMEDRIVE%"

set buildhost_ip $env(SMBTORTURE_LOCAL_IP)
set buildhost_share $env(SMBTORTURE_LOCAL_SHARE_NAME)
set buildhost_username $env(SMBTORTURE_LOCAL_USERNAME)
set buildhost_domain $env(SMBTORTURE_LOCAL_DOMAIN)
set buildhost_password $env(SMBTORTURE_LOCAL_PASSWORD)

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 Samba server testing."
	puts stderr "Error was: $err_str."
	exit 1
}

set err_str [run_test $remote_prompt $tmp_filename $share_drive $host_drive $buildhost_ip $buildhost_share $buildhost_username $buildhost_domain $buildhost_password]
if {$err_str != "OK"} {
	puts stderr "\nFunction run_test failed during Samba server testing."
	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