#!/bin/sh

echo "PPP connection closed" > /dev/console
echo "RUN: .$0 '$1' '$2' '$3' '$4' '$5' '$6' '$7'" > /dev/console

#ipaddr=`ifconfig $1 | grep "inet addr" | cut -d' ' -f 12| cut -d':' -f 2`
#mask=`ifconfig $1 | grep "inet addr" | cut -d' ' -f 16| cut -d':' -f 2`
#ipnm=`convert_ipmask $ipaddr $mask`
#/usr/sbin/ip route del $ipnm dev $1 table main

INTERNET_SERVICE_IFACE=`grep "INTERNET_SERVICE_IFACE"  /tmp/net_variables | cut -d"=" -f2`
SERVICE="$7"
echo "INTERNET_SERVICE_IFACE=$INTERNET_SERVICE_IFACE" > /dev/console

if [ "$INTERNET_SERVICE_IFACE" == "ppp0" ] ; then 
    echo "Service 'Internet'" > /dev/console
    /sbin/ledctl red green
fi

echo "6 = $6"
if [ "$6" == "pppoe" ]; then
    is_ipv6=`grep "PPPOE_DUAL_STACK_$SERVICE=" /tmp/net_variables | cut -d"=" -f2`
    echo "is_ipv6 = $is_ipv6"
    if [ "$is_ipv6" == "yes" ]; then
    	pid=`ps | grep "dibbler-client run -i ppp0" | grep -o "[0-9]*. " | grep -v "grep" | head -n1`
    	echo "kill dhcpv6-client"
    	if [ -n "$pid" ]; then
    		kill $pid
    	fi
        killall rdnssd
    fi
fi

[ -z "$6" ] || env -i ACTION="ifdown" INTERFACE="$6" DEVICE="$1" PROTO=ppp /sbin/hotplug-call "iface"

[ -f /tmp/.need_reset_usb_modem ] && {
    /sbin/reset_modem
}

[ -d /etc/ppp/ip-down.d ] && {
	for SCRIPT in /etc/ppp/ip-down.d/*
	do
		[ -x "$SCRIPT" ] && "$SCRIPT" $@
	done
}

echo "$0 done." > /dev/console

exit 0
