#!/bin/sh
# Name : eaglediag
# Goal : diagnostic script for eagle-usb driver (formerly adiusbadsl)
# Author : Benoit Audouard aka baud123 (baud at magic dot fr)
# Description : identifie les elements permettant un diagnostic
#       type de systeme, verification de l'install
#       verification bon fonctionnement modem
#	verification bon fonctionnement couche reseau
# ce script s'adapte a la version 1.0.4e des drivers et au-dessus
# il doit etre lance sous root
# Availability : see http://baud123.free.fr/eagle/
# Version : 0.9 20031017
VERSION_DIAG="v0.9"

# todo
# - ajouter un filtre sur /var/log/messages (mots cle grep -iE: pppd, usb, adi, ...)
# - obtenir un numero de version du driver eagle-usb (meme celles de dev)
# - obtenir le type de connexion (degroupe ok, Free/9Tel/...)
# - traduire completement les commentaires en anglais

# done
# 0.5 take into account eagle-cvs and eagle-1.0.4
# 0.6 correction anomaly eagleusb.conf => eagle-usb.conf (ballot)
# 0.7 20031007 do not rely on DEGROUPE=0 (1.0.4e specific...), choose VCI=23(Free) or 24 (Degroupe)
#     replace * with # to avoid unwanted copy/paste and rpm exist for red-hat !
# 0.8 20031013 rename script from diag_eagle_usb.sh to eaglediag, use tee instead of tail -f
#     to avoid a kill in the end...
# 0.9 20031017 module is eagle-usb not adiusbadsl since 1.0.5, deactivate 1.0.4e service check
#     remaining translations...

# INIT
# global variables
# date format YYYYMMDDHHmmss
TIME_STAMP=`date +"%Y""%m""%d""%H""%M""%S"`

# Paths
LOG_FILE=/var/log/eagle_diag_${TIME_STAMP}.txt

SYSCONF_FILE=/var/lock/subsys/adiusbadsl # for service in 1.0.4e
#SYSCONF_FILE=/etc/sysconfig/adiusbadsl
#EAGLEDIR=/usr/local/eagleusb
EAGLECONF=/etc/analog/adiusbadsl.conf
EAGLE_VERSION="1.0.4"
EAGLE_TEST_E=1 # verify 1.0.4e version
EAGLE_STAT=showstat
EAGLE_USB=adiusbadsl
# v1.0.5pre1cvs : directory change
if [ -f /etc/eagle-usb/eagle-usb.conf ] ; then
	EAGLECONF=/etc/eagle-usb/eagle-usb.conf
	EAGLE_VERSION="1.0.5pre1cvs"
	EAGLE_TEST_E=0 # do not verify 1.0.4e version
	EAGLE_STAT=eaglestat
	EAGLE_USB="eagle-usb"
fi

# Strings:
OPERSTR="Modem is operational"
IP_FREE="213.228.0.42"
URL_FREE="www.free.fr"

# GET_INFO
# groupe ou degroupe ? unlucky ou lucky ?
# by default NON DEGROUPE (Free)
FREE_DEGROUPE=0
if [ -f ${EAGLECONF} ] ; then
	if [ "X"`cat ${EAGLECONF}|grep -E "^VCI="` == "X""VCI=00000023" ] ; then
		FREE_DEGROUPE=0
	else
		FREE_DEGROUPE=1
	fi
fi

# Mandrake and Red-Hat have rpm
RPM_EXIST=0 ## trouver le test qui va bien...
if [ -f /etc/mandrake-release ] ; then
	DISTRIB="mdk"
	RPM_EXIST=1
else 
	if [ -f /etc/redhat-release ] ; then
             RPM_EXIST=1
        else
	     DISTRIB="other"
        fi
fi
CMDECHO="echo"

# START DIAG
${CMDECHO} "Lancement du diagnostic ${VERSION_DIAG}"
${CMDECHO} "Diagnostic ${VERSION_DIAG} du driver eagleusb le ${TIME_STAMP}" | tee -a ${LOG_FILE}

# System Information 
${CMDECHO} "# System Information" | tee -a ${LOG_FILE}
uname -a | tee -a ${LOG_FILE}
if [ ${DISTRIB} == "mdk" ] ; then
	cat /etc/mandrake-release | tee -a ${LOG_FILE}
fi
cat /proc/version | tee -a ${LOG_FILE}
# gcc version begins by gcc
gcc -v 2>&1 |grep -iE "^gcc" | tee -a ${LOG_FILE}

# Verify dependances of eagle-usb driver on source
# if a binary RPM is used, no need for this
# si c'etait un RPM qui etait livre, il n'y aurait pas besoin de cela...
${CMDECHO} "# Dependances" | tee -a ${LOG_FILE}
if [ ${RPM_EXIST} -eq 1 ] ; then
	rpm -q kernel-source 2>&1 | tee -a ${LOG_FILE} 
	rpm -q patch 2>&1 | tee -a ${LOG_FILE} 
	rpm -q slocate 2>&1 | tee -a ${LOG_FILE}
else
	${CMDECHO} "Verifier dependances a la main (kernel-source...)" | tee -a ${LOG_FILE}
	#${CMDECHO} "Verify dependances on source by hand (kernel-source...)" >> ${LOG_FILE}
