#!/bin/sh
######################################################################
#
# This script is part of the PPP System designed and used by
# Preston A. Elder of Shadow Realm.  If you have any problems
# with this system please email prez@antisocial.com.
#
# This system is publically distributable via. the GNU license.
#
######################################################################
#
# Kill PPP link(s).
#
. /etc/ppp-settings

# Determine the device to be terminated.
if [ -z "$1" ]; then
	echo "SYNTAX : $0 [opt] [ppp-dev [ppp-dev [...]]]"
	echo "EXAMPLE: $0 ppp0 ppp3 ppp5"
	echo "         $0 -A"
	echo
	echo "OPTIONS: -A	Kill ALL ppp processes"
	exit 0
fi

KA=0
DEVLIST=""
while [ -n "$1" ]; do
	if [ "$1" = "-A" -o "$1" = "-a" -o "$1" = "/A" -o "$1" = "/a" ]; then
		KA=1
	else
		DEVLIST="$DEVLIST $1"
	fi
	shift
done
if [ -z "${DEVLIST}" ]; then DEVLIST="`${basePPP}/curdef`"; fi
if [ -z "${DEVLIST}" ]; then DEVLIST="${baseDEV}"; fi

######################################################################
#
# If the ${baseDEV} pid file is present then the program is running. Stop it.

if [ $KA -eq 1 ]; then
	if [ -z "`ps xc | grep ppp | grep -v grep | cut -b1-5`" ]; then
		echo "ERROR: NO PPP links are active"
		exit 1
	else
		> ${basePID}/all.off
		kill `ps xc | grep ppp | grep -v grep | grep -v ppp-off | cut -b1-5`
		rm -f ${basePID}/ppp*
		rm -f ${basePID}/tty*
	fi
else
for DEVICE in $DEVLIST; do
	if [ -r ${basePID}/$DEVICE.dev ]; then
		TTY="`cat ${basePID}/$DEVICE.dev`"
	fi
	if [ -r ${basePID}/$DEVICE.pid ]; then
# If the kill did not work then there is no process running for this
# pid. It may also mean that the lock file will be left. You may wish
# to delete the lock file at the same time.
		> ${basePID}/$DEVICE.off
	        kill `cat ${basePID}/$DEVICE.pid`
	        if [ $? -ne 0 ]; then
	                rm -f ${basePID}/$DEVICE.pid
			rm -f ${basePID}/$DEVICE.dev
			if [ -n "$TTY" ]; then
				rm -f ${basePID}/$TTY.dev
			fi
	                echo "ERROR: Removed stale pid file"
	        fi
#
# Success. Let pppd clean up its own junk.
	        echo "PPP link to $DEVICE terminated."
	else
#
# The ppp process is not running for ${baseDEV}
		echo "ERROR: PPP link is not active on $DEVICE"
	fi
done
fi
