#!/bin/sh

# Author   : Jean-Sebastien VALETTE <jean-sebastien.valette@libertysurf.fr>
# Creation : 06/12/2001
# Licence  : GPL

# 23/10/2002 wwp:
# removed ugly hardcoded pathes, used new scheme.

# 21/10/2002 wwp:
# default .bin is synch01.bin now, /etc/eciadsl/synch.bin is a symlink to this one!
# removed ugly modutils uses, added some delays.

# 03/02/2002: added a sleep 2 after loading USB controller driver, as suggested
# by Sebastien YAPO <sebastien.yapo@free.fr>.

# 07/02/2002: added the CVS Id
# CVS $Id: startmodem,v 1.33 2002/11/11 09:06:00 wwp Exp $
# Tag $Name:  $

# 15/02/2002 Benoit PAPILLAULT
#   remove "modprobe uhci" since this module never worked with this modem

# 13/06/2002 Sebastien HELLEU <flashcode@free.fr>
#   new file: /etc/eciadsl/vidpid : contains vid/pid (1 & 2) for your modem
#             example of content ECI:   0547 2131 0915 8000

# 03/07/2002 Sebastien HELLEU <flashcode@free.fr>
#   new file: /etc/eciadsl/eci_wan.bin (created by Makefile and eciconf) :
#             this is a link to your .bin
#   to change your .bin file, run eciconf or type this command :
#     rm /etc/eciadsl/eci_wan.bin && ln -s /path/your.bin /etc/eciadsl/eci_wan.bin

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

SYNCH_ATTEMPTS=1

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

