#!/bin/bash
# Name: fctStopAdsl
# Goal: stop ADSL connexion (do not display anything)
# Author: Tux
# Version: 1.9.4
# Params:
#	h = display help
#	f = kill pppd even if PPPOX=none
#	s = simple mode (don't use ifup & ifdown scripts => do not requiere ifcfg-ethX)

if [ 1 == 1 ] ; then
	# when called from sources folder (when uninstalling)
	USE_IFUPDOWN=0
	PPPOX="pppoa"
	DELAY_KILL_PPPD=3
	i=1
	PARAMS=""
else
	# when called from /usr/local/sbin
	exit 123
fi

for param in $* ; do
	if [ $i -gt 0 ] ; then
			param=${param//--help/-h}
			param=${param//--force/-f}
			param=${param//--simple/-s}
			PARAMS="$PARAMS $param"
	fi
	let i++
done
evalParams() {
	while getopts "hfs" opt; do
		case $opt in
			h  ) echo -e $FCTSTOP_USAGE_MSG ; exit 0 ;;
			f  ) KILLPPP=1 ;;
			s  ) SIMPLE=1 ;;
			\? ) echo -e $FCTSTOP_USAGE_MSG ; exit 1 ;;
		esac
	done
}
if [ "$PPPOX" != "none" ] ; then
	KILLPPP=1
else
	KILLPPP=0
fi
evalParams $PARAMS


if [ $KILLPPP == 1 ] ; then
	# kill pppd (let Nsec after killall to stop properly)
	if [ ! -z "`pidof pppd`" ] ; then
		killall -q pppd
		sleep $DELAY_KILL_PPPD
		if [ ! -z "`pidof pppd`" ] ; then
			logger "2e tape stopadsl"
			sleep 2
			killall -9 -q pppd
		fi
	fi
fi

# if the modem is still plugged, modem interface is putted down
# if the modem is unplugged, hotplug should already have done that => do nothing
INTERFACE=`eaglectrl -i 2>/dev/null`
if [ $? == 0 ] ; then
	if [ $USE_IFUPDOWN == 1 ] && [ $SIMPLE == 0 ] ; then
		ifdown $INTERFACE
	else
		ifconfig $INTERFACE down
	fi
fi
