#!/bin/sh
#
# Itek Vulcan CL drivers

cd $(dirname $0)
KERNEL_PATH=`uname -r`
DEST_DIR=/lib/modules/$KERNEL_PATH/kernel/drivers/char/
MODULE=vulcancl

echo "Installing Itek Vulcan-CL Board drivers..."

# Check if root previlige
if [ `id -u` -ne 0 ]; then
    echo "Please install as root."
    exit 1
fi

# Check if the system configuration will allow loading the drivers
if [ -e /etc/sysconfig/hardware/config ]; then 
    if [ ! "`grep LOAD_UNSUPPORTED_MODULES_AUTOMATICALLY=yes /etc/sysconfig/hardware/config`" ]; then
        echo "The system is not configured to automatically load the drivers."
        echo
        echo "Please replace the line LOAD_UNSUPPORTED_MODULES_AUTOMATICALLY=no in "
        echo "/etc/sysconfig/hardware/config by LOAD_UNSUPPORTED_MODULES_AUTOMATICALLY=yes."
        exit 1
    fi
fi

# Check existence of kernel sources (tree header is enough)
if [ ! -e /lib/modules/$(uname -r)/build/Makefile ]; then
    echo "/lib/modules/$(uname -r)/build/Makefile does not exist."
    echo "Please install kernel sources header."
    exit 2
fi

# Check if required development tools are installed
make --version > /dev/null 2>&1
if [ $? != 0 ]; then
    echo "make is not installed."
    echo "Please install make."
    exit 3
fi

echo "Building drivers..."
if ! make -j6
then
    echo "Building modules FAILED!"
    exit 5
fi

cp -f $MODULE.ko $DEST_DIR 

if cat /etc/modules | grep "$MODULE" > /dev/null 2>&1
then
    echo "$MODULE exist in /etc/moudles"
else
    echo $MODULE >> /etc/modules
fi


# Insert drivers
echo "Inserting driver modules..."
/sbin/depmod
/sbin/modprobe $MODULE

make clean > /dev/null 2>&1
echo "Installation $MODULE finished!"
