#!/bin/bash
# $Id: testconnec,v 1.13 2004/09/28 10:27:19 mcoolive 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 "$@"

# testconnec is only enabled when $SYSCONF_FILE exists
if [ ! -e $SYSCONF_FILE ] ; then
	echo_log "====== testconnec : off, nothing to do... ======"
	exit 0
fi

# eagleconfig has never been run => quit
if [ -f $EU_SCRIPT_DIR/lock ] ; then
	echo_log "$TESTCONNEC_INSTALL_LOCK_MSG" 1
	exit 1
fi

# testconnec already launched => quit
LOCK_FILE=/var/run/LCK.testconnec
if [ -e $LOCK_FILE ]; then
	echo_log "$TESTCONNEC_ALREADY_LAUNCHED_MSG" 1
	exit 1
fi

# prevent this script from being launch 2 times simultaneously
echo_log "====== testconnec : begin ======"
touch $LOCK_FILE
trap 'echo_log "====== testconnec : end ======" ; rm -f $LOCK_FILE' EXIT

# 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 "$TESTCONNEC_REBOOT_MSG"
		fctStopAdsl
		$EAGLECTRL -w 1>/dev/null 2>/dev/null
		if [ $? -ne 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 "$TESTCONNEC_NO_MODEM_MSG"
	#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 [ $? -eq 0 ]; then
			OK="$i"
		fi
		CPT=$(($CPT+1))
	fi
done

if [ "$OK" = "no" ] ; then
	echo_log "$TESTCONNEC_CONNECTION_LOST_MSG"
	fctStopAdsl
	sleep 2
	fctStartAdsl
else
	echo_log "$TESTCONNEC_CONNECTION_OK_MSG ($CPT)"
fi
exit 0

#***************************************************************************
# $Log: testconnec,v $
# Revision 1.13  2004/09/28 10:27:19  mcoolive
# - (POSIX) commands are typed "int main..." => replace "$? ==" by "$? -eq"
#
# Revision 1.12  2004/09/26 19:39:34  Tux
# - improved lock file management
# - better log messages
# - localization
#
# 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
#
#***************************************************************************/
