#!/bin/sh

# Author: Benoit PAPILLAULT <benoit.papillault@free.fr>
# Creation: 12/05/2004
#
# Author      : Edouard Gomez <ed.gomez@free.fr>
# Last Modifs : May, the 7th 2002
#
# ToDo : this needs a special module configuration for debian
#        which uses files in /etc/modules/ and then run update-modules

# Setup the connection

# mandatory options :
# ask vpi
# ask vci
# ask username
# ask password

# advanced options :
# automatic reconnection : yes
# hotplug support : yes

# depending on kernel version and distro (debian)
# -> update /etc/modules.conf , /etc/modutils/speedtouch or /etc/modprobe.conf

# check for /etc/speedtouch/*.zip

# Ensure to have a working PATH
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin"

# Configuration script for the pppd daemon
PEER_PPPOA="speedtouch-pppoa"
PEER_PPPOE="speedtouch-pppoe"
PPP_DIR="/usr/pkg/etc/ppp"
PEER_DIR="${PPP_DIR}/peers"
CHAP_FILE="/usr/pkg/etc/ppp/chap-secrets"
PAP_FILE="/usr/pkg/etc/ppp/pap-secrets"
PPPOA3="/usr/pkg/sbin/pppoa3"
WHOAMI=`whoami`

PEER_PPPOA_FILE="${PEER_DIR}/${PEER_PPPOA}"
PEER_PPPOE_FILE="${PEER_DIR}/${PEER_PPPOE}"

# check for root privileges

if [ "$WHOAMI" != "root" ]; then
  echo "You must launch this script with root privileges. Enter root password."
  exec su -c "$0 $@"
  exit 1
fi

echo "PPPD Configuration Script for GNU/Linux"
echo

PPPD_PEER="speedtouch-pppoa"

# parse command line params

for arg
do
  case "${arg}" in
      --pppoe)
          PPPD_PEER="speedtouch-pppoe"
          ;;
      *)
    cat <<EOF
usage: $0 [--pppoe]

--pppoe : configure a PPPoE connection instead of PPPoA
EOF

    exit 1
          ;;
  esac
done

if [ ! -f "${PPPOA3}" ] ; then
  echo "You must install the drivers first"
  exit 1
fi

# check if we need to configure hotplug. This is only needed for old
# versions of hotplug (Mandrake 9.2 discovery)

if [ -d /etc/hotplug ]; then
    if [ ! -f /usr/lib/hotplug/speedtouch/usb.usermap ]; then
        if ! grep -q '*.usermap' /etc/hotplug/usb.agent; then
            mkdir -p /usr/lib/hotplug/speedtouch
            cp /etc/hotplug/usb/speedtouch.usermap \
                /usr/lib/hotplug/speedtouch/usb.usermap
            /usr/sbin/update-usb.usermap
        fi
    fi
fi

ZIPFILE=SpeedTouch330_firmware_3012.zip

if [ ! -f "/usr/pkg/etc/speedtouch/${ZIPFILE}" ]; then
  cat <<EOF
You must accept the license on
http://www.speedtouch.com/driver_upgrade_lx_3.0.1.2.htm and download
the file ${ZIPFILE}.
Copy this file in the directory /usr/pkg/etc/speedtouch.
EOF
  exit 1
fi

# -o : do not ask when overwriting files
(cd "/usr/pkg/etc/speedtouch"; unzip -o "${ZIPFILE}")

# Reads user input
read -p "Login    : " LOGIN
read -p "Password : " PASSWORD

cat << EOF

You will be prompted for your Vpi Vci,
Here are some working values but your configuration may differ

+----------------+-----+-----+
|    Country     | VPI | VCI |
+----------------+-----------+
|Belgium, ?      |   8 |  35 |
+----------------+-----------+
|Denmark, Orange |   8 |  35 |
+----------------+-----------+
|France, wanadoo |   8 |  35 |
+----------------+-----------+
|France, ?       |   8 |  67 |
+----------------+-----------+
|Italy, ?        |   8 |  35 |
+----------------+-----------+
|Netherlands, ?  |   8 |  48 |
+----------------+-----------+
|UK, BTopenworld |   0 |  38 |
+----------------+-----------+
|US, BellSouth   |   8 |  35 |
+----------------+-----------+

EOF

read -p "Your VPI      : " VPI
read -p "Your VCI      : " VCI

