#!/bin/bash
######################################################################
#
# This script is part of the PPP System designed and used by
# Preston A. Elder of Shadow Realm.  If you have any problems
# with this system please email prez@antisocial.com.
#
# This system is publically distributable via. the GNU license.
#
######################################################################
#
# Make sure PPP link started (should be called by ppp-on)
#
. /etc/ppp-settings

if [ -z "$1" -o "$1" = "-" ]
then	ISP="-"
else	ISP=$1
fi
if [ -z "$2" ]
then	DEVICE="${baseTTY}"
else	DEVICE=$2
fi

# Check for stale (or non-existant) lockfile
if [ ! -f ${basePID}/chkup.$DEVICE -o \
     -n "`find ${basePID} -name chkup.$DEVICE -mmin +5`" ]; then
	> ${basePID}/chkup.$DEVICE
	sleep 90

	rm -f ${basePID}/chkup.$DEVICE
	if [ -f ${basePID}/$dev.off ]; then
		exit 1
	fi

 	if [ -r ${basePID}/$DEVICE.dev ]; then
		dev=`cat ${basePID}/$DEVICE.dev`
		if [ -r ${basePID}/$dev.pid ]; then
			pid=`cat ${basePID}/$dev.pid`
			if [ -z "`ps x | grep $pid | grep -v grep | grep pppd`" ]; then
				if [ $MAXDIAL -gt 0 ]; then
					${basePPP}/ppp-retry $ISP $DEVICE
				else
					${basePPP}/ppp-cycle $ISP $DEVICE
				fi
			fi
		else
			if [ $MAXDIAL -gt 0 ]; then
					${basePPP}/ppp-retry $ISP $DEVICE
			else
					${basePPP}/ppp-cycle $ISP $DEVICE
			fi
		fi
	else
		if [ $MAXDIAL -gt 0 ]; then
			${basePPP}/ppp-retry $ISP $DEVICE
		else
			${basePPP}/ppp-cycle $ISP $DEVICE
		fi
	fi
fi
