#! /bin/sh

# remote-login.sh - Version 1.0
# Let you do a remote (or local) login to any machine on your network
#
# Copyright (c) 2000 Raphaël HALIMI <raph@captainblood.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

# Hostnames of the machines you want to connect to. Change this to suit your
# needs, by entering the hostnames separated by spaces

VERSION="1.0"

MACHINES="arche aldebaran saga lfo"

# Command to launch for login (ssh, rlogin, telnet...)

LOGIN_COMMAND=ssh

# Some variables you don't need to change

COUNT=1
NUMBER_OF_MACHINES=`echo $MACHINES | wc -w`

# Function to change window name
function change_window_name {
if [ "$TERM" = "xterm" ] ; then
   # Dirty, but it works. If you have a better way do do this, mail me :-)
   NEWNAME=`echo $1 | awk '{A = length ($0); if (A == 1) { printf ("%c\n", toupper (substr($0, 1, 1)))} else {if (A > 1) {printf ("%c%s\n", toupper (substr($0, 1, 1)), substr ($0, 2))} else {printf ("\n")}}}'`
   echo -ne "\033]0; $NEWNAME -- Remote Login \007"
fi
}

# Here we go... First we print the hostanmes list

clear ; echo "Which host do you want to login to ?"

echo

for I in $MACHINES ; do
    if [ $I = $HOSTNAME ] ; then		# Tests if the machine is
       echo "$COUNT) Login to $I (localhost)"	# the localhost, and if so,
       COUNT=$[ $COUNT + 1 ]			# we indicate it :-)
    else
       echo "$COUNT) Login to $I"
       COUNT=$[ $COUNT + 1 ]
    fi
done

echo ; echo -n "Please enter a number between 1 and $[ $COUNT - 1 ] to login, or ENTER to exit: "

# Now we wait for an answer

read MACHINE_NUMBER

# If that answer is empty (--> user pressed ENTER alone), terminate the script

if [ -z $MACHINE_NUMBER ] ; then
   exit
fi

# Check if the answer is in the correct range

while [ $MACHINE_NUMBER -lt 1 -o $MACHINE_NUMBER -gt $NUMBER_OF_MACHINES ] ; do
      echo ; echo "Sorry, this is not a valid number."
      echo -n "Please enter a number between 1 and $[ $COUNT - 1 ] (or Ctrl-C to exit): "
      read MACHINE_NUMBER
done


# Now we check which hostname the number entered matches to, and log to the
# matching host

COUNT=1

for I in $MACHINES ; do
    if [ $MACHINE_NUMBER = $COUNT ] ; then
       if [ $I = $HOSTNAME ] ; then	# If the machine is the local host,
          				# we start a simple shell instead
       					# of a remote login process
          echo -n "Login as user [$USER] : " ; read REMOTE_USER
	  if [ -z $REMOTE_USER ] ; then
	     echo ; echo "Login to $I as user $USER." ; echo
	     change_window_name $I
	     sh --login
	  else
	     echo ; echo "Login to $I as user $REMOTE_USER." ; echo
	     change_window_name $I
	     su - $REMOTE_USER
	  fi
       else				# of a remote login process
          echo -n "Login as user [$USER] : " ; read REMOTE_USER
	  if [ -z $REMOTE_USER ] ; then
	     echo ; echo "Login to $I as user $USER." ; echo
	     change_window_name $I
	     $LOGIN_COMMAND $I
	  else
	     echo ; echo "Login to $I as user $REMOTE_USER." ; echo
	     change_window_name $I
	     $LOGIN_COMMAND -l $REMOTE_USER $I
	  fi
       fi
    fi
    COUNT=$[ $COUNT + 1 ]
done
