#!/bin/bash

. /usr/lib/eole/diagnose.sh

#EOLERELEASE="/etc/eole/release"
#if [ ! -e $EOLERELEASE ]; then
#    EchoRouge "Fichier $EOLERELEASE est introuvable"
#    exit 1
#fi
#. $EOLERELEASE
#
#EchoGras "*** Test du module $EOLE_MODULE version $EOLE_VERSION ($nom_machine $numero_etab) ***"
#echo

EOLE_DIR="/usr/share/eole/creole"
nom_machine=$(CreoleGet nom_machine)
adresse_ip_gw=$(CreoleGet adresse_ip_gw)
nombre_interfaces=$(CreoleGet nombre_interfaces)

EchoGras "*** Cartes réseau"
for numint in `seq 0 $[nombre_interfaces-1]`; do
	int=eth$numint
	ifconfig $int > /dev/null 2>&1
	if [ ! $? = 0 ]; then
		echo -n "$int: "
		EchoRouge "inexistante"
	else
		mii-tool $int 2>/dev/null
		if [ $? -eq 1 ]; then
			echo -n "$int:"
			#ethtool $int 2>/dev/null | grep -E "Link"
			set -o pipefail
			ethtool $int 2>/dev/null|grep -E "Speed|Duplex|Link"|tr -d '\n'|sed 's/\t/ /g'
			if [ $? -ne 0 ]
			then
				EchoRouge " impossible de diagnostiquer l'interface"
			else
				echo
			fi
		fi

	fi
done
cat /proc/net/dev | awk -F ":" '{print $1}' | sed 's/[[:blank:]]//g' | grep "^eth" | grep -v '\.' | sed 's/^eth//g'| sort -n | while read num; do
	[ $num -ge $nombre_interfaces ] && echo -n "eth$num: " && EchoOrange "non prise en compte"
done
echo

EchoGras "*** Interfaces"
Cpt=0;
while [ $Cpt -lt $nombre_interfaces ]; do
    ip=$(CreoleGet adresse_ip_eth$Cpt)
    name=$(CreoleGet nom_machine_eth$Cpt 2>/dev/null)
    if [ -z "$name" ];then
        name=$nom_machine
    fi
    TestIP $ip $name
    let Cpt=Cpt+1
done
echo

EchoGras "*** Services Distants"

if [ -n "$adresse_ip_gw" ] && [ "$adresse_ip_gw" != "0.0.0.0" ];then
    TestARP $adresse_ip_gw "Passerelle $adresse_ip_gw"
fi

for dns in $(CreoleGet adresse_ip_dns);do
    TestDns $dns google.fr
done

# hack pour que le libellé soit aligné malgré l'accent ;)
len=$((len_pf+1))
label=`printf "%${len}s" "Accès distant"`

if [ -z "$adresse_ip_gw" ];then
    #si pas de GW, pas d'accès distant
    Inactif "$label"
else
    for ntp in $(CreoleGet serveur_ntp);do
        TestNTP $ntp "NTP $ntp"
    done
    TestWeb "$label" $(CreoleGet test_distant_domaine)
fi
echo

Cpt=0;
while [ $Cpt -lt $nombre_interfaces ]; do
    Eth="eth$Cpt"
    Adresse=$(CreoleGet adresse_ip_eth$Cpt)
    ssh_actif=$(CreoleGet ssh_eth$Cpt oui)
    adm_actif=$(CreoleGet admin_eth$Cpt oui)
    echo "Sur l'interface réseau $Eth"
    if [ $Cpt -eq 0 ];then
        if [ $(CreoleGet activer_firewall) = 'non' -a $Cpt -eq 0 ];then
            Inactif "Pare-feu"
        else
            TestFirewall
        fi
    fi
    if [ $ssh_actif =  "oui" ];then
        TestService "SSH" $Adresse:22
    else
        Inactif "SSH"
    fi
    if [ $adm_actif = "oui" ];then
        TestService "EAD Server" $Adresse:4201
        if [ "$(CreoleGet activer_ead_web 2>/dev/null)" = "oui" ];then
            TestService "EAD Web" $Adresse:4200
        else
            Inactif "EAD Web"
        fi
    else
        Inactif "EAD Web"
    fi
    let Cpt=Cpt+1
done
echo

exit 0