fi

# Another verification (which source is used ?)
# deux verifs valent mieux qu'une (3 en comptant le uname)
ls -l /usr/src/ | grep -i linux 2>&1 | tee -a ${LOG_FILE}

# Verify modules configuration
# Verifie config des modules
${CMDECHO} "# Kernel config : usb modules" | tee -a ${LOG_FILE}
lsmod | grep usb | tee -a ${LOG_FILE}

if [ ${EAGLE_TEST_E} -eq 1 ] ; then
	# valid for 1.0.4e
	$CMDECHO "# service started ? (info for 1.0.4e)" | tee -a ${LOG_FILE}
	if [ -f $SYSCONF_FILE ] ; then
		$CMDECHO "[ OK ]" | tee -a ${LOG_FILE}
	else
		$CMDECHO "[ KO ]" | tee -a ${LOG_FILE}
	fi
fi
$CMDECHO "# module loaded ?" | tee -a ${LOG_FILE}
if lsmod | grep -q "${EAGLE_USB}" ; then
	$CMDECHO "[ OK ]" | tee -a ${LOG_FILE}
else
	$CMDECHO "[ KO ]" | tee -a ${LOG_FILE}
fi
$CMDECHO "# modem operational ?" | tee -a ${LOG_FILE}
if ${EAGLE_STAT} | grep -q "Modem is operational" ; then
	$CMDECHO "[ OK ]" | tee -a ${LOG_FILE}
else
	$CMDECHO "[ KO ]" | tee -a ${LOG_FILE}
fi
# un petit showstat tout de meme (donne l'attenuation pour les deconnexions)
# provide showstat result (gives attenuation)
# identify VPI/VCI in decimal
${EAGLE_STAT} | tee -a ${LOG_FILE}

# si le service est lance (faut un minimum d'effort tout de meme)
# commente : n'a plus l'air de fonctionner avec Mandrake 9.2 ?
#if [ -f $SYSCONF_FILE ] ; then
	SERVICE_CONNEXION=0 # le service de connexion est operationnel ?
	# ce serait pas mal d'avoir plus d'info que groupe/degroupe
	# maintenant ya 9telecom, les anglais, ...
	if [ ${FREE_DEGROUPE} -eq 0 ] ; then
		$CMDECHO "# Config eagle : non degroupe Free" | tee -a ${LOG_FILE}
		$CMDECHO "# pppd lance ?" | tee -a ${LOG_FILE}
		if [ `pidof pppd` ] ; then
			$CMDECHO "[ OK ]" | tee -a ${LOG_FILE}
			SERVICE_CONNEXION=1
		else
			$CMDECHO "[ KO ]" | tee -a ${LOG_FILE}
		fi
		# ajouter tests divers lie au non degroupe
		# add tests related to "non dgroupe"
	else
		$CMDECHO "# Config eagle : degroupe Free" | tee -a ${LOG_FILE}
		$CMDECHO "# dhcpclient lance ?" | tee -a ${LOG_FILE}
		SERVICE_CONNEXION=1
		# tests divers lie au degroupe free
		# tests related to "Free degroupe"
		# pppd ne doit pas etre lance
		# pppd should not be launched
		if [ `pidof pppd` ] ; then
			$CMDECHO "[ KO pppd lance en degroupe ]" | tee -a ${LOG_FILE}
		fi
	fi
	if [ ${SERVICE_CONNEXION} -eq 1 ] ; then
		$CMDECHO "Un service de connexion est lance" | tee -a ${LOG_FILE}
	fi
	# bah on fait quand meme ces tests 
	# (avec IP fixe, il ne devrait pas yavoir besoin de dhcpclient...)
	# ya des infos sur le LAN que l'utilisateur devrait cacher
	$CMDECHO "/etc/resolv.conf doit contenir les DNS" | tee -a ${LOG_FILE}
	# debug : verifier que ca marche si c'est un lien
	if [ -f /etc/resolv.conf ] ; then
		cat /etc/resolv.conf | tee -a ${LOG_FILE}
	fi
	ifconfig -a | tee -a ${LOG_FILE}
	route -n | tee -a ${LOG_FILE}
		
	$CMDECHO "# ping vers IP ?        " | tee -a ${LOG_FILE}
	/bin/ping -n -q -c 1 -w 5 ${IP_FREE} 1>/dev/null 2>/dev/null
	if [ $? = 0 ] ; then
		$CMDECHO "[ OK ]" | tee -a ${LOG_FILE}
	else
		$CMDECHO "[ KO ]" | tee -a ${LOG_FILE}
	fi
	$CMDECHO "# test rsolution DNS ? " | tee -a ${LOG_FILE}
	/bin/ping -n -q -c 1 -w 5 ${URL_FREE} 1>/dev/null 2>/dev/null
	if [ $? = 0 ] ; then
		$CMDECHO "[ OK ]" | tee -a ${LOG_FILE}
	else
		$CMDECHO "[ KO ]" | tee -a ${LOG_FILE}
	fi
#fi

$CMDECHO "Le diagnostic est disponible dans le fichier ${LOG_FILE}"
$CMDECHO "Pensez a le trier et a enlever les informations sensibles"
exit 0

