#!/bin/bash
# $Id$
# Copyright (C) 2004-2005 Benoit Audouard aka baud123 (baud at magic dot fr)
#
# 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.

######
###                     Definition of global variables                      ###
###_________________________________________________________________________###

VERSION_DIAG=`head -n 2 $0|grep Id | cut -d " " --fields=4-5`

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

IP_FREE="212.27.48.10"
URL_FREE="www.free.fr"

# Display color only if the standard output is connected to a terminal
# which understand the escape sequences which define colors.
USE_COLORS="false"
if test -t 1; then
	case $TERM in
		linux | rxvt | xterm ) USE_COLORS="true" ;;
	esac
fi

# Paths to log depending on user that launches the program (root or standard)
if [ `id -u` -eq 0 ]; then
	LOG_DIRECTORY=/var/log/ueagle-atm
else
	PATH=$PATH:/usr/sbin:/usr/local/sbin:/sbin
	LOG_DIRECTORY=$HOME/.ueagle-atm
fi

LOG_FILE=$LOG_DIRECTORY/ueaglediag_${TIME_STAMP}.txt

######
###                     Definition of global functions                      ###
###_________________________________________________________________________###

echoLog () {
	# display to screen & into log file
	# add a -n to display without "\n" at the end (result following...)
        echo "$@" | tee -a ${LOG_FILE}
}

######
###                    MAIN - Display diagnostics                           ###
###_________________________________________________________________________###


# create log directory if necessary
if [ ! -d $LOG_DIRECTORY ] ; then
	mkdir $LOG_DIRECTORY
	# make it world writable to use with sudo
	chmod 755 $LOG_DIRECTORY
fi

# an output file is specified... verify I can write
# I made all I could to avoid this... msec is sometimes around :-(
touch ${LOG_FILE}
chmod 0644 ${LOG_FILE}
echo -n >${LOG_FILE}

echoLog "Diagnostic (${VERSION_DIAG}) driver ueagle-atm ${TIME_STAMP}"

# System Information
echoLog "# System Information"
# display kernel information (version, gcc compiler...)
cat /proc/version | tee -a ${LOG_FILE}
echo `gcc -v 2>&1 |grep -iE "gcc " 2>&1 | tee -a ${LOG_FILE}` 

# verify modules loaded, programs launched and interfaces available
lsmod | grep -iE "hci|hcd|usb|atm|firm" | tee -a ${LOG_FILE}
ps auxww|grep -E "atmarp|br2684"  | tee -a ${LOG_FILE}
ifconfig -a  | tee -a ${LOG_FILE}
route -n | tee -a ${LOG_FILE}

# verify if file stats is available in the same directory and can be executed
if [ -x ./ueaglestat ] 
then
	./ueaglestat | tee -a ${LOG_FILE}
elif [ -x /usr/sbin/ueaglestat ] 
then
	/usr/sbin/ueaglestat  | tee -a ${LOG_FILE}
fi

#ping www.free.fr  | tee -a ${LOG_FILE}
echoLog -n "# ping IP ?              "
# -n does not use DNS, -q quiet (), -c 1 un seul paquet envoy,
# -w 5 attend rponse au plus tard en 5s : non compatible debian
#/bin/ping -n -q -c 1 -w 5 ${IP_FREE} 1>/dev/null 2>/dev/null
/bin/ping -n -q -c 1 ${PING_WAIT} ${IP_FREE} 1>/dev/null 2>/dev/null
if [ $? = 0 ] ; then
	echoLog "[ OK ]"
else
	echoLog "[ KO ]"
fi

echoLog -n "# test DNS resolution ?  "         #/bin/ping -n -q -c 1 -w 5 ${URL_FREE} 1>/dev/null 2>/dev/null
/bin/ping -n -q -c 1 ${PING_WAIT} ${URL_FREE} 1>/dev/null 2>/dev/null
if [ $? = 0 ] ; then
	echoLog "[ OK ]"         
else
	echoLog "[ KO ]"
fi

echo "##Complete diagnostic in ${LOG_FILE}"
echo "##Please keep only relevant data and remove personal informations."
exit 0

