#!/bin/sh
#	ip-check
#
#	ip-up calls this script every 4 minutes (by default, change $chktime
#	if needed) to ping the gateway and restart the connecting process if
#	no reply is received ; it also logs what it does to $logfile (which
#	may of course be a console)
#
#							Olivier Mehani, 2002
#							   <shtrom@ssji.net>
#
#	$Date: 2002/11/10 21:10:47 $
#	$Revision: 1.5 $
#

gw=`route -n | grep ppp | head -n 1 | sed -e s/" .*"/""/` #GW's IP

chktime=240	#ping frequency (in fact ping period in seconds, but who cares ?)
killwait=10	#time to wait after kill before restarting the pppd

logfile=/var/log/ip-check

uptime=`date`
echo "Connection established (GW: $gw) $uptime" | tee -a $logfile
if [ -x ###PPPDIR###/routing-up ]; then
    echo Running the routing-up script...
    ###PPPDIR###/routing-up
fi
echo -n "Now pinging the other end every $chktime seconds"
ping -c4 $gw  > /dev/null 2>&1
while [ $? == 0 ]; do
    echo -n .
    sleep $chktime
    ping -c4 $gw > /dev/null 2>&1
done

echo
losttime=`date`
echo "Connection lost $losttime !" | tee -a $logfile
if [ -x ###PPPDIR###/routing-down ]; then
    echo Running the routing-down script...
    ###PPPDIR###/routing-down
fi
echo "Killing pppd..."
killall -9 pppd > /dev/null 2>&1
rm /var/run/pppd.pid 2&1 >/dev/null
killall -9 ###PPPOA### > /dev/null 2>&1
rm /var/run/###PPPOA###.pid 2&1 >/dev/null
if [ -x ###PPPDIR###/araw-down ]; then
    echo Running the araw-down script...
    ###PPPDIR###/araw-down
fi
echo "Waiting $killwait seconds to be sure..."
sleep $killwait
###PPPDIR###/ppp-go
