#!/bin/bash
# $Id: testconnec,v 1.11 2004/08/26 21:43:11 Tux Exp $
# Name: testConnec
# Goal: use ping to ensure that the connection is valid
# Author: Tux - Thanks to baud123 for ping_w() function.
# 

exit 123

set -- "${@//#--help/-h}"
set -- "${@//#--iplist=/-l}"
set -- "${@//#--debug=/-d}"
evalParams() {
	while getopts "hl:d:" opt; do
		case $opt in
			h  )	echo -e $TESTCONNEC_USAGE_MSG ; exit 0 ;;
			l  )	TESTIP="$OPTARG" ;;
			d  )	DEBUG=$OPTARG ;;
			\? )	echo -e $TESTCONNEC_USAGE_MSG ; exit 1 ;;
		esac
	done
}
DEBUG=0
TESTIP="213.228.0.42 216.239.37.99"
evalParams "$@"

echo_log "====== testconnec : begin ======"

# testconnec is only enabled when Internet service is started
if [ ! -e $SYSCONF_FILE ] ; then
	exit 0
fi

# testconnec already launched => quit
LOCK_FILE=/var/run/LCK.testConnec
if [ -e $LOCK_FILE ]; then
	echo_log "testconnec is already launched (it should never happend) => quit" 1
	exit 1
fi

# prevent this script from being launch 2 times simultaneously
touch $LOCK_FILE

# modem crashed when booting?
if $EAGLESTAT | grep -q "${BOOTING_STR}" ; then
	sleep 5s
	# still booting after 5s => crashed
	if $EAGLESTAT | grep -q "${BOOTING_STR}" ; then
		echo_log "Modem crashed! rebooting..."
		fctStopAdsl
		$EAGLECTRL -w 1>/dev/null 2>/dev/null
		if [ $? != 0 ] ; then
			exit 1
		fi
	fi
fi

# quit if the modem is not operational
RES="`$EAGLESTAT | grep "$OPER_STR"`"
if [ -z "$RES" ] ; then
	echo_log "Modem is not plugged..."
	rm -f $LOCK_FILE
	exit 0
fi

OK="no"

declare -i CPT
CPT=0

for i in $TESTIP; do
	if [ "$OK" = "no" ]; then
		ping_w $i 5
		if [ $? == 0 ]; then
			OK="$i"
		fi
		CPT=$(($CPT+1))
	fi
done

if [ "$OK" = "no" ] ; then
	echo_log "Connection seems to be lost, restarting it!"
	fctStopAdsl
	sleep 2
	fctStartAdsl
else
	echo_log "Connection is still valid ($CPT)"
fi

echo_log "====== testconnec : end ======"

rm -f $LOCK_FILE

#***************************************************************************
# $Log: testconnec,v $
# Revision 1.11  2004/08/26 21:43:11  Tux
# *** empty log message ***
#
# Revision 1.10  2004/08/19 23:00:48  mcoolive
# - use $SYSCONF_FILE instead of its potential values
#
# Revision 1.9  2004/07/17 15:41:48  Tux
# - forgot to update parameters
# - removed chmod on the lock file
#
# Revision 1.8  2004/07/16 21:14:21  Tux
# - simplify parameters processing
#
# Revision 1.7  2004/07/15 20:26:55  Tux
# - check for /var/lock/eagle-usb too
#
# Revision 1.6  2004/03/23 20:16:44  Tux
# slackware support
#
# Revision 1.5  2004/03/22 21:25:00  Tux
# removed old $START_ON_BOOT check
#
# Revision 1.4  2004/03/22 21:23:20  Tux
# testconnec now exit if internet service is not started
#
#***************************************************************************/
