#/bin/sh
# Script to execute xterm  or other X app on remote machine transferring
# correct auth info 
# Usage:
#     foreign_xterm [rsh options] host [xterm options]
#     foreign_xapp  [rsh options] host command [command options]
#
# Some installations don't have X binary dirs in default PATH, used for 
# rsh executing. This varliable lets you append them manually
XBINDIRS="/usr/X11R6/bin:/usr/openwin/bin"
# You might prefer rxvt or eterm, but make sure that it is available
# on all machines you can rsh in
XTERMCMD=xterm 
RSH=ssh

# If display environment variable, starts with colon, append hostname
case $DISPLAY in
:*) DISPLAY=`hostname`:0
;;
esac 
myname=$0
cont=y
RSH_OPT=
while [ "$cont" = "y" ]; do
  case $1 in
   -l|-k) RSH_OPT="$RSH_OPT $1 $2"
          shift 
	  shift
   ;;
  -*) RSH_OPT="$RSH_OPT $1"
            shift
   ;;
   *) # First non-option arg is hostname
      HOST=$1
      shift
      cont=n
      
   ;;
   esac
done   
# let path be expanded on _that_ side
if [ -n "$XBINDIRS" ]; then
   MYPATH=\$PATH:$XBINDIRS
else
   MYPATH=\$PATH
fi   
# If we a called as foreign_xterm, add xterm command and titles to
# command to execute. Otherwise just add arg -display
case $myname in
*xterm*)
    set -- $XTERMCMD -display $DISPLAY -ls -T "\"Shell on $HOST\"" -n $HOST "$@"
    ;;
 *)
    cmd=$1
    shift
    set -- DISPLAY=$DISPLAY $cmd "$@"
    ;;
esac    
# transfer authentication
xauth nextract - $DISPLAY | ${RSH} $RSH_OPT $HOST PATH=$MYPATH xauth nmerge -

${RSH} -n $RSH_OPT $HOST PATH=$MYPATH "$@" &