# Generates chap/pap-secrets line
SECRETLINE="\"$LOGIN\" \"*\" \"$PASSWORD\" \"*\""

# Generates the user line needed in /etc/ppp/peers/adsl
USERLINE="user \"$LOGIN\""

# Creates /etc/ppp/peers
if [ ! -d "${PEER_DIR}" ] ; then
  echo "Creating ${PEER_DIR} directory"
  mkdir -p "${PEER_DIR}"
fi

# Check for /etc/ppp/peers/speedtouch-pppoa existence
if [ -f "${PEER_PPPOA_FILE}" ]; then
  read -p "Do you want to overwrite $PEER_PPPOA_FILE [y/n] ? " ANSWER
  if [ $ANSWER = "n" ] || [ $ANSWER = "N" ] ; then
     exit 1
  fi
  rm -f "${PEER_PPPOA_FILE}"
fi

# Check for /etc/ppp/peers/speedtouch-pppoe existence
if [ -f "${PEER_PPPOE_FILE}" ]; then
  read -p "Do you want to overwrite $PEER_PPPOE_FILE [y/n] ? " ANSWER
  if [ $ANSWER = "n" ] || [ $ANSWER = "N" ] ; then
     exit 1
  fi
  rm -f "${PEER_PPPOE_FILE}"
fi

#
# Generates /etc/ppp/peers/speedtouch-pppoa and 
# /etc/ppp/peers/speedtouch-pppoe files
#

echo "Generating ${PEER_PPPOA_FILE}"
cat > "${PEER_PPPOA_FILE}" <<EOF
# ##########################################################################
#
# Automatically generated on $(date)
# by $0 for User space Speedtouch USB driver
#
# This file could be rename but its place is under ${PEER_DIR}
# To connect to Internet using this configuration file
# pppd call ${PEER_PPPOA}
#
# ##########################################################################

#
# Put your ISP login name here and update your chap-secrets
# (This example is a typical Wanadoo.fr login)
#

$USERLINE

#
# PPPoA3 daemon is the default one but you can use the old PPPoA2
#
# To help you for your choice :
#
# pppoa3 is multithreaded, has more features, has a better design.
# pppoa2 is simpler but is deprecated due to its limited design.
#
# pppoa2 is still in this driver set because BSD systems have a
# problem with usb+multithreading but all GNU/Linux systems should
# use pppoa3
#

#
# Comment only one of those pty option
#
# Don't forget to adapt the vpi.vci couple to your ISP/country settings
# Read the FAQ for some vpi.vci couples
#
# If you installed from a rpm or deb package the right path is /usr/sbin
# instead of /usr/local/sbin
#

pty "\\"${PPPOA3}\\" -vpi $VPI -vci $VCI"
linkname "${PEER_PPPOA}"

#
# pppoa2 must run in sync mode, this option must be set.
#
# pppoa3 can run in either sync or async mode
#
# In order to use the async mode with pppoa3 :
#   - comment this option
#   - replace the pty "..." line with this one
#     pty "$PPPOA3 -a -c -m 1 -vpi $VPI -vci $VCI"
#

sync

#
# We don't need a default ip, and we don't need the peer to auth itself
#

noauth
noipdefault

#
# We don't want to negociate compression schemes at all
#

default-asyncmap
noaccomp
nopcomp
noccp
novj

#
# We don't want to use proxy ARP
#

noproxyarp

#
# Makes pppd "dial again" when the connection is lost
#

persist

#
# pppd will try to connect "maxfail" times and wait "holdoff" seconds
# between each try.
#

holdoff 4
maxfail 1000

#
# Asks your ISP for its DNS ip
# (look at the /etc/ppp/resolv.conf)
#

usepeerdns

#
# Makes this ppp link the default inet route
# (route -n to check it)
#

defaultroute

#
# If something goes wrong try uncommenting this
#

debug
kdebug 1

#
# LCP requests are not mandatory plus they fail
# on some systems
#
#lcp-echo-interval 600
#lcp-echo-failure 10
EOF

echo "Generating ${PEER_PPPOE_FILE}"
cat > "${PEER_PPPOE_FILE}" <<EOF
# ##########################################################################
#
# Automatically generated on $(date)
# by $0 for User space Speedtouch USB driver
#
# This file could be rename but its place is under ${PEER_DIR}
# To connect to Internet using this configuration file
# pppd call ${PEER_PPPOE}
#
# ##########################################################################

