#!/bin/bash
# written by Antti Kajander aka Kaja on IRCNet
# there's no warranty of any kind, use at your own risk
# the script is in the public domain, use/modify/distribute as you wish

script_version="0.9.8.4"
script_date="11th Mar 2009"
svn_url="http://svn.madwifi-project.org/madwifi/trunk"
PATH=$PATH:/sbin

function check_distro {
	if [ -f /etc/fedora-release ]; then
		distro="Fedora"
	elif [ -f /etc/debian_version ]; then
		distro="Debian"
	else
	echo "Unknown distro, exiting..."
	exit
	fi
}

function check_hw {
	if [ "$(lspci | grep Atheros -c)" != "0" ]; then
		echo "Found at least one Atheros device."
	else
		echo "No Atheros device found, trying anyway."
	fi
}

function dbg_info {
	uname -a
	echo "Distro: $distro"
	lspci -v | grep Atheros -A 20 | grep -v Capabilities | sed 's/^$/\n\n\n\n\n\n\n\n\n\n/' | head -n 10 | uniq
	lsmod | grep ath_
	dmesg | grep ath_ | sort -u
	dmesg | grep wifi0 | sort -u
	echo "wifi0: $(ifconfig wifi0 2> /dev/null | wc -l) ath0: $(ifconfig ath0 2> /dev/null | wc -l)"
	exit
}

function check_params {
	if [ "$mode" = "--tarball" ]; then
		if [ "$tarball" = "" ]; then
			echo "You must tell where the tarball is, exiting..."
			exit
		fi
		if [ -f $tarball ]; then
			echo -n
		else
			"Tarball not found in $tarball, exiting..."
			exit
		fi
	elif [ "$mode" = "--reinstall" ]; then
		echo -n
	elif [ "$mode" = "--save" ]; then
		echo -n
	elif [ "$mode" = "--working" ]; then
		echo -n
	elif [ "$mode" = "--info" ]; then
		echo -n
	elif [ "$mode" = "--uninstall" ]; then
		echo -n
	else
		if [ "$mode" != "" ]; then
			echo "Unknown option $mode, see --help, exiting..."
			exit
		fi
	fi
}

function print_help {
	echo "madwifi.sh version $script_version ($script_date)"
	echo "written by Antti Kajander aka Kaja on IRCNet"
	echo
	echo "If you have a working internet connection just do ./madwifi.sh"
	echo
	echo "For a reinstall you can use ./madwifi.sh --reinstall"
	echo
	echo "If the current source code works fine, use --save to save it!"
	echo "Updated code doesn't work? Use --working to use the saved source code!"
	echo
	echo "To use a source tarball from http://madwifi-project.org/"
	echo "use ./madwifi --tarball /path/to/madwifi-x.y.z.tar.gz"
	echo "You can use a release or a snapshot tarball."
	echo
	echo "If you don't have a working internet connection you must"
	echo "manually install all build dependencies:"
	if [ "$distro" = "Debian" ]; then
		echo "make, gcc, libc6-dev, linux-libc-dev, kernel-headers-$(uname -r)"
	elif [ "$distro" = "Fedora" ]; then
		echo "make, gcc, perl, kernel-devel-$(uname -r)"
	fi
	echo
	echo "To uninstall MadWifi run ./madwifi.sh --uninstall"
	echo
	echo "REMEMBER TO RUN THE SCRIPT AFTER EVERY KERNEL UPDATE!"
	echo "Otherwise things won't work."
	echo
	exit
}

function save_sources {
	cd /usr/src
	if [ -x madwifi ]; then
		echo -n "Saving current sources to /usr/src/madwifi-working.tar.gz..."
		tar cfz madwifi-working.tar.gz madwifi || exit
		echo "OK"
		exit
	else
		echo "No sources found, exiting..."
		exit
	fi
}

function check_env {
	if [ "`whoami`" != "root" ]; then
		if [ $distro = "Debian" ]; then
			echo "You must use sudo ./madwifi.sh $mode $tarball"
		elif [ $distro = "Fedora" ]; then
			echo "You must use su -c './madwifi.sh $mode $tarball'"
		fi
		exit
	fi
}