if [ $# -eq 4 ]; then
	vid1="$1"
	pid1="$2"
	vid2="$3"
	pid2="$4"
	echo -e "\ngot vid/pid from command line"
else
	if [ -f "$CONF_DIR/vidpid" ]; then
		vid1=`cat "$CONF_DIR/vidpid" | cut -f 1 -d ' '`
		pid1=`cat "$CONF_DIR/vidpid" | cut -f 2 -d ' '`
		vid2=`cat "$CONF_DIR/vidpid" | cut -f 3 -d ' '`
		pid2=`cat "$CONF_DIR/vidpid" | cut -f 4 -d ' '`
		echo -e "\ngot vid/pid from $CONF_DIR/vidpid"
	else
	    vid1="0547"
	    pid1="2131"
	    vid2="0915"
	    pid2="8000"
		echo -e "\ndefault vid/pid assumed"
	fi
fi
echo ": $vid1/$pid1 $vid2/$pid2"


echo -e "\nsetting up USB support (1/5).."

if [ ! -d /proc/bus/usb ]; then
    modprobe usbcore > /dev/null
	sleep 1
fi

# mount usbdevfs is this is not the case
if [ ! -f /proc/bus/usb/devices ]; then
    echo -e "\nloading preliminary USB device filesystem" 
    mount -t usbdevfs none /proc/bus/usb
	sleep 1
	mount | grep usbdevfs > /dev/null
	if [ $? == "0" ]; then
		echo "couldn't mount usbdevfs"
	else
		echo "usbdevfs mounted successfully"
	fi
fi

#verify if dabusb is loaded
lsmod | grep dabusb > /dev/null
if [ $? -eq 0 ]; then
    echo -e "\ndabusb seems to be present trying to remove it..."
    modprobe -r dabusb
	sleep 1
    lsmod | grep dabusb > /dev/null
	if [ $? == "0" ]; then
		echo "couldn't remove dabusb try to rmmod dabusb manually"
	else
		echo "dabusb removed successfully"
	fi
fi

#verify if ehci-hcd is loaded
lsmod | grep ehci-hcd > /dev/null
if [ $? -eq 0 ]; then
    echo -e "\nehci-hcd seems to be present trying to remove it..."
    modprobe -r ehci-hcd
	sleep 1
    lsmod | grep ehci-hcd > /dev/null
	if [ $? -eq 0 ]; then
		echo "couldn't remove ehci-hcd try to rmmod ehci-hcd manually"
	else
		echo "ehci-hcd removed successfully"
	fi
fi

# try to locate UHCI controller
grep -A 4 USB /proc/pci | grep I/O > /dev/null
if [ $? -eq 0 ]; then
	# ok, we have a UHCI controller, check if the linux driver is loaded
    grep "^S:  Product=USB UHCI Root Hub" /proc/bus/usb/devices > /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\nloading UHCI support"
		modprobe -l | grep usb-uhci > /dev/null
		if [ $? -ne 0 ]; then
			echo "warning: usb-uhci module doesn't exist"
		else
        	modprobe usb-uhci
        	sleep 2
    		lsmod | grep usb-uhci > /dev/null
			if [ $? -ne 0 ]; then
				echo "couldn't load usb-uhci module"
			else
				echo "usb-uhci loaded successfully"
			fi
		fi
    fi
fi

# try to locate OHCI controller
grep -A 4 USB /proc/pci | grep memory > /dev/null
if [ $? -eq 0 ]; then
	# ok, we have a OHCI controller, check if the linux driver is loaded
    grep "^S:  Product=USB OHCI Root Hub" /proc/bus/usb/devices > /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\nloading OHCI support"
		modprobe -l | grep usb-ohci > /dev/null
		if [ $? -ne 0 ]; then
			echo "warning: usb-ohci module doesn't exist"
		else
	        modprobe usb-ohci
	        sleep 2
    		lsmod | grep usb-ohci > /dev/null
			if [ $? -ne 0 ]; then
				echo "couldn't load usb-ohci module"
			else
				echo "usb-ohci loaded successfully"
			fi
		fi
    fi
fi

#verify if n_hdlc is loaded (module only so, it's safe)
"$BIN_DIR/check-hdlc" > /dev/null
if [ $? -ne 0 ]; then
	lsmod | grep n_hdlc > /dev/null
	if [ $? -ne 0 ]; then
		modprobe -l | grep n_hdlc > /dev/null
		if [ $? -ne 0 ]; then
			echo -e "\nwarning: n_hdlc support not found in kernel config"
		else
    		modprobe n_hdlc
			sleep 1
    		lsmod | grep n_hdlc > /dev/null
			if [ $? -ne 0 ]; then
				echo -e "\ncouldn't load n_hdlc module"
			else
				echo -e "\nn_hdlc loaded successfully"
			fi
		fi
	fi
fi

echo -e "\nloading firmware (2/5)..\n"
# check for the EZ-USB chips. If it's not there, maybe the firmware is
# already loaded ... so continue.
grep "^P:  Vendor=$vid1 ProdID=$pid1" /proc/bus/usb/devices > /dev/null
if [ $? -eq 0 ]; then
    "$BIN_DIR/eci-load1" 0x$vid1 0x$pid1 0x$vid2 0x$pid2 "$CONF_DIR/firmware.bin"
    if [ $? -ne 0 ]; then
        echo "failed to load firmware"
        exit -1
    fi
    echo "firmware loaded successfully"
else
	grep "^P:  Vendor=$vid2 ProdID=$pid2" /proc/bus/usb/devices > /dev/null
	if [ $? -eq 0 ]; then
		echo "firmware seems to be already loaded"
	else
    	echo "modem not found"
    	exit -1
	fi
fi


echo -e "\nsetting up modem (3/5)..\n"
CNT=1
RET=$CNT
while [ $CNT -le $SYNCH_ATTEMPTS ]; do
	"$BIN_DIR/eci-load2" 0x$vid2 0x$pid2 "$CONF_DIR/synch.bin"
	RET=$?
	CNT=$(expr $CNT + 1)
done
if [ $RET -eq 0 ]; then
	echo "synchronization successful"
else
    echo "failed to get synchronization"
    exit -1
fi


echo -e "\nconnecting modem (4/5)..\n"
nice --20 pppd call adsl updetach
if [ $? -ne 0 ]; then
    echo "failed to connect"
    exit -1
fi
echo "connection successful"


echo -e "\nsetting up route table (5/5)..\n"
# Wait until ppp0 get available
COUNTER=10
FLREADY=0
TIMES=1
echo -n "waiting for ppp0.."
while [ $TIMES -le $COUNTER ]; do
    ifconfig ppp0 > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        FLREADY=1
        break
    fi
    sleep 1
    echo -n "."
	TIMES=$(expr $TIMES + 1)
done
echo
if [ "$FLREADY" -eq 0 ]; then
	echo "no ppp0 device found"
	exit -1
fi

for IFACE in 0 1; do
	route -n | grep -E "[ \t]UG[ \t][ \t0-9\+\-]+eth$IFACE$" > /dev/null
	if [ $? -eq 0 ]; then
		echo "removing existing default route (eth$IFACE).."
		route del default dev eth$IFACE
	fi
done

echo "adding default route.."
route -n | grep -E "[ \t]UG[ \t][ \t0-9\+\-]+ppp0$" > /dev/null
if [ $? -ne 0 ]; then
   	route add default dev ppp0
	if [ $? -ne 0 ]; then
		echo "failed to set default route"
	else
		echo "default route added"
	fi
else
	echo "default route already set"
fi

exit 0
