#!/bin/bash

##Simple bash script to build the VirtualBox kernel module for
#VectorLinux by easuter

KMOD_DIR="/lib/modules/`uname -r`/misc"

#Compile and install kernel module function
function compile_install_kmod {

echo "
--> Compiling kernel module...
"
make -j3 2>/dev/null

if ( ! make ); then
	echoc "FAILED" red
	echo "Make sure you have the kernel source code installed"
   	echo "and configured!"
	exit
fi

echoc "OK" green
echo "
--> Installing kernel module..."

if [ ! -d $KMOD_DIR ]; then
	mkdir -p $KMOD_DIR
fi

if [ ! -f vboxdrv.ko ]; then
	echoc "FAILED" red
	echo "Can't find module"
	exit
fi

chown root:root vboxdrv.ko
cp vboxdrv.ko $KMOD_DIR

depmod -a
modprobe vboxdrv
sleep 1
chmod 777 /dev/vboxdrv
echoc "OK" green

echoc "
Kernel module built and installed!" green

}

##The actual script starts here
#Check for user id:

if [ $UID != 0 ]; then
	echoc "You must be root to run this script!" red
	exit
fi

clear

#Check if kernel source package is installed
echo "
--> Checking for kernel source package..."

if [ -z `ls /var/log/packages | grep kernel-src` ]; then
	echoc "FAILED \n" red
	echo "Kernel source package doesn't seem to be installed."
	echo "Compiling the VirtualBox kernel module may fail,"
	echo "Do you wish to proceed anyway? (y/n)"
	echo "\n answer: "

	read input
	case $input in
		y | Y | yes | YES ) compile_install_kmod;;
		n | N | no | NO ) exit;;
		* ) echo "Answer y or n"
	esac
else
	echoc "OK" green
	compile_install_kmod
fi

