#!/bin/bash
# $Id: fctStartAdsl,v 1.4 2004/02/21 01:25:02 Tux Exp $
# Name: fctStartAdsl
# Goal: start ADSL connexion (do not display anything)
# Author: Tux
# Params:
#	h = display help
#	m = start mire
#	s = simple mode (don't use ifup & ifdown scripts => do not requiere ifcfg-ethX)
#	t = set timeout delay (default=60s)
#	a = use ip address (use -a xx.xx.xx.xx), imply simple mode
#	d = launch pppd in debug mode
#	i = only put modem interface up (ifup ethX)
#	k = keep: garder la main Ns (ou jusqu' ce que connexion ok)
# Error codes:
#	1 = pppd already lauched (non degroup)
#	2 = modem can't be synchronized
#	3 = can't launch pppd
#	4 = can't set modem interface "up"
#	5 = lock still present

. /etc/eagle-usb/scripts/setvars

for param in $* ; do
	if [ $i -gt 0 ] ; then
			param=${param//--help/-h}
			param=${param//--mire/-m}
			param=${param//--simple/-s}
			param=${param//--timeout=/-t}
			param=${param//--ifup/-i}
			param=${param//--ip=/-a}
			param=${param//--ip/-a}
			param=${param//--keep=/-k}
			param=${param//--debug/-d}
			PARAMS="$PARAMS $param"
	fi
	let i++
done
evalParams() {
	while getopts "hmst:a:dik:" opt; do
		case $opt in
			h  ) echo -e $FCTSTART_USAGE_MSG ; exit 0 ;;
			m  ) MIRE=1 ;;
			s  ) SIMPLE=1 ;;
			t  ) TIMEOUT=$OPTARG ;;
			a  ) STATIC_IP=$OPTARG ; SIMPLE=1 ;;
			d  ) PPPD_DEBUG=1 ;;
			i  ) DONT_START=1 ;;
			k  ) KEEP=$OPTARG ;;
			\? ) echo -e $FCTSTART_USAGE_MSG ; exit 1 ;;
		esac
	done
}
ping_test_mt() {
	touch /tmp/setvars_ping_mt
	PING_MT_RES=1
	while [ $PING_MT_RES != 0 ]
	do
		sleep 2
		ping_w $1 $2
		PING_MT_RES=$?
	done
	rm -f /tmp/setvars_ping_mt
}
ping_w_maxtimeout() {
	ping_test_mt $1 $2 &
	PID_PING_MT=$!
	kill_ping $PID_PING_MT $2 &
	wait ${PID_PING_MT} 2>/dev/null
	if [ -e /tmp/setvars_ping_mt ] ; then
		rm -f /tmp/setvars_ping_mt
		return 1
	fi
	return 0
}
#STATIC_IP="none"
MIRE=0
TIMEOUT=60
DONT_START=0
PPPD_DEBUG=0
KEEP=0
evalParams $PARAMS


# lock still present? (user forgot to run eagleconfig)
if [ -f $EU_SCRIPT_DIR/lock ] ; then
	exit 5
fi

if [ "$PPPOX" != "none" ] ; then

	# startadsl or startmire?
	if [ $MIRE == 1 ] ; then
		OPTIONS=$PPP_OPTIONS_MIRE
	else
		OPTIONS=$PPP_OPTIONS_ADSL
	fi

	# pppd already lanched?
	if [ $DONT_START == 0 ] && [ ! -z "`pidof pppd`" ] ; then
		exit 1
	fi

fi

# waiting Nsec for synchro (default: N=60)
$EAGLECTRL -s$TIMEOUT 0>/dev/null 1>/dev/null 2>/dev/null

if [ $? != 0 ] ; then
	exit 2
fi

if [ ! -z "`route | grep default`" ] ; then
	route del default
fi

INTERFACE="`$EAGLECTRL -i`"

if [ "$PPPOX" = "none" ] ; then
	# ===== degroup => only ifup =====
	if [ "$STATIC_IP" != "none" ] ; then
			GATEWAY="`echo $STATIC_IP | cut -d '.' -f1-3`.254"
			ifconfig $INTERFACE $STATIC_IP netmask 255.255.255.0
			if [ $? != 0 ] ; then exit 4 ; fi
			route add default gw $GATEWAY
	else
		if [ $USE_IFUPDOWN == 1 ] && [ $SIMPLE == 0 ] ; then
			ifup $INTERFACE 0>/dev/null 1>/dev/null 2>/dev/null
		else
			dhclient $INTERFACE 0>/dev/null 1>/dev/null 2>/dev/null
		fi
		if [ $? != 0 ] ; then exit 4 ; fi
	fi
else
	# ===== not degroup => ifup & pppd =====
	if [ $DONT_START == 1 ] ; then
		RES="`ifconfig | grep "192.168.60.30"`"
		if [ -z "$RES" ] ; then
			# - pppd call adsl => interface is not up yet
			# - startadsl => interface is already up, skip this step
			if [ $USE_IFUPDOWN == 1 ] && [ $SIMPLE == 0 ] ; then
				ifup $INTERFACE 0>/dev/null 1>/dev/null 2>/dev/null
			else
				ifconfig $INTERFACE 192.168.60.30 netmask 255.255.255.0 up
			fi
		fi
		echo "$INTERFACE"
		exit 0
	fi
	if [ $USE_IFUPDOWN == 1 ] && [ $SIMPLE == 0 ] ; then
		ifup $INTERFACE 0>/dev/null 1>/dev/null 2>/dev/null
	else
		ifconfig $INTERFACE 192.168.60.30 netmask 255.255.255.0 up
	fi
	if [ "$DISTRIB" != "Suse" ] ; then
		if [ $PPPD_DEBUG == 0 ] ; then
			pppd file $OPTIONS
		else
			pppd debug file $OPTIONS
		fi
		if [ $? != 0 ] ; then exit 4 ; fi
	else
		if [ $PPPD_DEBUG == 0 ] ; then
			pppd file $OPTIONS >/dev/null &
		else
			pppd debug file $OPTIONS >/dev/null &
		fi
	fi
fi

if [ $KEEP != 0 ] ; then
	ping_w_maxtimeout 213.228.0.42 $KEEP
	exit $?
else
	exit 0
fi
