#!/bin/bash

#Kernel Versions that the modules were compiled for:
VER1=2.4.0

#Get the current Kernel Version
# (it's the 3rd word on the line that says 'Linux version')
KVERSION=`awk '/Linux version/ {print $3}' /proc/version`

#make sure we work with the current kernel version
if [ "$KVERSION" = "$VER1" ]
then
	echo "Using Module for kernel version $KVERSION."
	MODNAME="backpack-$KVERSION.o"
else
	echo "ERROR: your kernel is not a compatible version."
	exit 1
fi

#check for SMP and warn if they are using it
if grep SMP /proc/version > /dev/null
then
	echo "WARNING: this module is not tested with an SMP Kernel."
	echo "         you are running an SMP Kernel."
fi

#make a directory for the module in the right place
mkdir -p /lib/modules/$KVERSION/misc
if cp -f $MODNAME /lib/modules/$KVERSION/misc/backpack.o
then
#check wheather kernel has version info here
	if [ "$RPMBUILD" = "1" ]
	then
# if we're in an RPM build make a file list
		echo "/lib/modules/$KVERSION/misc/backpack.o" >rpmfilelist
	fi
	if [ "`grep printk /proc/ksyms | awk '{print $2}'`" != "printk" ]
	then
#kernel has version info
		echo "Kernel has version info.. you might get warnings"
                echo "They may be safely ignored"
#is there a conf.modules file?
		if [ -f /etc/conf.modules ]
		then
#update it for working with version info
			echo "altering /etc/conf.modules.."
                	echo "saving old version in /etc/conf.modules.old"
			mv /etc/conf.modules /etc/conf.modules.old
			grep -v backpack /etc/conf.modules.old > /etc/conf.modules
			echo "pre-install pd modprobe backpack" >> /etc/conf.modules
			echo "pre-install pcd modprobe backpack" >> /etc/conf.modules
			echo "pre-install pf modprobe backpack" >> /etc/conf.modules
			echo "pre-install pt modprobe backpack" >> /etc/conf.modules
			echo "pre-install pg modprobe backpack" >> /etc/conf.modules
                fi
	fi
#update the module dependencies
	if depmod -a > /dev/null
	then
		echo "Driver sucessfully installed"
		exit 0
    	else
		echo "Something failed.."
		echo "Please look at the README and try the manual install"
		exit 1
	fi
	echo "Could not copy new driver into proper location"
	echo '(Are you root? You need to be)'
	echo "Please look at the README and try the manual install"
	exit 1
fi
