#!/bin/sh
# ../scripts/pppoe-linkdown.  
#***********************************************************************

# Set to "C" locale so we can parse messages from commands
LANG=C
export LANG

ME="`basename $0`"
LOGGER="/usr/bin/logger -t $ME"
CGI_FILE=/tmp/pppoe_cgi_status
STS_FILE=/tmp/pppoe_connect_status
PRE_MODE=""
CONFIG=""

if [ $# -ne 0 ]; then

	if [ "$1" = "pre" ] ; then
		PRE_MODE=$1
	else
		CONFIG="$1"
	fi
fi


if [ "$CONFIG" = "" ] ; then
	CONFIG=/etc/ppp/pppoe.conf
fi

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

PPPOE_PIDFILE="$PIDFILE.pppoe"
PPPD_PIDFILE="$PIDFILE.pppd"
STARTPID="$PIDFILE.start"

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

# Ignore SIGTERM
trap "" 15

# Check for pidfile
if [ -r "$PIDFILE" ] ; then
	PID=`cat $PIDFILE`

	# Kill pppd
	if [ -r "$PPPD_PIDFILE" ] ; then
		PPPD_PID=`cat "$PPPD_PIDFILE"`
		# Check if still running
		kill -0 $PPPD_PID > /dev/null 2>&1
		if [ $? = 0 ] ; then
			echo "Killing pppd ($PPPD_PID)"
			kill $PPPD_PID > /dev/null 2>&1
		else
			echo "pppd not running ($PPPD_PID)"
		fi
	fi

	# Kill pppoe_cli
	if [ -r "$PPPOE_PIDFILE" ] ; then
		PPPOE_PID=`cat "$PPPOE_PIDFILE"`
		# Check if still running
		kill -0 $PPPOE_PID > /dev/null 2>&1
		if [ $? = 0 ] ; then
			echo "Killing pppoe_cli ($PPPOE_PID)"
			kill $PPPOE_PID > /dev/null 2>&1 || exit 1
		else
			echo "pppoe_cli not running ($PPPOE_PID)"
		fi
	fi

	# Kill pppoe-start
	if [ -r "$STARTPID" ] ; then
		PIDS=`cat $STARTPID`
		# Check if still running
		kill -0 $PIDS > /dev/null 2>&1
		if [ $? = 0 ] ; then
			echo "Killing pppoe_start ($PIDS)"
			kill $PIDS > /dev/null 2>&1
		else
			echo "pppoe-start not running ($PIDS)"
		fi
	fi

	# Kill pppoe-connect
	# Check if still running
	kill -0 $PID > /dev/null 2>&1
	if [ $? = 0 ] ; then
		echo "Killing pppoe-connect ($PID)"
		kill  $PID > /dev/null 2>&1
	else
		echo "pppoe-connect not running ($PID)"
	fi

	# Kill pppd again
	if [ -r "$PPPD_PIDFILE" ] ; then
		PPPD_PID=`cat "$PPPD_PIDFILE"`
		# Check if still running
		kill -0 $PPPD_PID > /dev/null 2>&1
		if [ $? = 0 ] ; then
			echo "Killing pppd -9 ($PPPD_PID)"
			kill -9 $PPPD_PID > /dev/null 2>&1
		else
			echo "pppd not running ($PPPD_PID)"
		fi
	fi

	# Kill pppoe_cli again
	if [ -r "$PPPOE_PIDFILE" ] ; then
		PPPOE_PID=`cat "$PPPOE_PIDFILE"`
		# Check if still running
		kill -0 $PPPOE_PID > /dev/null 2>&1
		if [ $? = 0 ] ; then
			echo "Killing pppoe_cli -9 ($PPPOE_PID)"
			kill -9 $PPPOE_PID > /dev/null 2>&1 || exit 1
		else
			echo "pppoe_cli not running ($PPPOE_PID)"
		fi
	fi

	# Kill pppoe-start again
	if [ -r "$STARTPID" ] ; then
		PIDS=`cat $STARTPID`
		# Check if still running
		kill -0 $PIDS > /dev/null 2>&1
		if [ $? = 0 ] ; then
			echo "Killing pppoe_start -9 ($PIDS)"
			kill -9 $PIDS > /dev/null 2>&1
		else
			echo "pppoe-start not running ($PIDS)"
		fi
	fi

	# Kill pppoe-connect again
	# Check if still running
	kill -0 $PID > /dev/null 2>&1
	if [ $? = 0 ] ; then
		echo "Killing pppoe-connect -9 ($PID)"
		kill -9 $PID > /dev/null 2>&1
	else
		echo "pppoe-connect not running ($PID)"
	fi

	rm -f "$PIDFILE" "$PPPD_PIDFILE" "$PPPOE_PIDFILE" "$STARTPID"

	### CGI status reset
	if [ "$PRE_MODE" != "pre" ] ; then
		# file lock
		exec 9>>$CGI_FILE
		flock 9
		if [ $? -eq 0 ] ; then
			echo  "CONNECT_DONE" > $CGI_FILE
			# file unlock
			flock -u 9
		fi
	fi

else
	exit 1
fi

#### to sysmgr
/usr/sbin/pppmgr OTHER_ERR

### PPPOE status reset
# file lock
exec 9>>$STS_FILE
flock 9
if [ $? -eq 0 ] ; then
	echo "OTHER_ERR" > $STS_FILE

	# file unlock
	flock -u 9
fi

### send USR1 signal to pppmgr reconnect
PPPMGR_PID=`ps ax | grep pppmgr | grep reconnect | awk -F" " '{ print $1 }'`
kill -USR1 $PPPMGR_PID


exit 0
