#!/bin/sh

# need to use gnu test because dec-ultrix test doesn't support the -x flag.
if [ ! -d /mit/gnu ]; then
    attach -q -n -h -a -r gnu
fi

RTEL_USER=$USER
RTEL_DES=
RTEL_ATTACH=true
RTEL_RTKT=true
RTEL_GETROOT=false
RTEL_XTITLE=false
RTEL_EXEC=false
RTEL_ATTACHFL=
if /mit/gnu/bin/gtest -x /bin/hostname ; then
   RTEL_HOST=`/bin/hostname`
else
   if /mit/gnu/bin/gtest -x /usr/bsd/hostname ; then
      RTEL_HOST=`/usr/bsd/hostname`
   fi
fi

if [ x$RTEL_RKNTBIN = 'x' ]; then
   RTEL_RKNTBIN=/usr/athena/bin/rkinit
   if /mit/gnu/bin/gtest -x /srvd/patch/rkinit ; then
      RTEL_RKNTBIN=/srvd/patch/rkinit
   fi
fi

if [ x$RTEL_RLOGBIN = 'x' ]; then
   RTEL_RLOGBIN=/usr/athena/bin/rlogin
#   if [ `/bin/athena/machtype` = 'sun4' ]; then
#      RTEL_RLOGBIN=/afs/athena.mit.edu/user/j/w/jweiss/sun4bin/rlogin
#   fi
fi

if [ x$RTEL_RSHBIN = 'x' ]; then
   RTEL_RSHBIN=/usr/athena/bin/rsh
fi

if [ x$RTEL_KLISTBIN = 'x' ]; then
   RTEL_KLISTBIN=/usr/athena/bin/klist
fi

while [ x$1 != 'x' ]; do
   case $1 in
      -l)
         if [ x$2 != "x" ]; then
            RTEL_USER=$2
            shift; shift
         else
            echo "Error: no username specified with -l argument."
            exit 1
         fi ;;
      -noattach)
         RTEL_ATTACH=false
         shift ;;
      -roottickets)
         RTEL_GETROOT=true
	 KRBTKFILE=$2 ; export KRBTKFILE
         shift ; shift ;;
      -notickets)
         RTEL_RTKT=false
	 RTEL_ATTACHFL=-n
         shift ;;
      -exec)
         RTEL_EXEC=true
         shift ;;
      -xtitle)
         RTEL_XTITLE=true
         shift ;;
      -x)
         RTEL_DES=-x
         shift ;;
      *)
         RTEL_HOST=$1
         shift ;;
   esac
done

if [ $RTEL_GETROOT = 'true' ]; then
   if $RTEL_KLISTBIN -t; then
      echo >> /dev/null # do something
   else
      until kinit $USER.root; do
         fmt >&2 << _END1_


Wrong password. Try again.

_END1_
      done
   fi
fi

if [ $RTEL_RTKT = 'true' ]; then
   until $RTEL_RKNTBIN $RTEL_HOST -l $RTEL_USER; do
      fmt >&2 << _END2_


Wrong password. Try again.

_END2_
   done
fi

if [ $RTEL_ATTACH = 'true' -a $RTEL_USER != 'root' ]; then
    $RTEL_RSHBIN $RTEL_HOST -l $RTEL_USER /bin/athena/attach $RTEL_ATTACHFL $RTEL_USER
fi


if [ $RTEL_XTITLE = 'true' ]; then
   echo -n "]0;Rlogin[$RTEL_HOST]"
fi

if [ $RTEL_EXEC = 'true' -o $RTEL_XTITLE = 'false' ]; then
   exec $RTEL_RLOGBIN $RTEL_HOST $RTEL_DES -x -l $RTEL_USER
else
   $RTEL_RLOGBIN $RTEL_HOST $RTEL_DES -l $RTEL_USER
fi

if [ $RTEL_XTITLE = 'true' ]; then
   echo -n "]0;Xterm[`/bin/hostname`]"
fi
