#!/bin/bash
set -e

install_driver() {
	echo "Downloading the driver."
	if (wget -q http://downloads.fars-robotics.net/wifi-drivers/$driver\-drivers/$driver\-$kernel\-$build.tar.gz -O wifi-driver.tar.gz) ; then
		echo "Installing the driver."
		tar -xf wifi-driver.tar.gz
		sudo cp $driver.conf /etc/modprobe.d/.
		sudo install -p -m 644 $driver.ko /lib/modules/$kernel+/kernel/drivers/net/wireless
		sudo depmod $kernel+
		if (lsmod | grep $driver > /dev/null) ; then
			echo "A version of the driver $driver.ko is already loaded and running. You will need to reboot to load the new driver."
			exit
		else
			sudo modprobe $driver
		fi
		echo "Driver installed. We recommend a reboot."
	else
		echo
		echo "A driver does not yet exist for this kernel. Try again after an 'apt-get update && apt-get dist-upgrade'."
	fi
}

tmpdir=$(mktemp -d)
trap "\rm -rf $tmpdir" EXIT
cd $tmpdir

driver=8812au
kernel=$(uname -r | tr -d '+')
build=${build:-$(uname -v | awk '{print $1}' | tr -d '#')}
install_driver
exit 0
