#!/bin/bash

##----------------------------------------------------
## w.modemsetup
## Sets up the horrible Intel 536EP winmodem on Ubuntu
## Last updated 2006-07-09 by Bnonn
##
## This script has only been tested on Breezy, but I
## can't see any reason it won't work on Dapper.
##----------------------------------------------------

requiredpackages="linux-headers-386 linux-source-2.6.12 build-essential gcc-3.4 gnome-ppp"
driversource="ftp://aiedownload.intel.com/df-support/9266/eng/Intel-536EP-4.71.tgz"
driverdir="$HOME/modem/"
moduledir="/lib/modules/`uname -r`/kernel/drivers/char"
# udev_string='BUS=\"pci\", KERNEL==\"Intel536\", NAME=\"%k\", SYMLINK+=\"modem\", GROUP=\"dialout\"'

function errorcheck()
{
	## It's always wise, I've learned the hard way, to incorporate a basic error-checking function...
	if [ "${1}" -ne "0" ] ; then
		echo -e "\a\n= ERROR #${1}; bailing out! ${2}\n"
		cleanup
		exit ${1}
	fi
}

function cleanup()
{
	if [ -e $driverdir ] ; then
		echo -e "\n= Cleaning up temporary source files..."
		rm -r $driverdir \
			&& echo -e "= OK."
	fi
}

## Main functions

function get_requiredpackages()
{
	## Getting the packages we need to successfully compile the driver
	echo -e "\n= Downloading kernel packages to allow source compilation (requires root privileges, so enter your password)...\n"	
	sudo apt-get update
	sudo apt-get install -y $requiredpackages
}

function get_driversource()
{
	## Getting the driver source code itself. Hopefully this URI is persistent
	echo -e "\n= Downloading the modem driver source code...\n"
	wget -t 20 -T 20 -P $driverdir $driversource
}

function build_driver()
{
	## Extracting the driver and building it
	echo -e "\n= Extracting the driver source...\n"
	cd $driverdir
	tar xfvz Intel-536EP-4.71.tgz
	cd Intel-536
	echo -e "\n= Compiling the drive source...\n"
	make clean && make 536
}

function install_driver()
{
	## Copy the driver to the correct directory...
	echo -e "\n= Installing the driver..."
	sudo cp Intel536.ko $moduledir
	## Make the system aware of it...
	sudo depmod -a
	## And then load it...
	sudo modprobe Intel536 \
		&& echo -e "= OK."
}

function configure_for_boot()
{
	## Configuring the driver to load at boot-time
	echo -e "\n= Configuring the driver kernel module for boot-time load..."

	## The commented code below never seemed to work well, and Matt Brown's method seems like
	## a much better way, although I don't imagine it will automatically create /dev/modem.
	## Still, you can use /dev/536ep0 without problems.

	install -o root -g root -m 755 Intel536_boot /etc/init.d/Intel536
	ln -s /etc/init.d/Intel536 /etc/rcS.d/S16Intel536

	## Add the 536 driver to the modules file
	## sudo bash -c "echo Intel536 >> /etc/modules"
	## echo -e "= OK."
	## Write a local udev ruleset to create a /dev/modem with appropriate permissions,
	## linked to /dev/536ep0 (the default Intel 536 modem device node)
	## echo -e "\n= Configuring driver device node options..."
	## sudo bash -c "echo ${udev_string} >> /etc/udev/rules.d/10-local.rules"
	## echo -e "= OK."

	## Lastly, we configure the current user to be part of the dialout group, 
	## so they can use /dev/modem
	echo -e "\n= Configuring current user for dialout privileges..."
	sudo gpasswd -a $USER dialout > /dev/null \
		&& echo -e "= OK."
}

echo -e "\n= Running Intel 536EP modem setup script"
echo -e "= We're going to download system packages via apt, as well as the driver source via wget"
echo -e "= A lot of info text will scroll up the screen. If something goes wrong, the script will bail out with an error."
echo -e "= Cross your fingers now..."

get_requiredpackages;
	errorcheck $? "An error occurred installing the packages required to compile the modem driver..."
get_driversource;
	errorcheck $? "An error occurred downloading the modem driver source package..."
build_driver;
	errorcheck $? "An error occurred building the driver from source..."
install_driver;
	errorcheck $? "An error occurred installing the driver..."
configure_for_boot;
	errorcheck $? "An error occurred configuring the driver module for boot-time..."
cleanup;
	errorcheck $? "An error occurred cleaning up temporary files. You'll need to clean these manually..."

echo -e "\n= Modem installation completed happily. Exiting normally...\n"
