#!/bin/csh -f
if ($#argv == 0) then
   echo 'Usage: xlogin [-n] host ["xterm-options" [rsh-options]]'
else

if (! ($TERM =~ xt*)) then
   rsh $*
   exit
endif

if ($?FORK) then
   if ($?XT) setenv TERM ${XT}.$1
   unsetenv XT
   shift
   rsh $*
   exit
endif
setenv FORK true

set x=""
if ($?X) set x=`echo  $X | sed s.X..`
setenv XT xt$x
unset x

if ($1 == "-n") then
   unsetenv XT
   shift
endif

set names=`gethost -sF $1 -f $1 $DISPLAY`
if ($#names < 3) then
   echo "Invalid host name: $1 or $DISPLAY"
   exit
endif

set rname=$names[2]
shift
set xfunc=""
if ($#argv > 0) then
   set xfunc=($1)
   shift
endif

xhost $rname
(xterm -n $names[1] $xfunc -e xlogin $names[3] $rname $* &)
exit

endif
