#!/bin/sh
#
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /etc/sysconfig/network
. /etc/rc.d/init.d/functions
. /lib/rc-scripts/functions.network

CONFIG=$1
source_config

if [ ! -f /var/run/ppp-$DEVNAME.pid ]; then
	# ppp isn't running, or we didn't start it
	exit 0
fi

# set all major variables
setup_ip_param
get_ppp_device_and_pid

if [ -z "$PID" ]; then
	exit 1
fi

rm -f /var/run/ppp-$DEVNAME.pid

[ ! -d /proc/${PID} ] && exit 0

kill -TERM ${PID} > /dev/null 2>&1

sleep 2
[ -d /proc/${PID} ] && sleep 2 && [ -d /proc/${PID} ] && sleep 5

if [ -d /proc/${PID} ]; then
	kill -KILL ${PID} > /dev/null 2>&1
	logger -p daemon.info -t ifdown-ppp "ifdown-ppp unable to nicely kill pppd-$DEVICE (pppd-$DEVNAME)" &
fi

# Special cases
if is_yes "${PPPOA_EAGLE}"; then
	EAGLECTRL="/usr/sbin/adictrl"
	if [ ! -x $EAGLECTRL ]; then
		EAGLECTRL="/usr/sbin/eaglectrl"
	fi
	if [ -x $EAGLECTRL ]; then
		PPPOA_IFACE=$( $EAGLECTRL -i 2>/dev/null)
		[ -n "$PPPOA_IFACE" ] && ip link set "$PPPOA_IFACE" down
	fi
fi

/lib/rc-scripts/ifdown-post $1

exit 1
