#!/bin/sh
# Generated automatically from adsl-connect.in by configure.
#***********************************************************************
#
# adsl-connect
#
# Shell script to connect to an ADSL provider using PPPoE
#
# Copyright (C) 2000 Roaring Penguin Software Inc.
#
# $Id: adsl-connect.in,v 1.20 2000/05/08 01:00:49 dfs Exp $
#
# This file may be distributed under the terms of the GNU General
# Public License.
#
# Usage: adsl-connect [config_file]
#        adsl-connect interface user [config_file]
# Second form overrides USER and ETH from config file.
# If config_file is omitted, defaults to /etc/ppp/pppoe.conf
#
#***********************************************************************

# From AUTOCONF
prefix=/usr
exec_prefix=${prefix}

# Paths to programs
IFCONFIG=/sbin/ifconfig
PPPD=/usr/sbin/pppd
PPPOE=${exec_prefix}/sbin/pppoe
LOGGER=/usr/bin/logger

# Must be root
if test "`id -u`" != 0 ; then
    echo "$0: You must be root to run this script" >& 2
    exit 1
fi

CONFIG=/etc/ppp/pppoe.conf
USER=""
ETH=""

# Sort out command-line arguments
case "$#" in
    1)
	CONFIG="$1"
	;;
    3)
	CONFIG="$3"
	;;
esac

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

. $CONFIG

# Check for command-line overriding of ETH and USER
case "$#" in
    2|3)
	ETH="$1"
	USER="$2"
	;;
esac

# Check that config file is sane
if test "$USER" = "" ; then
    echo "$0: Check '$CONFIG' -- no setting for USER" >& 2
    exit 1
fi
if test "$ETH" = "" ; then
    echo "$0: Check '$CONFIG' -- no setting for ETH" >& 2
    exit 1
fi

# Catch common error
if test "$DEBUG" = "1" ; then
    echo "*** If you want to use DEBUG, invoke adsl-start, not adsl-connect."
    exit 1
fi

if test "$DEBUG" != "" ; then
    echo "ETH=$ETH; USER=$USER" >> $DEBUG
    echo "---------------------------------------------" >> $DEBUG
fi

# Remove any old terminate files
rm -f "$TERMINATEFILE"

# MTU of Ethernet card attached to modem MUST be 1500.

$IFCONFIG $ETH up mtu 1500

if test "$SYNCHRONOUS" = "yes" ; then
	PPPOE_SYNC=-s
	PPPD_SYNC=sync
else
	PPPOE_SYNC=""
	PPPD_SYNC=""
fi

if test "$CLAMPMSS" = "no" ; then
	CLAMPMSS=""
else
	CLAMPMSS="-m $CLAMPMSS"
fi

# Backward config file compatibility
if test "$DEMAND" = "" ; then
	DEMAND=no
fi

if test "$DEMAND" = "no" ; then
	DEMAND=""
else
	DEMAND="demand persist idle $DEMAND 192.168.99.1:192.168.99.2 ipcp-accept-remote ipcp-accept-local connect true noipdefault ktune"
fi

case "$FIREWALL" in
    STANDALONE)
	. /etc/ppp/firewall-standalone
	;;
    MASQUERADE)
	. /etc/ppp/firewall-masq
	;;
esac

# Standard PPP options we always use
PPP_STD_OPTIONS="ipparam 5 noipdefault noauth defaultroute hide-password nodetach usepeerdns local mtu 1492 mru 1492 noaccomp noccp nobsdcomp nodeflate nopcomp novj novjccomp user $USER lcp-echo-interval $LCP_INTERVAL lcp-echo-failure $LCP_FAILURE"

# PPPoE invocation
PPPOE_CMD="$PPPOE -p $PIDFILE -I $ETH -T $PPPOE_TIMEOUT $PPPOE_SYNC $CLAMPMSS"
if test "$DEBUG" != "" ; then
    if test "$DEMAND" != "" ; then
	echo "(Turning off DEMAND for debugging purposes)"
	DEMAND=""
    fi
    echo "pppd invocation" >> $DEBUG
    echo "$PPPD pty '$PPPOE_CMD' $PPP_STD_OPTIONS $PPPD_SYNC debug" >> $DEBUG
    echo "---------------------------------------------" >> $DEBUG
    $PPPD pty "$PPPOE_CMD -D $DEBUG-0" \
	$PPP_STD_OPTIONS \
	$PPPD_SYNC \
	debug >> $DEBUG 2>&1
    echo "---------------------------------------------" >> $DEBUG

    echo "Extract from /var/log/messages" >> $DEBUG
    grep 'ppp' /var/log/messages | tail -150 >> $DEBUG
    date >> $DEBUG
    echo "---------------------------------------------" >> $DEBUG
    echo "rp-pppoe debugging dump" >> $DEBUG
    cat $DEBUG-0 >> $DEBUG
    rm -f $DEBUG-0
    for i in 1 2 3 4 5 6 7 8 9 10 ; do
    echo ""
    echo ""
    echo ""
    done
    echo "*** Finished debugging run.  Please review the file"
    echo "*** '$DEBUG' and make sure it does not contain"
    echo "*** any sensitive information.  Once you are satisfied with it,"
    echo "*** e-mail it to 'pppoe@roaringpenguin.com' for diagnosis."
    echo ""
    echo "*** BUT BEFORE YOU DO THIS, make sure you have read the HOW-TO-CONNECT"
    echo "*** file which came with the rp-pppoe package."
    exit 0
fi

while [ true ] ; do
    $PPPD pty "$PPPOE_CMD" \
	$PPP_STD_OPTIONS \
	$DEMAND \
	$PPPD_SYNC

    # If there's a terminate file, exit
    if test -f "$TERMINATEFILE" ; then
	$LOGGER -p daemon.notice "ADSL connection terminated by user"
	rm -f "$TERMINATEFILE" "$PIDFILE"
	exit 0
    fi

    # Otherwise, re-establish the connection
    $LOGGER -p daemon.notice \
        "ADSL connection lost; attempting re-connection."
done
