#!/bin/sh
# ************************************************************************
# *                                                                      *
# *      makeconfig v0.6 - 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 or "" = skipped)           *
# *        5  = DNS 2 (depends on provider, 0 or "" = skipped)           *
# *        6  = VPI (depends on provider)                                *
# *        7  = VCI (depends on provider)                                *
# *        8  = VID/PID before synchro (example: 05472131 for ECI modem) *
# *        9  = VID/PID after synchro  (example: 09158000 for ECI modem) *
# *        10 = path to .bin (optional)                                  *
# *                                                                      *
# ************************************************************************

# <CONFIG>
BIN_DIR="/usr/local/bin"
ETC_DIR="/etc"
CONF_DIR="/etc/eciadsl"
PPPD_DIR="/etc/ppp"
VERSION=""
# </CONFIG>

if [ "$1" == "--version" -o "$1" == "-v" ]; then
	echo "$VERSION"
	exit 0
fi

#
# check parameters
#

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

if [ $# -ne 9 -a $# -ne 10 ]; then
    echo -e "\nerror: invalid number of parameters."
    echo -e "syntax:\n  makeconfig <username> <password> <path_to_pppoeci> <dns1> <dns2> <vpi> <vci> <vidpid1> <vidpid2> [<path_bin>]"
    exit 1
fi

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

RET=0

#
# backup and create resolv.conf
#

if [ "$4" != "0" -o "$5" != "0" ]; then
	if [ -n "$4$5" ]; then
    	nomresolv="${ETC_DIR}/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.."
		:> "$nomresolv"
		if [ "$5" != "0" -a -n "$5" ]; then
	    	echo "nameserver $4" >> "$nomresolv"
		fi
		if [ "$5" != "0" -a -n "$5" ]; then
	    	echo "nameserver $5" >> "$nomresolv"
		fi
    	echo "OK"
	fi
fi

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

for auth in "chap" "pap"; do
    nomsecret="${ETC_DIR}/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.. "
		TMPFILE="/tmp/${0##*/}.tmp"
		TMP=$(echo "$1" | sed "s/\\\/\\\\\\\\\\\/g")
		grep -v -E "^[ \t]*\"?$TMP\"?[ \t]*.*" $nombak > "$nomsecret"
        echo -e "\"$1\"\t*\t\"$2\"\t*" >>"$nomsecret"
		rm -f $TMPFILE
        echo "OK"
    else
        echo -n "creating $nomsecret.. "
        echo "# Secrets for authentication using $auth" > "$nomsecret"
        echo -e "\"$1\"\t*\t\"$2\"\t*" >> "$nomsecret"
        echo "OK"
    fi
done

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

vid1=`echo $8 | cut -c 1-4`
pid1=`echo $8 | cut -c 5-8`

vid2=`echo $9 | cut -c 1-4`
pid2=`echo $9 | cut -c 5-8`

nomadsl="${PPPD_DIR}/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.. "
	TMPFILE="/tmp/${0##*/}.tmp"
	TMP=$(echo "$3" | sed "s/\//\\\\\//g")
	echo "s/^pty .\+/pty \\\"$TMP -vpi $6 -vci $7 -vendor 0x$vid2 -product 0x$pid2\\\"/" > $TMPFILE
	TMP=$(echo "$1" | sed "s/\//\\\\\//g")
	echo "s/^user .\+/user \\\"$TMP\\\"/" >> $TMPFILE
	sed -f $TMPFILE "$nombak" > "$nomadsl"
	rm -f $TMPFILE
	if [ -s "$nomadsl" ]; then
	    echo "OK"
	else
		echo -e "\nfailed to set up $nomadsl"
		echo -e "this is a bug, please report this to us, and join your $nombak file\n"
		RET=1
	fi
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 -vendor 0x$vid2 -product 0x$pid2"
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 following line
#
lcp-echo-interval 0

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

#
# create vidpid file
#

echo -n "creating ${ETC_DIR}/eciadsl/vidpid.."
echo "$vid1 $pid1 $vid2 $pid2" > "${ETC_DIR}/eciadsl/vidpid"
echo "OK"

#
# change .bin file
#

if [ $# -eq 10 ]; then
    echo -n "modifying .bin link (${ETC_DIR}/eciadsl/synch.bin -> ${10}).."
    rm -f "${ETC_DIR}/eciadsl/synch.bin" >/dev/null
    ln -sf ${10} "${ETC_DIR}/eciadsl/synch.bin"
    echo "OK"
fi
exit $RET