function check_deps_deb {
	mak=`dpkg -l make | grep ^ii -c`
	echo -n "."
	comp=`dpkg -l gcc | grep ^ii -c`
	libc=`dpkg -l libc6-dev | grep ^ii -c`
	echo -n "."
	perl=`dpkg -l perl | grep ^ii -c`
	kerndev=`dpkg -l linux-headers-$(uname -r) | grep ^ii -c`
	echo "."
	if [ "$mode" != "--tarball" ]; then
		sub=`dpkg -l subversion | grep ^ii -c`
		if [ $sub = 0 ]; then
			echo -n "Installing subversion..."
			apt-get install -qq subversion || exit
			echo "OK"
		fi
	fi
	if [ $mak = 0 ]; then
		echo -n "Installing make..."
		apt-get install -qq make || exit
		echo "OK"
	fi
	if [ $kerndev = 0 ]; then
		echo -n "Installing linux-headers..."
		apt-get install -qq linux-headers-$(uname -r) || exit
		echo "OK"
	fi
	if [ $comp = 0 ]; then
		echo -n "Installing gcc..."
		apt-get install -qq gcc || exit
		echo "OK"
	fi
	if [ $libc = 0 ]; then
		echo -n "Installing libc6-dev..."
		apt-get install -qq libc6-dev || exit
		echo "OK"
	fi
	if [ $perl = 0 ]; then
		echo -n "Installing perl..."
		apt-get install -qq perl || exit
		echo "OK"
	fi
}

function check_deps_rh {
	mak=`yum list make | grep installed -c`
	echo -n "."
	kerndev=`yum list kernel-devel-$(uname -r) | grep installed -c`
	echo -n "."
	comp=`yum list gcc | grep installed -c`
	echo "."
	if [ "$mode" != "--tarball" ]; then
		sub=`yum list subversion | grep installed -c`
		if [ $sub = 0 ]; then
			echo "Installing subversion..."
			yum install -y subversion > /dev/null || exit
		fi
	fi
	if [ $mak = 0 ]; then
		echo -n "Installing make..."
		yum install -y make > /dev/null || exit
		echo "OK"
	fi
	if [ $kerndev = 0 ]; then
		echo -n "Installing kernel headers..."
		yum install -y kernel-devel-`uname -r` > /dev/null || exit
		echo "OK"
	fi
	if [ $comp = 0 ]; then
		echo -n "Installing gcc..."
		yum install -y gcc > /dev/null || exit
		echo "OK"
	fi
}


function check_deps {
	echo -n "Checking build dependencies"
	if [ "$distro" = "Debian" ]; then
		check_deps_deb
	elif [ "$distro" = "Fedora" ]; then
		check_deps_rh
	fi
	echo "...build dependencies OK"
}

function svn_co {
	echo -n "Fetching sources from SVN..."
	svn co $svn_url madwifi > /dev/null || exit
	cd madwifi
	echo "OK"
}

function svn_up {
	cd madwifi
	echo -n "Checking for updates."
	current_version=$(svn info | grep ^Revision)
	echo -n "."
	svn up > /dev/null || exit
	echo ".OK"
	if [ "$(svn info | grep ^Revision)" == "$current_version" ]; then
		echo "Already up to date, exiting..."
		echo "Use --reinstall to rebuild the current source tree."
		exit
	fi
}

function unroll_tarball {
	version=`echo "$tarball" | sed -e 's/.*madwifi-//' | sed -e 's/.tar.gz//'`
	echo -n "Unrolling tarball version $version..."
	tar zxf $tarball -C /usr/src || exit
	cd /usr/src/madwifi-$version || exit
	echo "OK"
}

function unroll_working {
	cd /usr/src
	if [ -f madwifi-working.tar.gz ]; then
		echo -n "Unrolling saved sources from backup tarball"
		if [ -x madwifi ]; then
			rm -R madwifi
		fi
		echo -n "."
		tar zxf madwifi-working.tar.gz || exit
		echo -n "."
		cd madwifi || exit
		echo ".OK"
	else
		echo "No backup tarball found, exiting..."
		exit
	fi	
}

