#!/bin/csh -f

# An rsh hack to be used only as in
#     rshdisplay hostname [command]
# It behaves the same way as 
#     rsh hostname [command]
# with the following changes.

# If a command is given, it is prepended by a command to set the environment
# variable DISPLAY on the remote machine to the value on the local machine.
# If no command is given, a separate rsh command is issued to place the
# local value of the DISPLAY variable in the remote file .DISPLAY
# In the latter case, the .login of the remote machine is responsible for
# looking in the .DISPLAY file.
# If DISPLAY does not have a value on the local machine, emptystring is used.
#
# If the local DISPLAY value is the local machine, the necessary xhost command
# is issued so that the local machine will allow display from the remote
# machine.
#
# In addition to the DISPLAY hack, the planupdate command is run on the local
# and remote machines in order to keep a record of the latest rsh's in and out.
# Note that planupdate is not run on the remote machine if there is only one
# argument to rshdisplay; in that case the .login of the remote machine can
# run planupdate.

if ($#argv == 0) then
  echo 'usage: rshdisplay hostname [command]'
  exit 1
endif

# planupdate "rsh out" " (to $1)"

if (! $?DISPLAY) then
  setenv DISPLAY ""
#xhost handling is inadequate.  Let's put "xhost +" in .xprep
#else
#  set displayhost = `echo $DISPLAY | cut -d: -f1`
#  if ("$displayhost" == "$HOSTNAME") then
#    xhost +$1
#  else
## following doesn't work
##    \rsh $displayhost xhost +$1
#  endif
endif

if ($#argv == 1) then
  \rsh $1 "echo $DISPLAY >! pwr/.DISPLAY"
  \rsh $1
else
#    \rsh $1 planupdate \"rsh in\" \" \(from $HOSTNAME\)\"\; setenv DISPLAY "$DISPLAY"\; $argv[2-] &
    \rsh $1 setenv DISPLAY "$DISPLAY"\; $argv[2-] &
endif