#
# Put your ISP login name here and update your chap-secrets
# (This example is a typical Wanadoo.fr login)
#

$USERLINE

#
# PPPoA3 daemon is the default one but you can use the old PPPoA2
#
# To help you for your choice :
#
# pppoa3 is multithreaded, has more features, has a better design.
# pppoa2 is simpler but is deprecated due to its limited design.
#
# pppoa2 is still in this driver set because BSD systems have a
# problem with usb+multithreading but all GNU/Linux systems should
# use pppoa3
#

#
# Comment only one of those pty option
#
# Don't forget to adapt the vpi.vci couple to your ISP/country settings
# Read the FAQ for some vpi.vci couples
#
# If you installed from a rpm or deb package the right path is /usr/sbin
# instead of /usr/local/sbin
#

pty "pppoe -s -I \`\\"${PPPOA3}\\" -vpi $VPI -vci $VCI -b\`"
linkname "${PEER_PPPOE}"

disconnect "killall pppoa3"

#
# pppoa2 must run in sync mode, this option must be set.
#
# pppoa3 can run in either sync or async mode
#
# In order to use the async mode with pppoa3 :
#   - comment this option
#   - replace the pty "..." line with this one
#     pty "$PPPOA3 -a -c -m 1 -vpi $VPI -vci $VCI"
#

sync

#
# We don't need a default ip, and we don't need the peer to auth itself
#

noauth
noipdefault

#
# We don't want to negociate compression schemes at all
#

default-asyncmap
noaccomp
nopcomp
noccp
novj

#
# We don't want to use proxy ARP
#

noproxyarp

#
# Makes pppd "dial again" when the connection is lost
#

persist

#
# pppd will try to connect "maxfail" times and wait "holdoff" seconds
# between each try.
#

holdoff 4
maxfail 1000

#
# Asks your ISP for its DNS ip
# (look at the /etc/ppp/resolv.conf)
#

usepeerdns

#
# Makes this ppp link the default inet route
# (route -n to check it)
#

defaultroute

#
# If something goes wrong try uncommenting this
#

debug
kdebug 1

#
# LCP requests are not mandatory plus they fail
# on some systems
#
#lcp-echo-interval 600
#lcp-echo-failure 10
EOF

#
# Generates /etc/speedtouch/speedtouch.conf
#

CONF="/usr/pkg/etc/speedtouch/speedtouch.conf"
echo "Generating ${CONF}"
mkdir -p "`dirname "${CONF}"`"
cat > "${CONF}" <<EOF
# ##########################################################################
#
# Automatically generated on $(date)
# by $0 for User space Speedtouch USB driver
# 

# define the usb devices path for Suse 9.1
DEVICES=/proc/bus/usb/devices
DEVICES_SUSE=/proc/bus/usb/devices_please-use-sysfs-instead

if [ ! -f "\${DEVICES}" ] && [ -f "\${DEVICES_SUSE}" ]; then
	echo "Warning: using \${DEVICES_SUSE}"
	DEVICES="\${DEVICES_SUSE}"
fi


