# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
# 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
#
# This file is part of DejaGnu.
#
# DejaGnu is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DejaGnu is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DejaGnu; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

# Connect to HOSTNAME using rlogin(1).
#
proc rsh_open { hostname } {
    global spawn_id

    set tries 0
    set result -1

    if {![board_info $hostname exists rsh_prog]} {
	if { [which remsh] != 0 } {
	    set RSH remsh
	} else {
	    set RSH rsh
	}
    } else {
	set RSH [board_info $hostname rsh_prog]
    }

    if {[board_info $hostname exists username]} {
	set rsh_useropts "-l [board_info $hostname username]"
    } else {
	set rsh_useropts ""
    }

    # Get the hostname and port number from the config array.
    if {[board_info $hostname exists name]} {
	set hostname [board_info $hostname name]
    }
    set hostname [lindex [split [board_info ${hostname} netport] ":"] 0]
    if {[board_info ${hostname} exists shell_prompt]} {
	set shell_prompt [board_info ${hostname} shell_prompt]
    } else {
	set shell_prompt ".*> "
    }

    if {[board_info $hostname exists fileid]} {
	unset board_info($hostname,fileid)
    }

    spawn $RSH $rsh_useropts $hostname
    if { $spawn_id < 0 } {
	perror "invalid spawn id from $RSH"
	return -1
    }

    send "\r\n"
    while { $tries <= 3 } {
	expect {
	    -re ".*$shell_prompt.*$" {
		verbose "Got prompt\n"
		set result 0
		break
	    }
	    -re "TERM = .*$" {
		warning "Setting terminal type to vt100"
		set result 0
		send "vt100\n"
		break
	    }
	    "unknown host" {
		exp_send "\003"
		perror "telnet: unknown host"
		break
	    }
	    "has logged on from" {
		exp_continue
	    }
	    -re "isn't registered for Kerberos.*service.*$" {
		warning "$RSH: isn't registered for Kerberos, please kinit"
		catch close
		catch wait
		break
	    }
	    -re "Kerberos rcmd failed.*$" {
		warning "$RSH: Kerberos rcmd failed, please kinit"
		catch close
		catch wait
		break
	    }
	    -re "You have no Kerberos tickets.*$" {
		warning "$RSH: No kerberos Tickets, please kinit"
		catch close
		catch wait
		break
	    }
	    "Terminal type is" {
		verbose "$RSH: connected, got terminal prompt" 2
		set result 0
		break
	    }
	    -re "trying normal rlogin.*$" {
		warning "$RSH: trying normal rlogin."
		catch close
		catch wait
		break
	    }
	    -re "unencrypted connection.*$" {
		warning "$RSH: unencrypted connection, please kinit"
		catch close
		catch wait
		break
	    }
	    -re "Sorry, shell is locked.*Connection closed.*$" {
		warning "$RSH: already connected."
	    }
	    timeout {
		warning "$RSH: timed out trying to connect."
	    }
	    eof {
		perror "$RSH: got EOF while trying to connect."
		break
	    }
	}
	incr tries
    }

    if { $result < 0 } {
	close -i $spawn_id
	set spawn_id -1
    } else {
	set board_info($hostname,fileid) $spawn_id
    }

    return $spawn_id
}

