#!/bin/sh
#***********************************************************************
#
# adsl-status
#
# Shell script to report on status of ADSL connection
#
# Copyright (C) 2000 Roaring Penguin Software Inc.
#
# $Id: adsl-status,v 1.3 2000/03/16 03:29:26 dfs Exp $
#
# This file may be distributed under the terms of the GNU General
# Public License.
#
# Usage: adsl-status [config_file]
# If config_file is omitted, defaults to /etc/ppp/pppoe.conf
#
#***********************************************************************

# Defaults
CONFIG=/etc/ppp/pppoe.conf
case "$#" in
    1)
	CONFIG="$1"
	;;
esac

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

. $CONFIG

# If no PIDFILE, connection is down
if [ ! -r $PIDFILE ] ; then
    echo "adsl-status: Link is down (can't read PID file $PIDFILE)"
    exit 1
fi

# If PIDFILE, get PPID
PID=`cat $PIDFILE`
PARENT="`cat /proc/$PID/status | grep '^PPid' | cut -f2`"
if [ "$PARENT" = "" ] ; then
    echo "adsl-status: Link is down (can't get parent process-ID of pppoe process $PID)"
    exit 1
fi


# Search the /var/run/pppn.pid files
for i in /var/run/ppp*.pid ; do
    if [ -r $i ] ; then
	PID=`cat $i`
	if [ "$PID" = "$PARENT" ] ; then
	    IF=`basename $i .pid`
	    /sbin/ifconfig $IF | grep "UP POINTOPOINT" > /dev/null
	    if [ "$?" != "0" ] ; then
		echo "adsl-status: Link is attached to $IF, but $IF is down"
		exit 1
	    fi
	    echo "adsl-status: Link is up and running on interface $IF"
	    /sbin/ifconfig $IF
	    exit 0
	fi
    fi
done

echo "adsl-status: Link is down -- could not find interface corresponding to"
echo "pppd pid $PARENT"
exit 1