#!/bin/sh
# ../scripts/pppoe-stop.  Generated from pppoe-stop.in by configure.
#***********************************************************************
#
# pppoe-stop
#
# Shell script to bring down a PPPoE connection
#
# Copyright (C) 2000 Roaring Penguin Software Inc.
#
# $Id$
#
# This file may be distributed under the terms of the GNU General
# Public License.
#
# LIC: GPL
#
# Usage: pppoe-stop [config_file]
# If config_file is omitted, defaults to /etc/ppp/pppoe.conf
#
#***********************************************************************

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

ME="`basename $0`"
LOGGER="/usr/bin/logger -t $ME"
CONFIG="$1"
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
. /etc.defaults/rc.subr
. /usr/syno/etc.defaults/iptables_modules_list

PPPOE_PIDFILE="$PIDFILE.pppoe"
PPPD_PIDFILE="$PIDFILE.pppd"
STARTPID="$PIDFILE.start"
BIN_SYNOMODULETOOL=/usr/syno/bin/synomoduletool

# Reverse module list
reverse_modlist() {
	local modules=$1
	local mod
	local ret=""

	for mod in $modules; do
		ret="$mod $ret"
	done

	echo $ret
}

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

# Ignore SIGTERM
trap "" 15

# SYNO: Remove iptables rule for kernel mode
iptables -D PPPOE_FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

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

    # Check if still running
    ps -p $PID > /dev/null 2>&1
    if [ $? != 0 ] ; then
	echo "$ME: The pppoe-connect script (PID $PID) appears to have died" >& 2
    fi

    # Kill pppd, which should in turn kill pppoe
    if [ -r "$PPPD_PIDFILE" ] ; then
	PPPD_PID=`cat "$PPPD_PIDFILE"`
	$LOGGER -p daemon.notice "Killing pppd"
	echo "Killing pppd ($PPPD_PID)"
	kill $PPPD_PID > /dev/null 2>&1 || exit 1
	rm -f "$PPPD_PIDFILE"
    fi

    # Kill pppoe-start
    PIDS=`cat $STARTPID`
    ps -p $PIDS > /dev/null 2>&1
    if [ $? = 0 ] ; then
	$LOGGER -p daemon.notice "Killing pppoe-start"
	kill $PIDS > /dev/null 2>&1
    fi

    # Kill pppoe-connect
    $LOGGER -p daemon.notice "Killing pppoe-connect"
    echo "Killing pppoe-connect ($PID)"
    kill -9 $PID > /dev/null 2>&1

    # Kill pppd again, in case it's still hanging around
    if [ -r "$PPPD_PIDFILE" ] ; then
	sleep 3
	PPPD_PID=`cat "$PPPD_PIDFILE"`
	kill -9 $PPPD_PID > /dev/null 2>&1 || exit 1
	rm -f "$PPPD_PIDFILE"
    fi

    rm -f "$PIDFILE" "$PPPOE_PIDFILE" "$STARTPID"
else
    echo "$ME: No PPPoE connection appears to be running" >&2
	$BIN_SYNOMODULETOOL --rmmod pppoe `reverse_modlist "${PPPOE_MODULES}"`
	$BIN_SYNOMODULETOOL --rmmod pppoe `reverse_modlist "${PPP_MODULES}"`
    exit 1
fi

sleep 5
$BIN_SYNOMODULETOOL --rmmod pppoe `reverse_modlist "${PPPOE_MODULES}"`
$BIN_SYNOMODULETOOL --rmmod pppoe `reverse_modlist "${PPP_MODULES}"`
exit 0