REV=\`grep "Vendor=06b9 ProdID=4061" "\${DEVICES}" | awk '{print \$5}' | head -1 \`

if [ "\${REV}" = "4.00" ]; then 
# For rev 4 modems :
  FIRMWARE_LOADER_FILE="/usr/pkg/etc/speedtouch/ZZZL_3.012"
  FIRMWARE_FILE="/usr/pkg/etc/speedtouch/ZZZL_3.012"
else
# For rev 0 and rev 2 modems:
  FIRMWARE_LOADER_FILE="/usr/pkg/etc/speedtouch/KQD6_3.012"
  FIRMWARE_FILE="/usr/pkg/etc/speedtouch/KQD6_3.012"
fi

MODEM_RUN_OPTIONS=""

PPPD_PEER=${PPPD_PEER}

EOF

#
# Generates user/password entry in chap and pap secrets files
#

# Creates the chap file if needed
if [ ! -f "${CHAP_FILE}" ] ; then
  echo Creating $CHAP_FILE
  touch $CHAP_FILE
  chmod 600 $CHAP_FILE
  echo "# Secrets for authentication using CHAP" > $CHAP_FILE
  echo "# client        server         secret         IP addresses" >> $CHAP_FILE
fi

# Creates the pap file if needed
if [ ! -f "${PAP_FILE}" ] ; then
  echo Creating $PAP_FILE
  touch $PAP_FILE
  chmod 600 $PAP_FILE
  echo "# Secrets for authentication using PAP" > $PAP_FILE
  echo "# client        server         secret         IP addresses" >> $PAP_FILE
fi

# Saves old files
echo Saving old $CHAP_FILE to $CHAP_FILE.old
cp -f $CHAP_FILE $CHAP_FILE.old
chmod 600 $CHAP_FILE.old

echo Saving old $PAP_FILE to $PAP_FILE.old
cp -f $PAP_FILE  $PAP_FILE.old
chmod 600 $PAP_FILE.old

# Remove old login entry from pap and chap file
echo Removing old $LOGIN entry from $CHAP_FILE
grep -v $LOGIN $CHAP_FILE.old > $CHAP_FILE
echo Removing old $LOGIN entry from $PAP_FILE
grep -v $LOGIN $PAP_FILE.old  > $PAP_FILE

# Add the new entry
echo Adding new $LOGIN entry to $CHAP_FILE
echo $SECRETLINE >> $CHAP_FILE
echo Adding new $LOGIN entry to $PAP_FILE
echo $SECRETLINE >> $PAP_FILE

#
# Add ppp entries to /etc/modules.conf
#

if [ -f /etc/conf.modules ] ; then
  MODULE_CONF="/etc/conf.modules"
fi

if [ -f /etc/modules.conf ] ; then
  MODULE_CONF="/etc/modules.conf"
fi

#
# We handle the Debian way here
#
if [ -f /etc/debian_version ]; then
  MODULE_CONF="/etc/modutils/hdlc"
  echo "Configuring the modules.conf the debian way !"
fi

#
#    Handle Fedora (patch from Jon LEVELL <jon@coralbark.net>). Untested!
#

if [ -f /etc/redhat-release ]; then
	if grep -q "Fedora Core" /etc/redhat-release; then
		if grep -q "Yarrow" /etc/redhat-release; then
            # Core 1 based system
			MODULE_CONF="/etc/modules.conf"
		else
            # Core 2 or later
			MODULE_CONF="/etc/modprobe.conf"
		fi
	fi
fi

echo "Adding ppp entries to your $MODULE_CONF"

# This bunch of aliases are already defined in debian systems
if [ ! -f /etc/debian_version ]; then

  echo >> $MODULE_CONF
  grep -q "char-major-108" $MODULE_CONF
  if [ $? -ne 0 ] ; then
    echo "alias char-major-108  ppp_generic" >> $MODULE_CONF
  fi 
  grep -q "tty-ldisc-3" $MODULE_CONF
  if [ $? -ne 0 ] ; then
    echo "alias tty-ldisc-3     ppp_async" >> $MODULE_CONF
  fi

fi

# This one needs to be added in every case
grep -q "tty-ldisc-13" $MODULE_CONF
if [ $? -ne 0 ] ; then
  echo "alias tty-ldisc-13    n_hdlc" >> $MODULE_CONF
fi

# This bunch of aliases are also already defined in debian systems
if [ ! -f /etc/debian_version ]; then

  grep -q "tty-ldisc-14" $MODULE_CONF
  if [ $? -ne 0 ] ; then
    echo "alias tty-ldisc-14    ppp_synctty" >> $MODULE_CONF
  fi
  grep -q "ppp-compress-21" $MODULE_CONF
  if [ $? -ne 0 ] ; then
    echo "alias ppp-compress-21 bsd_comp" >> $MODULE_CONF
  fi
  grep -q "ppp-compress-24" $MODULE_CONF
  if [ $? -ne 0 ] ; then
    echo "alias ppp-compress-24 ppp_deflate" >> $MODULE_CONF
  fi
  grep -q "ppp-compress-26" $MODULE_CONF
  if [ $? -ne 0 ] ; then
    echo "alias ppp-compress-26 ppp_deflate" >> $MODULE_CONF
  fi

fi

#
# We update the /etc/modules.conf with the debian tool
#
if [ -f /etc/debian_version ]; then
  echo "Updating /etc/modules.conf using update-modules"
  update-modules
fi

# Finished...
echo
echo PPPD configuration done.
