#!/bin/sh

# Author: Benoit PAPILLAULT <benoit.papillault@free.fr>
# Creation: 12/05/2004

# Goal: Start the connection

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

# 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 "Uploading Speedtouch firmware ..."

# if one configuration file exists, read it. This file should define :
# 
# FIRMWARE_LOADER_FILE
# FIRMWARE_FILE
# PPPD_PEER
#

# At this point, USB is properly configured (/proc/bus/usb/devices can
# be used)

if [ -f /etc/speedtouch.conf ]; then
  . /etc/speedtouch.conf
fi

if [ "$FIRMWARE_LOADER_FILE" ]; then
  MODEM_RUN_OPTIONS="$MODEM_RUN_OPTIONS -a $FIRMWARE_LOADER_FILE"
fi

if [ "$FIRMWARE_FILE" ]; then
    MODEM_RUN_OPTIONS="$MODEM_RUN_OPTIONS -f $FIRMWARE_FILE"
else
    echo "FIRMWARE_FILE undefined"
    exit 1
fi

# convention : when modem_run returns, meaning that the DSL line is
# up, we create the file /var/run/speedtouch-up. This file is removed
# when the modem is removed (in the hotplug script).

# Of course, this does not work when several modems are connected (we
# should write a small executable that get the mac addresse from the
# modem and use it as a key)

if ! "/usr/sbin/modem_run" $MODEM_RUN_OPTIONS; then
    exit 1
fi

#
# for PPPoA connections :
#
# pppd call speedtouch-pppoa updetach
#
# for PPPoE connections :
#
# pppd call speedtouch-pppoe updetach
#

FROM_HOTPLUG=
for arg; do
    [ "${arg}" = "--nocall" ] && exit 0
    [ "${arg}" = "--boot" ] && FROM_HOTPLUG=1
done

if [ -n "$FROM_HOTPLUG" ]; then
    ONBOOT=`sed -n "/ONBOOT=\(.*\)/s//\1/p" /etc/sysconfig/network-scripts/ifcfg-${PPPD_PEER} 2> /dev/null`
    if [ "${ONBOOT}" = "no" -o "${ONBOOT}" = "NO" ]; then
	echo "called from hotplug, not starting connection"
	exit 0
    else
	echo "called from hotplug, starting connection"
    fi
fi

echo "Starting ADSL connection..."

if [ "${PPPD_PEER}" == "" ]; then
    echo "PPPD_PEER undefined"
    exit 1
fi

case `uname` in
  Linux)
    pppd call "${PPPD_PEER}" updetach
    ;;
  FreeBSD)
    ppp -background "${PPPD_PEER}"
    ;;
esac

