#!/bin/bash

pilote="** UNKNOWN VERSION (please report your working driver version and your lshal) **"
refsys=`lshal | grep "system.*product" | tr '\n' ' '`
medion=`lshal | grep "system" | grep -i "MEDION" | wc -l`
f191=`lsusb | grep -i "05e3:f191" | wc -l`
if [ $f191 -gt 0 ]; then pilote="F191"; fi
if [ $medion -gt 0 ]; then pilote="0503 sim"; fi
if [ "${refsys/37E/}" != "$refsys" ]; then pilote="0503 ms"; fi
if [ "${refsys/[sS]37[sS]/}" != "$refsys" ]; then pilote="0503 a"; fi
if [ "${refsys/C90/}" != "$refsys" ]; then pilote="0503 c"; fi
if [ "${refsys/[Zz]37[sS]/}" != "$refsys" ]; then pilote="0503 c"; fi
refsys=`lshal | grep "system.*product" | tr '\n' ' '`

echo
echo "Did you use the '$pilote' ?"
echo

# Let's check for kernel source or headers
KERNEL_VERSION=$(uname -r)
# Now let's check if there is a version of gl860 running
GL860_PRESENT=$(lsmod | grep ^gl860 | wc -l)
# Verify if we have a 05e3:f191
f191=`lsusb | grep -i "05e3:f191" | wc -l`

if [ -e /lib/modules/$KERNEL_VERSION/build ]; then
	echo "*****************************************************************************"
	echo " 1/10) [Kernel headers for current kernel installed]"
else
	echo " 1/10) [Kernel headers for current kernel missing] Please install them using the package manager of your distribution. For Debian/Ubuntu systems, look for the linux-headers package. Make sure the headers version matches your running kernel version"
	exit 1
fi
echo

# Now let's check for make
if [ -f /usr/bin/make ]; then
	echo " 2/10) [Make is present]"
#    echo "Compilers look OK"
else
	echo " 2/10) [Compilers not found] Please install them. On Debian/Ubuntu install the build-essential package."
	exit 1
fi

echo "*****************************************************************************"
if [ -e /lib/modules/$KERNEL_VERSION/kernel/drivers/media/video/gl860.ko ]; then
    # We also remove the module from the hard drive
	echo " 3/10) [Deleting old module...]"
	echo " $ sudo rm -fr /lib/modules/$KERNEL_VERSION/kernel/drivers/media/video/gl860.ko"
	sudo rm -fr /lib/modules/$KERNEL_VERSION/kernel/drivers/media/video/gl860.ko
else
	echo " 3/10) [No old module...] Nothing to do"
fi
echo

if [ $GL860_PRESENT == 1 ]; then
    echo " 4/10) [Removing running instance of the gl860 driver...]"
    echo " $ sudo modprobe -r gl860 "
    sudo modprobe -r gl860 

	if [ "$f191" -eq 0 ]; then
	 echo
	 echo -e "\\033[31;1mIf your previous driver did not work  YOU HAVE TO SHUTDOWN YOUR COMPUTER."
	 echo "Former driver use may result in a malfunction of the webcam if you test the good driver."
	 echo -e "Please shutdown your computer and run once again $0.\\033[0;0m"
	 echo 
	 echo "Would you like to shutdown ? [yes / anything else]"
	 read reponse
	 if [ "$reponse" = "yes" ]; then
	 	echo " $ sudo shutdown -h now"
		sudo shutdown -h now
	 fi
	fi
else
    echo " 4/10) [No gl860 loaded] Let's go on"
fi
echo

# Let's clean up from previous compilations
echo " 5/10) [Clean up from previous compilations]"
echo " $ make -f Makefile.standalone clean"
make -f Makefile.standalone clean

echo "*****************************************************************************"
echo " 6/10) [Generation of the code source...]"

lng_fr=`locale | grep -o "=fr_"`
if [ "$lng_fr" != "" ]; then echo "#define _FRANCAIS_" > gl860.h; fi

cat base_gl860.h >> gl860.h
chmod a-w gl860.h

# Now let's compile the driver
echo
echo " 7/10) [Compiling driver...]"
echo " $ make -f Makefile.standalone"
make -f Makefile.standalone || exit 2

# Now we copy the driver where it belongs
echo "*****************************************************************************"
echo " 8/10) [Copying the driver to the modules location...]"
echo " $ sudo cp gl860.ko /lib/modules/$KERNEL_VERSION/kernel/drivers/media/video/gl860.ko"
sudo cp gl860.ko /lib/modules/$KERNEL_VERSION/kernel/drivers/media/video/gl860.ko

# We create the module's dependencies
echo
echo " 9/10) [Creating module dependencies, this may take a while...]"
echo " $ sudo depmod -a /lib/modules/$KERNEL_VERSION/kernel/drivers/media/video/gl860.ko"
sudo depmod -a /lib/modules/$KERNEL_VERSION/kernel/drivers/media/video/gl860.ko

# Finally we insert the driver in the kernel
echo
echo "10/10) [Inserting the module...]"
echo " $ sudo modprobe gl860"
sudo modprobe gl860

echo "*****************************************************************************"
echo "Compiling relog.c..."
make relog -f Makefile.standalone

# Let's tell the user everything looks ok and to try the camera!
echo
echo -en "\\033[31m"
echo "Now please try Camorama or Cheese. Please refer to the README file!"
echo "If you are testing "a" or "b" and have an image,"
echo "please run  ./shakedown  and report what works and what doesn't work."
echo
echo "If the driver does not work, test another version (even not intended for your laptop)."
echo "Run once again  $0  to remove the driver and shutdown the computer."
echo "At the restart, run  $0  and the driver will be compiled and installed."
echo -en "\\033[0m"
echo

exit 0
