#!/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=$(CreoleGet nom_zone_eth$numint)
    echo -n "$int: "
    if ! /sbin/ifconfig $int > /dev/null 2>&1
    then
        EchoRouge "inexistante"
    else
        set -o pipefail
        if ! ethtool $int 2>/dev/null \
                | grep -E "Speed|Duplex|Link detected" \
                | tr -d '\t' \
                | tr '\n' ' '
        then
            EchoRouge " impossible de diagnostiquer l'interface"
        else
            echo
        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
    if [ $(CreoleGet eth${Cpt}_method) != 'manuel' ]; then
        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
    else
        echo -n "interface-${Cpt}: " && echo "configuration manuelle"
    fi
    let Cpt=Cpt+1
done
echo

EchoGras "*** Services distants"

if [ -n "$adresse_ip_gw" ] && [ "$adresse_ip_gw" != "0.0.0.0" ]; then
    if [ "$(CreoleGet activer_agregation non)" = "oui" ]; then
        TestARP $adresse_ip_gw "Passerelle $adresse_ip_gw" "$(CreoleGet nom_zone_eth0)"
        for passerelle in $(CreoleGet alias_gw_eth0); do
            TestARP $passerelle "Passerelle $passerelle" "$(CreoleGet nom_zone_eth0)"
        done
    else
        TestARP $adresse_ip_gw "Passerelle $adresse_ip_gw"
    fi
fi

IPS_DNS_USER=$(CreoleGet adresse_ip_dns)
IPS_DNS_DHCP=$(CreoleGet adresse_ip_dns_du_dhcp)
if [ -n "${IPS_DNS_USER}" ]
then
    IPS_DNS="${IPS_DNS_USER}"
elif [ -n "${IPS_DNS_DHCP}" ]
then
    IPS_DNS="${IPS_DNS_DHCP}"
fi

for dns in $(CreoleGet ad_address '') $(CreoleGet ad_rescue '') ${IPS_DNS}; do
    for dnsname in $(CreoleGet test_distant_domaine); do
        TestDns $dns $dnsname
        [ $? = 0 ] && break
    done
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
	TestNTP "$(CreoleGet serveur_ntp)"
    TestWeb "$label" $(CreoleGet test_distant_domaine)
fi
echo

Cpt=0;
while [ $Cpt -lt $nombre_interfaces ]; do
    if [ $(CreoleGet eth${Cpt}_method) != 'manuel' ]; then
        Eth="$(CreoleGet nom_zone_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 [ $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
    fi
    let Cpt=Cpt+1
done
echo

exit 0