function run_scripts {
	echo -n "Removing previous MadWifi installations."
	cd scripts || exit
	for i in 0 1 2 3; do
		ifconfig ath$i down &> /dev/null
		ifconfig wifi$i down &> /dev/null
	done

	if [ -f madwifi-unload ]; then
		./madwifi-unload > /dev/null
	elif [ -f madwifi-unload.bash ]; then
		./madwifi-unload.bash > /dev/null
	fi
	echo -n "."
	echo r | ./find-madwifi-modules.sh $(uname -r) > /dev/null
	cd ..
	echo ".OK"
}

function build_mw {
        echo -n "Building MadWifi (warnings are okay)."
        make clean > /dev/null || exit
	echo -n "."
        make -j2 > /dev/null || exit
	echo ".OK"
}

function install_mw {
	echo -n "Installing MadWifi."
	make install > /dev/null || exit
	echo -n "."
	depmod -ae || exit
	echo ".OK"
}

function load_module {
	echo -n "Loading kernel module..."
	modprobe ath_pci || exit
	echo "OK"
	echo -n "Adding ath_pci to /etc/modules..."
	echo "ath_pci" >> /etc/modules || ecit
	echo "OK"
}

function check_module {
	if [ "$(dmesg | grep wifi0 | grep found -c)" != 0 ]; then
		echo
		echo "Driver detected the chip."
		echo "You can use the networking icon in the notification area to connect to a network in a minute."
	else
		echo
		echo "It seems that the driver did not detect compatible hardware."
		echo "Please try to connect to an AP. If it doesn't work, see the "
		echo "Troubleshooting part in the forum. If after reading the Troubleshooting"
		echo "part and the WHOLE THREAD it still doesn't work, post your"
		echo "sudo ./madwifi.sh --info output to the forum."
	fi
}


mode="$1"
tarball="$2"

check_distro
if [ "$mode" = "--help" ]; then
	print_help
fi

check_params
check_env

if [ "$mode" = "--uninstall" ]; then
	cd /usr/src
	echo -n "Removing ath_pci from /etc/modules..."
	sed -ie 's/^ath_pci//g' /etc/modules
	echo "OK"
	if [ -x madwifi ]; then
		cd madwifi
		run_scripts
		echo -n "Removing the source tree.."
		cd /usr/src
		rm -R madwifi
		echo ".OK"
		if [ "$distro" == "Debian" ]; then
			echo -n "Reinstalling Ubuntu kernel and module packages."
			if [ "$(dpkg -l | grep linux-restricted-modules | grep ^ii | wc -l)" != "0" ]; then
				apt-get install --reinstall -qq linux-restricted-modules-$(uname -r) > /dev/null
			fi
			echo -n "."
			if [ "$(dpkg -l | grep linux-backports-modules | grep ^ii | wc -l)" != "0" ]; then
				apt-get install --reinstall -qq linux-backports-modules-$(uname -r) > /dev/null
			fi
			echo -n "."
			apt-get install --reinstall -qq linux-image-$(uname -r) > /dev/null
			echo "OK"
		fi
		echo "A backup of the latest working sources left in /usr/src/madwifi-working.tar.gz"
	else
		echo "No MadWifi sources found in /usr/src/madwifi."
		echo "Cannot run uninstall scripts. Try to install first!"
	fi
	exit
fi

check_hw

if [ "$mode" = "--info" ]; then
	dbg_info
fi

if [ "$mode" = "--save" ]; then
	save_sources
fi

check_deps

if [ "$mode" = "--tarball" ]; then
	unroll_tarball
elif [ "$mode" = "--working" ]; then
	unroll_working
elif [ "$mode" = "--reinstall" ]; then
	cd /usr/src
	if [ -x madwifi ]; then
		cd madwifi
	else
		echo "MadWifi sources not found in /usr/src/madwifi, exiting..."
		exit
	fi
else
	cd /usr/src
	if [ -x madwifi ]; then
		svn_up
	else
		svn_co
	fi
fi

run_scripts
build_mw
install_mw
load_module
check_module

