#!/usr/local/bin/expect -f
# xrshells 1.0.0: a controller of multiple remote shells
# Copyright (C) 1998 J.P.M. de Vreught
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Contact information:
# Hans de Vreught <J.P.M.deVreught@cs.tudelft.nl>
# De Brink 70
# 2553 HA  The Hague
# The Netherlands

set timeout -1

if {[llength $argv] == 0} {
	send_error "Usage: xrshells machine..."
	exit 1
}

if {[info exists env(XRSHELLS)]} {
	send_error "You are already running xrshells!\n"
	exit 2
}

set env(XRSHELLS) 1

if {[file exists "$exp_exec_library/cat_buffers"]} {
	set catflags "-u"
} else {
	set catflags ""
}

set fifo_prefix "/tmp/fifo"
set log_prefix "/tmp/log"

for {set i 0} {$i < [llength $argv]} {incr i} {
	set infifo($i) "$fifo_prefix\_[pid].in.[lindex $argv $i]"
	exec mkfifo $infifo($i)
	exec chmod go-rwx $infifo($i)
	set outfifo($i) "$fifo_prefix\_[pid].out.[lindex $argv $i]"
	exec mkfifo $outfifo($i)
	exec chmod go-rwx $outfifo($i)
	set logfile($i) "$log_prefix\_[pid].[lindex $argv $i]"
}

if {[fork] != 0} {
	exit -onexit {
		for {set i 0} {$i < [llength $argv]} {incr i} {
			exec rm -f $infifo($i) $outfifo($i) $logfile($i)
			catch "exec kill -9 $xterm($i)"
		}
	}

	set outs {}

	for {set i 0} {$i < [llength $argv]} {incr i} {
		spawn -noecho -open [open $infifo($i) "w"]
		set out($i) $spawn_id
		lappend outs $out($i)
		spawn -noecho -open [open "| cat $catflags < $outfifo($i)" "r"]
		set in($i) $spawn_id
		spawn -noecho -open [open $logfile($i) "w"]
		exec chmod go-rwx $logfile($i)
		set log($i) $spawn_id
		set xterm($i) [exec xterm -e tail -f $logfile($i) &]
	}

	set command "interact -input $user_spawn_id -output outs "
	append command "-input $in(0) -output $user_spawn_id -output $log(0)"

	for {set i 1} {$i < [llength $argv]} {incr i} {
		append command " -input $in($i) -output $log($i)"
	}

	eval $command

	for {set i 0} {$i < [llength $argv]} {incr i} {
		catch "close $in($i)"
		catch "wait $in($i)"
		catch "close $out($i)"
		catch "wait $out($i)"
	}

	send_user "Terminated!\n"

	exit
}

for {set i 0} {$i < [llength $argv]} {incr i} {
	if {[fork] != 0} {
		continue
	}

	spawn -noecho rsh [lindex $argv $i]
	set shell $spawn_id

	spawn -noecho -open [open "| cat $catflags < $infifo($i)" "r"]
	set in $spawn_id

	spawn -noecho -open [open $outfifo($i) "w"]
	set out $spawn_id

	interact {
		-u $shell
		-input $in -output $out
	}

	exit
}
