#!/bin/sh
######################################################################
#
# 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.
#
######################################################################
#
# Activate PPP link (if $1 specified use $1-data/ppp-on-dialer)
#
. /etc/ppp-settings

rm -f ${basePID}/all.off

${basePPP}/chk4net
if [ $? -eq 0 ]; then exit 0; fi

if [ -z "$2" -o "$2" = "-" ]; then
	TTY=${baseTTY}	# The default device of the modem to dial with
else
	TTY="$2"
fi

if [ -z "$1" -o "$1" = "-" ]; then
	if [ -r ${basePPP}/cycle.$TTY ]; then
		ISP=`head -1 ${basePPP}/cycle.$TTY`
		len=`expr length $ISP`
		if [ "`expr $len \> 1`" = 0 ]; then
			ISP=${baseISP}
		fi
		unset len
	else
		ISP=${baseISP}
	fi
else
	ISP="$1"
fi

if [ -z "$3" -o "$3" = "-" ]; then
	LINESPEED=${baseSPEED}
else
	LINESPEED=$3
fi

if [ -r ${basePPP}/${ISP}-data -a -r ${basePPP}/${ISP}-ppp-on-dialer ]; then
	. ${basePPP}/${ISP}-data
else
	echo "An error has occured reading your ISP data files."
	echo "Please ensure that ${ISP}-data and ${ISP}-ppp-on-dialer"
	echo "files exist and are readable in ${basePPP}."
	exit 1
fi

#
# Export them so that they will be available at 'ppp-on-dialer' time.
export TELEPHONE ACCOUNT PASSWORD ISP CHATOPT
# 
# This is the location of the script which dials the phone and logs
# in.  Please use the absolute file name as the $PATH variable is not
# used on the connect option.  (To do so on a 'root' account would be
# a security hole so don't ask.)
#
DIALER_SCRIPT=${basePPP}/${ISP}-ppp-on-dialer
#
# Initiate the connection
# 
# I put most of the common options on this command. Please, don't
# forget the 'lock' option or some programs such as mgetty will not
# work. The asyncmap and escape will permit the PPP link to work with
# a telnet or rlogin connection. You are welcome to make any changes
# as desired. Don't use the 'defaultroute' option if you currently
# have a default route to an ethernet gateway.
#

if [ "$TTY" = "$baseTTY" ]; then
	if [ -n "`${basePPP}/curdef`" ]; then
		route del default
	fi
fi
$basePPP/chkup $ISP $TTY &
if [ -z "`${basePPP}/curdef`" ]; then
	if [ -n "$CHAP" ]; then
		exec /usr/sbin/pppd $DEBUG /dev/$TTY $LINESPEED $PPPOPT \
		     $LOCAL_IP:$REMOTE_IP netmask $NETMASK defaultroute \
		     ipparam $ISP name $CHAP connect $DIALER_SCRIPT &
	elif [ -n "$PAP" ]; then
		exec /usr/sbin/pppd $DEBUG /dev/$TTY $LINESPEED $PPPOPT \
		     $LOCAL_IP:$REMOTE_IP netmask $NETMASK defaultroute \
		     ipparam $ISP name $PAP connect $DIALER_SCRIPT &
	else
		exec /usr/sbin/pppd $DEBUG /dev/$TTY $LINESPEED $PPPOPT \
		     $LOCAL_IP:$REMOTE_IP netmask $NETMASK defaultroute \
		     ipparam $ISP connect $DIALER_SCRIPT &
	fi
else
	if [ -n "$CHAP" ]; then
		exec /usr/sbin/pppd $DEBUG /dev/$TTY $LINESPEED $PPPOPT \
		     $LOCAL_IP:$REMOTE_IP netmask $NETMASK \
		     ipparam $ISP name $CHAP connect $DIALER_SCRIPT &
	elif [ -n "$PAP" ]; then
		exec /usr/sbin/pppd $DEBUG /dev/$TTY $LINESPEED $PPPOPT \
		     $LOCAL_IP:$REMOTE_IP netmask $NETMASK \
		     ipparam $ISP name $PAP connect $DIALER_SCRIPT &
	else
		exec /usr/sbin/pppd $DEBUG /dev/$TTY $LINESPEED $PPPOPT \
		     $LOCAL_IP:$REMOTE_IP netmask $NETMASK \
		     ipparam $ISP connect $DIALER_SCRIPT &
	fi
fi
