#!/bin/sh
#----------------------------------------------------------------------------
# /etc/ppp/ip-down - called whenever a (i)ppp connection has been come down
#
# Creation:        29.03.2000  fm
# Last Update:     $Id: ip-down 10994 2006-02-15 11:58:37Z jw5 $
#----------------------------------------------------------------------------
export PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin

real_interface=$1
interface=$1
tty=$2
speed=$3
local=$4
remote=$5

run_ip_down ()
{
    case $interface in
    ppp0)           # it's DSL, map interface
        case $tty in
        "" | eth*| br* | /dev/ttyp0 | /dev/ttyS[0-4] | /dev/ttyUSB0 | /dev/capi/0 | /dev/ttyACM0 )
            # empty: PPPoE, eth[0-9] in_kernel, else PPTP
            interface=pppoe
            rm -f /var/run/pppoe.up

            if [ -f /var/run/imond.pid ]
            then
                /usr/local/bin/imond-stat "pppoe: down"
            fi
        ;;
        esac
    ;;
    esac

    rm -f /var/run/online /var/run/$interface.ip

    # default route existing or last default route interface going down?
    if [ -f /etc/default-route-interface ]
    then
	default_route_interface=`/bin/cat /etc/default-route-interface`
    elif [ -f /etc/last-default-route-interface ]
    then
	default_route_interface=`/bin/cat /etc/last-default-route-interface`
	rm -f /etc/last-default-route-interface
    else
	default_route_interface=`/sbin/route -n | grep '^0.0.0.0' | sed 's/.* //g'`
	case $default_route_interface in
	    ppp0) default_route_interface=pppoe ;;
	esac
    fi

    if [ "$default_route_interface" = "$interface" ]
    then
        is_default_route='yes'
    else
        is_default_route='no'
    fi

    # Call all custom ip-downs
    for j in /etc/ppp/ip-down[0-9][0-9][0-9].*
    do
        if [ -f $j ]
        then
             . $j
        fi
    done
}

if [ -f /etc/syslog.conf ]		# Log to syslog if available...
then
  run_ip_down >/tmp/$$.tmp 2>&1
  logger -t ip-down </tmp/$$.tmp
  rm /tmp/$$.tmp
else
  run_ip_down >/dev/console 2>&1
fi

exit 0
