#!/bin/sh
#$Id: rcmd,v 1.1.1.1 1995/02/27 10:13:46 tomas Exp $
#
# Modified by Tomas Stephanson Ellemtel to run with OpenWindows.
# 
# It also sets up a logfile on the remote machine
# /tmp/rcmd.log.$USER so it is easier to debug when
# programs dont start.
#
# Original Notice
# 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


logfile=/tmp/rcmd.log.$USER

# 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 > /dev/null ;;
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 -n "TERM=$TERM;export TERM;" \
      "DISPLAY=$DISPLAY;export DISPLAY;" \
      "MANPATH=$MANPATH;export MANPATH;" \
      "GUIDEHOME=$GUIDEHOME;export GUIDEHOME;" \
      "PRINTER=$PRINTER;export PRINTER;" \
      "EDITOR=$EDITOR;export EDITOR;" \
      "LD_LIBRARY_PATH=$LD_LIBRARY_PATH;export LD_LIBRARY_PATH;"\
      "OPENWINHOME=$OPENWINHOME;export OPENWINHOME;" \
      "PATH=$PATH;export PATH;" \
      "HELPPATH=$HELPPATH;export HELPPATH;" \
      ${CONSOLETYPE:+"CONSOLETYPE=$CONSOLETYPE; export CONSOLETYPE;"} \
      "exec  > $logfile 2>&1 <&1; ( "
 case "$@" in
 '')	while read line; do echo "$line"; done;;
 *)	echo -n "$@";;
 esac
 echo ") &"
) |  rsh $host $rshargs 

