#!/bin/bash
# Name: fctStartAdsl
# Goal: start ADSL connexion (do not display anything)
# Author: Tux
# Version: 1.9.4
# 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)
# 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

exit 123

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//--timeout/-t}
			param=${param//--ifup/-i}
			param=${param//--ip=/-a}
			param=${param//--ip/-a}
			param=${param//--debug/-d}
			PARAMS="$PARAMS $param"
	fi
	let i++
done
evalParams() {
	while getopts "hmst:a:di" 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 ;;
			\? ) echo -e $FCTSTART_USAGE_MSG ; exit 1 ;;
		esac
	done
}
MIRE=0
TIMEOUT=60
#STATIC_IP="none"
DONT_START=0
PPPD_DEBUG=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
exit 0
