#! /bin/sh
# This command runs command(s) remotely in the background, by pointing
# stdout and stderr at /dev/null. (Should we point stdin at /dev/null too?)
# By running this through the Bourne shell at the other end, and
# we get rid of the rsh and rshd which otherwise hang around at either
# end. Ideas from from a posting by clyde@emx.utexas.edu to list.xpert,
# and Stephen Bellantoni <sjb@cs.toronto.edu>
# Mark Moraes, University of Toronto
rshargs="/bin/sh"
case $# in
0)
	echo Usage: $0 remotehost [-l remoteuser] [command] >&2
	exit 1
	;;
*)	
	host=$1
	shift
	case "$1" in
	-l)
		shift
		rshargs="-l $1 /bin/sh"
		shift
		;;
	esac
	;;
esac
localhost=y
case "$DISPLAY" in
'')
	# Assume unix:0.0
	DISPLAY=${HOST-`hostname`}:0.0
	;;
:*|unix:*)
	DISPLAY=${HOST-`hostname`}`expr "$DISPLAY" : "[^:]*\(.*\)"`
	;;
*)
	localhost=n
	;;
esac
# If we are on the local host, do an xhost -- the xhost won't work
# if DISPLAY isn't unix:0.0, even if we are on the local host.
case $localhost in
y)	xhost +$host ;;
esac
(
 # Set remote terminal type, remote display, and close stdin, stdout and
 # stderr at the remote end so the rsh doesn't stay around. This also
 # means that no error feedback is possible, which is a real pain.
 # common problems -- DISPLAY being wrong or the remote host not
 # being authorized to connect to the X server.
 echo "TERM=$TERM;export TERM;DISPLAY=$DISPLAY;export DISPLAY;" \
      ${CONSOLETYPE:+"CONSOLETYPE=$CONSOLETYPE; export CONSOLETYPE;"} \
      "exec > /dev/null 2>&1 <&1; (";
 case "$@" in
 '')	while read line; do echo "$line"; done;;
 *)	echo "$@";;
 esac
 echo ") &"
) | rsh $host $rshargs
