#!/bin/sh
#*********************************************************************
# adsl-connect
#
# Shell script to connect to an ADSL provider using PPPoE
#
# Copyright (C) 2000 Roaring Penguin Software Inc.
#
# This file may be distributed under the terms of the GNU General
# Public License.
#
# Anpassung an ODS-Server mit Kommentaren von Reiner Klaproth, 16.9.2000
#
#   Aufruf dieses Scripts: (Im Gegensatz zum Original!)
#     adsl-connect <user>
#***********************************************************************

# Paths to programs
IFCONFIG=/sbin/ifconfig
PPPD=/usr/sbin/pppd
PPPOE=/usr/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=""

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

. $CONFIG

test -z "$1" || USER="$1"
test -z "$ETH" && ETH=`cat /var/lock/adsl-dev`

# 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

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

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

$IFCONFIG $ETH up mtu 1500

#DEMAND_OPTIONS="demand persist idle $DEMAND 192.168.99.1:192.168.99.2 ipcp-accept-remote ipcp-accept-local connect true noipdefault ktune"
DEMAND_OPTIONS=""

# 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"

$LOGGER -p daemon.notice "Baue ADSL-Verbindung auf"

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

    # 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
