#!/bin/sh

CONFIG=/etc/ppp/pppoe.conf

if [ ! -f "$CONFIG" -o ! -r "$CONFIG" ] ; then
    echo "$0: Cannot read configuration file '$CONFIG'" >& 2
    exit 1
fi
export CONFIG
. $CONFIG

. /usr/bin/scriptlib

PIDFILE_START="$PIDFILE.start"
PIDFILE_CONNECT="$PIDFILE.connect"
PIDFILE_PPPD="/var/run/ppp-pppoe.pid"


# Backward config file compatibility
if test "$DEMAND" = "" ; then
	DEMAND=no
fi

# Ignore SIGTERM
trap "" 15

# Check for pidfile
if [ -r "$PIDFILE_CONNECT" ] ; then
    PID=`cat $PIDFILE_CONNECT`
    # Check if still running
    kill -0 $PID > /dev/null 2>&1
    if [ $? != 0 ] ; then
		echo "$0: The pppoe-connect script (PID $PID) appears to have died" >& 2
	else
		# Kill pppoe-connect
	    echo "Killing pppoe-connect ($PID)"
	    kill $PID > /dev/null 2>&1
    fi
    fi



    # Kill pppd, which should in turn kill pppoe
    if [ -r "$PIDFILE_PPPD" ] ; then
		sl_get_ppp_PID_IFname $PIDFILE_PPPD PPPD_PID PPPD_IF
		echo "Killing pppd ($PPPD_PID)"
		kill $PPPD_PID > /dev/null 2>&1 || exit 1
	fi	

    # Kill pppoe-start
if [ -r $PIDFILE_START ] ; then
    PIDS=`cat $PIDFILE_START`
    kill -0 $PIDS > /dev/null 2>&1
    if [ $? = 0 ] ; then
		kill $PIDS > /dev/null 2>&1
    fi
fi

if [ -r "$PIDFILE_CONNECT" ] ; then
    # Kill pppoe-connect
    echo "Killing pppoe-connect ($PID)"
    kill -9 $PID > /dev/null 2>&1
fi


    # Kill pppd again, in case it's still hanging around
    if [ -r "$PIDFILE_PPPD" ] ; then
    	sl_get_ppp_PID_IFname $PIDFILE_PPPD PPPD_PID PPPD_IF
		kill -9 $PPPD_PID > /dev/null 2>&1 
    fi

    rm -f "$PIDFILE_START" "$PIDFILE_CONNECT" "$PIDFILE_PPPD"
exit 0
