#!/bin/sh
#
#	$Id: ifdown-sl 10092 2009-01-23 13:39:27Z glen $
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin

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

CONFIG=$1
source_config

# set all major variables
setup_ip_param

# signals ifup-sl not to persist
rm -f /var/run/sl-$DEVICE.dev

# we can use dip or direct slip connection via slattach
if is_no "$DIRECT_CONNECT"; then
	PID=$(pidof dip-$DEVICE)
else
	PID=$(pidof slattach)
	for x in $PID; do
		if grep -q "$MODEMPORT" /proc/$x/cmdline; then
			logger -p daemon.info -t ifdown-sl \
				"signal TERM send to \"$(ps -o cmd= $x)\" - this should be slattach"
			kill -TERM $x
			PID=$x
		fi
	done
fi
#
# The proctitle for connected dip daemons is actually "-dip (ipaddr)"
#	with ipaddr = local on dial-out,  remote on dial-in
# Grab the PID of connected dial-out daemon.
#
if [ -z "$PID" ]; then
	PID=$(pidof -- -dip\ \($IPADDR\))
fi
if [ -z "$PID" ]; then
	exit 1
fi

kill $PID > /dev/null 2>&1
if [ ! -d /proc/$PID ]; then
	/etc/sysconfig/network-scripts/ifdown-post $1
	exit 0
fi
sleep 2
if [ ! -d /proc/$PID ]; then
	/etc/sysconfig/network-scripts/ifdown-post $1
	exit 0
fi

kill -KILL $PID > /dev/null 2>&1
if [ -d /proc/$PID ]; then
	logger -p daemon.info -t ifdown-sl "ifdown-sl unable to kill sl-$DEVICE" &
else
	/etc/sysconfig/network-scripts/ifdown-post $1
fi

exit 1
