#!/bin/sh
# ************************************************************************
# *                                                                      *
# *      makeconfig v0.1 - ECI Linux driver configuration script         *
# *                        by FlashCode and Crevetor (c) 14/04/2002      *
# *                                                                      *
# *          For any support, contact one of us :                        *
# *           - FlashCode: flashcode@free.fr  http://flashcode.free.fr   *
# *           - Crevetor : ziva@caramail.com                             *
# *                                                                      *
# *        Parameters for this script :                                  *
# *          $1 = username + domain (username@domain)                    *
# *          $2 = password (given by provider)                           *
# *          $3 = path to pppoeci (eg: /usr/local/bin/pppoeci)           *
# *          $4 = DNS 1 (depends on provider, 0=no DNS setup)            *
# *          $5 = DNS 2 (depends on provider)                            *
# *          $6 = VPI (depends on provider)                              *
# *          $7 = VCI (depends on provider)                              *
# *                                                                      *
# ************************************************************************

#
# target "etc" directory (useful for testing this script)
#

base_etc=/etc

#
# check parameters :
#

if [ $UID -ne 0 ]; then
    echo -e "Error: you must be root in order to run this script.\n"
    exit 255
fi

if [ $# -ne 7 ]; then
    echo -e "Error: invalid number of parameters."
    echo -e "Syntax:\n  makeconfig <username> <password> <path_to_pppoeci> <dns1> <dns2> <vpi> <vci>"
    exit 1
fi

pppoeci=$3
if [ ! -f ${pppoeci} -o ! -x ${pppoeci} ]; then
    echo "Error: ${pppoeci} is not a valid executable"
    exit 1
fi

#
# backup and create resolv.conf
#

if [ $4 != 0 ]; then
    nomresolv=${base_etc}/resolv.conf
    if [ -s $nomresolv ]; then
        nombak="${nomresolv}.bak"
        numero=0
        while [ -s $nombak ]
        do
            numero=`expr $numero + 1`
            nombak="${nomresolv}.bak$numero"
        done
        echo -n "Backing up $nomresolv (to $nombak)... "
        cp -f $nomresolv $nombak
        echo "ok"
    fi
    echo -n "Creating $nomresolv..."
    echo "nameserver $4" >$nomresolv
    echo "nameserver $5" >>$nomresolv
    echo "ok"
fi

#
# create or backup/modify "chap-secrets" and "pap-secrets"
#

for auth in "chap" "pap"
{
    nomsecret=${base_etc}/ppp/${auth}-secrets
    if [ -s $nomsecret ]; then
        nombak="${nomsecret}.bak"
        numero=0
        while [ -s $nombak ]
        do
            numero=`expr $numero + 1`
            nombak="${nomsecret}.bak$numero"
        done
        echo -n "Backing up $nomsecret (to $nombak)... "
        cp -f $nomsecret $nombak
        echo "ok"
        echo -n "Modifying $nomsecret... "
        echo -e "\"$1\"\t*\t\"$2\"\t*\t" >>$nomsecret
        echo "ok"
    else
        echo -n "Creating $nomsecret... "
        echo "# Secrets for authentication using $auth" >$nomsecret
        echo -e "\"$1\"\t*\t\"$2\"\t*\t" >>$nomsecret
        echo "ok"
    fi
}

#
# create or backup/modify "adsl" script
#

nomadsl=${base_etc}/ppp/peers/adsl
if [ -s $nomadsl ]; then
    nombak="${nomadsl}.bak"
    numero=0
    while [ -s $nombak ]
    do
        numero=`expr $numero + 1`
        nombak="${nomadsl}.bak$numero"
    done
    echo -n "Backing up $nomadsl (to $nombak)... "
    cp -f $nomadsl $nombak
    echo "ok"
    echo -n "Modifying $nomadsl... "
    sed s"^pty \".*""pty \"$3 -vpi $6 -vci $7\"" $nomadsl | sed s"^user \".*""user \"$1\"" >$nomadsl
    echo "ok"
else
    echo -n "Creating $nomadsl... "
    cat <<EOF >$nomadsl
# 12/04/2001 Benoit PAPILLAULT <benoit.papillault@free.fr>
# 08/05/2001 Updated. Added "novj" & removed "kdebug 7"
# 07/02/2002 Replace "maxfail 0" by "maxfail 10"
# 29/04/2002 Added the option "linkname" to easily locate the running pppd
#
# This file could be rename but its place is under /etc/ppp/peers
# To connect to Internet using this configuration file, type
# pppd call adsl, where "adsl" stands for the name of this file

debug
kdebug 1
noipdefault
defaultroute
pty "$3 -vpi $6 -vci $7"
sync
user "$1"
noaccomp
nopcomp
noccp
novj
holdoff 10

# This will store the pid of pppd in the first line of /var/run/ppp-eciadsl.pid
# and the interface created (like ppp0) on the second line.
linkname eciadsl

# maxfail is the number of times pppd retries to execute pppoeci after
# an error. If you put 0, pppd retries forever, filling up the process table
# and thus, making the computer unusable.
maxfail 10

usepeerdns
noauth

# If your PPP peer answer to LCP EchoReq (lcp-echo requests), you can
# use the lcp-echo-failure to detect disconnected links with:
#
# lcp-echo-interval 600
# lcp-echo-failure 10
#
# However, if your PPP peer DOES NOT answer to lcp-echo request, you MUST
# desactivate this feature with the folowing line
#
lcp-echo-interval 0

# You may need the following, but ONLY as a workaround
# mtu 1432
EOF
    echo "ok"
fi
