#! /bin/sh

# remote-login.sh - Version 0.9
# Permet de se connecter a une quelconque machine (distante ou locale)
# de votre reseau 
#
# 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

# Traduction par Eric Depagne <Eric.Depagne@obspm.fr>

# Nom des machines auxquelles vous voulez vous connecter. Personalisez-le
# en entrant les noms separes par des espaces

MACHINES="192.233.54.85 192.233.54.95 192.233.54.121"

# Diminutifs de ces noms de machines

RACCOURCIS="85 95 121"

# Commande a executer pour se connecter (ssh, rlogin, telnet, ...)

LOGIN_COMMAND="telnet ftp"

# Variables qui n'ont pas besoin d'etre changees

COUNT1=1
NUMBER_OF_MACHINES=`echo $MACHINES | wc -w`
COUNT2=1
NUMBER_OF_COMMAND=`echo $LOGIN_COMMAND | wc -w`

# Début : on choisit le type de connection, ftp ou telnet

clear ; echo "Quel type de connection : $LOGIN_COMMAND ?"

echo

for H in $LOGIN_COMMAND ; do
    echo "$COUNT2) $H"
    COUNT2=$[ $COUNT2 + 1]
done	 

echo ; echo -n "Entrez un nombre entre  1 et $[ $COUNT2 - 1 ] pour se connecter ou ENTREE pour sortir: "

# On attend une réponse

read LOGIN_TYPE

# Si la reponse est vide , on a pressé ENTREE, dans ce cas, on termine le script 
if [ -z $LOGIN_TYPE ] ; then
   exit
fi 

# On vérifie que la reponse a un format attendu

while [ $LOGIN_TYPE -lt 1 -o $LOGIN_TYPE -gt $NUMBER_OF_COMMAND ] ; do
      echo ; echo "Désolé, ce n'est pas un nombre valide."	
      echo -n "Entrez un nombre compris entre 1 et $[ $COUNT2 - 1 ] (ou appuyez sur Ctrl+C pour terminer): "
      read LOGIN_TYPE
done

# On affiche la liste des machines

echo "A quelle machine on se connecte?"

for I in $MACHINES ; do
    if [ $I = $HOSTNAME ] ; then			# Teste si la machine
       echo "$COUNT1) Connection à $I (localhost)"	# est locale, et si
       COUNT1=$[ $COUNT1 + 1 ]				# oui, on le dit :-)
    else
       echo "$COUNT1) Connection à $I"
       COUNT1=$[ $COUNT1 + 1 ]
    fi
done


echo ; echo -n "Entrez un nombre entre  1 et $[ $COUNT1 - 1 ] pour se connecter ou ENTREE pour sortir: "

# On attend une reponse

read MACHINE_NUMBER

# Si la reponse est vide, on a pressé ENTREE, dans ce cas, on termine le script 

if [ -z $MACHINE_NUMBER ] ; then
   exit
fi

# Verifie que la reponse a un format attendu

while [ $MACHINE_NUMBER -lt 1 -o $MACHINE_NUMBER -gt $NUMBER_OF_MACHINES ] ; do
      echo ; echo "Désolé, ce n'est pas un nombre valide."	
      echo -n "Entrez un nombre compris entre 1 et $[ $COUNT1 - 1 ] (ou appuyez sur Ctrl+C pour terminer): "
      read MACHINE_NUMBER
done

# Maintenant, on associe une commande avec le numero choisi

COUNT2=1

for H in $LOGIN_COMMAND ; do
    if [ $LOGIN_TYPE != $COUNT2 ] ; then
       COUNT2=$[ $COUNT2 + 1 ]	
    else
       break
    fi
done	

# Maintenant, on associe un nom de machine au numero entre, et on se connecte
# à cette machine

COUNT1=1

if [ $TERM != linux ] ; then
   for I in $RACCOURCIS ; do
       if [ $MACHINE_NUMBER != $COUNT1 ] ; then
          COUNT1=$[ $COUNT1 + 1 ]
       else				
          echo -n -e "\033]0; $I \007" 	
          break 		
       fi 
   done
fi

COUNT1=1

for J in $MACHINES ; do
    if [ $MACHINE_NUMBER != $COUNT1 ] ; then
       COUNT1=$[ $COUNT1 + 1 ]
    else				
       break 		
    fi
 
done

echo ; echo "Lancement la commande : $H $J..."

echo

$H $J

exit
