#!/bin/sh
DIALOG=${DIALOG=dialog}
 fichtemp=`tempfile 2>/dev/null` || fichtemp=/tmp/test$$ 
trap "rm -f $fichtemp" 0 1 2 5 15 
$DIALOG --clear --backtitle "Reseau.sh" --title "Moovyplay Diagnostic" \
        --menu "Bienvenu dans le script Reseau.sh :\nChoissisez une action ?" 0 0 0 \
         "1" " Config reseau pour Serveur" \
         "2" " Config reseau pour un pc de selection" \
         "3" " Config CUSTOM" \
         "4" " Faire un test de ping" \
         "Q" " Quitter le script" 2> $fichtemp 

choix=`cat $fichtemp` 
case $choix in
  
  1)
  cat /script/network/rsx > /etc/network/interfaces
  cat /script/network/dns > /etc/resolv.conf
  cat /script/network/hosti > /etc/hosts
  ;;
   2)
  cat /script/network/rsc > /etc/network/interfaces
  cat /script/networkd/dns > /etc/resolv.conf
  cat /script/network/hosto > /etc/hosts
   ;;
   3) 
ip=`tempfile 2>/dev/null` || ip=/tmp/test$$ 
trap "rm -f $ip" 0 1 2 5 15 
$DIALOG --backtitle "Reseau.sh" --title "Configuration CUSTOM" --clear \
        --inputbox "Bonjour, Veuilez entrer une adresse ip:" 8 51 2> $ip 
valret=$? 
ip=`cat $ip`

if [ $valret -eq 0 ] 
then
echo "# This file describes the network interfaces available on your system" > CUSTOM
echo "# and how to activate them. For more information, see interfaces(5)." >> CUSTOM
echo "" >> CUSTOM
echo "# The loopback network interface" >> CUSTOM
echo "auto lo" >> CUSTOM
echo "iface lo inet loopback" >> CUSTOM
echo "" >> CUSTOM
echo "# The primary network interface" >> CUSTOM
echo "auto eth0" >> CUSTOM
echo "allow-hotplug eth0" >> CUSTOM
echo "iface eth0 inet static" >> CUSTOM
echo "        address $ip" >> CUSTOM
else
echo "        address 192.168.1.11" >> CUSTOM
$DIALOG --infobox "Ca a foirer." 0 0
fi

mask=`tempfile 2>/dev/null` || mask=/tmp/test$$
trap "rm -f $mask" 0 1 2 5 15
$DIALOG --backtitle "Reseau.sh" --title "Configuration CUSTOM" --clear \
        --inputbox "Bonjour, Veuilez la route reseau:" 8 51 2> $mask
mask=`cat $mask`

if [ $valret -eq 0 ]
then
echo "        netmask $mask" >> CUSTOM
elif [ $mask = " " ]
then
echo "        netmask 255.255.255.0" >> CUSTOM
else
true
$DIALOG --infobox "Ca a foirer." 0 0
fi


net=`tempfile 2>/dev/null` || net=/tmp/test$$
trap "rm -f $net" 0 1 2 5 15
$DIALOG --backtitle "Reseau.sh" --title "Configuration CUSTOM" --clear \
        --inputbox "Bonjour, Veuilez le debut de la route reseau:" 8 51 2> $net
net=`cat $net`

if [ $valret -eq 0 ]
then
echo "        network $net" >> CUSTOM
else
echo "        network 192.168.1" >> CUSTOM
$DIALOG --infobox "Ca a foirer." 0 0
fi

broad=`tempfile 2>/dev/null` || broad=/tmp/test$$
trap "rm -f $broad" 0 1 2 5 15
$DIALOG --backtitle "Reseau.sh" --title "Configuration CUSTOM" --clear \
        --inputbox "Bonjour, Veuilez la fin de la route reseau:" 8 51 2> $broad
broad=`cat $broad`

if [ $valret -eq 0 ]
then
echo "        broadcast $net" >> CUSTOM
else
$DIALOG --infobox "Ca a foirer." 0 0
fi

gate=`tempfile 2>/dev/null` || gate=/tmp/test$$
trap "rm -f $gate" 0 1 2 5 15
$DIALOG --backtitle "Reseau.sh" --title "Configuration CUSTOM" --clear \
        --inputbox "Bonjour, Veuilez la passerelle par Defaut:" 8 51 2> $gate
gate=`cat $gate`

if [ $valret -eq 0 ]
then
echo "        gateway $gate" >> CUSTOM
#echo "nameserver $gate" > /etc/resolv.conf
else
echo "        gateway 192.168.1.1" >> CUSTOM
$DIALOG --infobox "Ca a foirer." 0 0
fi

(echo "10" ; sleep 3 ; 

echo "XXX"; 
echo "Loading..."
echo "\\n"
echo "Recuperations de la saisie...";
echo "XXX";
echo "20"
sleep 3; 

echo "XXX";
echo "Initialisation du fichier de config..."
echo "\n"
echo "Test de la configuration reseau..."
echo "XXX"
echo "50"
sleep 2;


echo "75" ; sleep 3
echo "XXX" 
echo "SUCCES !"
echo "XXX"
echo "100") | \

$DIALOG --title "Configuration CUSTOM" --gauge "Demarrage...\nConfiguration CUSTOM en cours..." 10 50 0

if [ "$?" = 255 ] ; then
	echo ""
	echo "Box closed !"
fi


   ;;
   4)

   dialog --infobox "test de connexion vers google.fr\n`ping google.fr -c 4`" 0 0
   ping google.fr -c 2
   if [ $? -eq 0 ]
    then
    dialog --msgbox "Connexion [OK]\n :)" 0 0
   else
    dialog --infobox "la connexion est pourrie..refaire les config" 0 0
   fi
    ;;

   Q)
   dialog --infobox "Bye !" 0 0
   ;; 
esac