# Download SRCFILE to DESTFILE on DESTHOST.
#
proc rsh_download {desthost srcfile destfile} {
    # must be done before desthost is rewritten
    if {[board_info $desthost exists rcp_prog]} {
	set RCP [board_info $desthost rcp_prog]
    } else {
	set RCP rcp
    }

    if {[board_info $desthost exists rsh_prog]} {
	set RSH [board_info $desthost rsh_prog]
    } else {
	if { [which remsh] != 0 } {
	    set RSH remsh
	} else {
	    set RSH rsh
	}
    }

    if {[board_info $desthost exists username]} {
	set rsh_useropts "-l [board_info $desthost username]"
	set rcp_user "[board_info $desthost username]@"
    } else {
	set rsh_useropts ""
	set rcp_user ""
    }

    if {[board_info $desthost exists name]} {
	set desthost [board_info $desthost name]
    }

    if {[board_info $desthost exists hostname]} {
	set desthost [board_info $desthost hostname]
    }

    set status [catch "exec $RSH $rsh_useropts $desthost rm -f $destfile |& cat" output]
    set status [catch "exec $RCP $srcfile $rcp_user$desthost:$destfile |& cat" output]
    if { $status == 0 } {
	verbose "Copied $srcfile to $desthost:$destfile" 2
	return $destfile
    } else {
	verbose "Download to $desthost failed, $output."
	return ""
    }
}

proc rsh_upload {desthost srcfile destfile} {
    if {[board_info $desthost exists rcp_prog]} {
	set RCP [board_info $desthost rcp_prog]
    } else {
	set RCP rcp
    }

    if {[board_info $desthost exists username]} {
	set rcp_user "[board_info $desthost username]@"
    } else {
	set rcp_user ""
    }

    if {[board_info $desthost exists name]} {
	set desthost [board_info $desthost name]
    }

    if {[board_info $desthost exists hostname]} {
	set desthost [board_info $desthost hostname]
    }

    set status [catch "exec $RCP $rcp_user$desthost:$srcfile $destfile" output]
    if { $status == 0 } {
	verbose "Copied $desthost:$srcfile to $destfile" 2
	return $destfile
    } else {
	verbose "Upload from $desthost failed, $output."
	return ""
    }
}

# Execute CMD on BOARDNAME.
#
proc rsh_exec { boardname program pargs inp outp } {
    global timeout

    verbose "Executing $boardname:$program $pargs < $inp"

    if {![board_info $boardname exists rsh_prog]} {
	if { [which remsh] != 0 } {
	    set RSH remsh
	} else {
	    set RSH rsh
	}
    } else {
	set RSH [board_info $boardname rsh_prog]
    }

    if {[board_info $boardname exists username]} {
	set rsh_useropts "-l [board_info $boardname username]"
    } else {
	set rsh_useropts ""
    }

    if {[board_info $boardname exists name]} {
	set boardname [board_info $boardname name]
    }

    if {[board_info $boardname exists hostname]} {
	set hostname [board_info $boardname hostname]
    } else {
	set hostname $boardname
    }

    # If CMD sends any output to stderr, exec will think it failed.
    # More often than not that will be true, but it doesn't catch the
    # case where there is no output but the exit code is non-zero.
    if { $inp == "" } {
	set inp "/dev/null"
    }

    set ret [local_exec "$RSH $rsh_useropts $hostname sh -c '$program $pargs \\; echo XYZ\\\${?}ZYX'" $inp $outp $timeout]
    set status [lindex $ret 0]
    set output [lindex $ret 1]

    verbose "$RSH status is $status, output is $output"

    # `status' doesn't mean much here other than rsh worked ok.
    # What we want is whether $program ran ok.  Return $status
    # if the program timed out, status will be 1 indicating that
    # rsh ran and failed.  If rsh fails, we will get FAIL rather
    # than UNRESOLVED - this will help the problem be noticed.
    if { $status != 0 } {
	regsub "XYZ(\[0-9\]*)ZYX\n?" $output "" output
	return [list $status "$RSH to $boardname failed for $program, $output"]
    }
    regexp "XYZ(\[0-9\]*)ZYX" $output junk status
    verbose "rsh_exec: status:$status text:$output" 4
    if { $status == "" } {
	return [list -1 "Couldn't parse $RSH output, $output."]
    }
    regsub "XYZ(\[0-9\]*)ZYX\n?" $output "" output
    # Delete one trailing \n because that is what `exec' will do and we want
    # to behave identical to it.
    regsub "\n$" $output "" output
    return [list [expr {$status != 0}] $output]
}
