#!/bin/bash

pcid64hu_inst()
{
	# Set default driver name
	OS=$1
	drv_name=9054

	# Default to non-service driver
	bServiceDriver=0

	# Verify superuser access
	if [ `id -u` != 0 ]; then
		echo
		echo "   ***************************************************"
		echo "   * NOTE: You must be superuser, logged in as root, *"
		echo "   *       or have sufficient rights to install      *"
		echo "   *       modules or this script will not work.     *"
		echo "   ***************************************************"
		echo
		echo "    ---- ERROR: Admin rights not detected, halting ----"
		echo
		#exit
		return 1
	fi

	# Verify insmod is found
	if [ -f /sbin/insmod ]; then
		InsMod=/sbin/insmod
	fi

	if [ -f /usr/sbin/insmod ]; then
		InsMod=/usr/sbin/insmod
	fi

	if [ "$InsMod" = "" ]; then
		echo "ERROR: 'insmod' not found in /sbin or /usr/sbin"
		echo
		return 1
		#exit
	fi

	# Path to the driver nodes
	path=/dev/plx

	# Registered name of driver
	name=Plx$drv_name

	# Name of module
	module=$name

	# Probe for a loaded conflicting driver
	drv_Name=`lsmod | awk "\\$1==\"$name\" {print \\$1}"`
	if [ "$drv_Name" = "" ]; then
		drv_Name=`lsmod | awk "\\$1==\"${name}_dbg\" {print \\$1}"`
	fi

	echo
	echo Install: $module

	echo -n "  Load module......... "
	# Check if conflicting driver already loaded
	if [ "$drv_Name" != "" ]; then
		echo "ERROR: '$drv_Name' conflicts & already loaded"
		echo
		#exit
		return 1
	fi

	# Verify driver file exists
	if [ ! -f ./driver/$OS/$module.ko ]; then
		echo "ERROR: Driver not built or invalid path"
		echo "         \-- File: $PLX_DIR/Driver/$OS/$module.ko"
		echo
		#exit
		return 1
	fi

	# Load module
	if $InsMod ./driver/$OS/$module.ko 2>/dev/null; then
		echo "Ok ($module.ko)"
	else
		echo ERROR: Load error or no supported devices found
		echo
		#exit
		return 1
	fi

	# Verify driver loaded
	echo -n "  Verify load......... "
	drv_Name=`lsmod | awk "\\$1==\"$module\" {print \\$1}"`

	if [ "$drv_Name" = "" ]; then
		echo ERROR: \'$module\' not detected
		echo
		#exit
		return 1
	fi
	echo Ok

	# Get the major number
	echo -n "  Get major number.... "
	major=`cat /proc/devices | awk "\\$2==\"$name\" {print \\$1}"`

	# Check if valid
	if [ "$major" = "" ]; then
		echo ERROR: Module major number not detected
		echo
		#exit
		return 1
	fi

	# Display Major ID
	echo "Ok (MajorID = $major)"

	# Create the device node path
	echo -n "  Create node path.... "
	if [ -d $path ]; then
		echo "Ok ($path already exists)"
	else
		mkdir $path
		chmod 0777 $path
		echo "Ok ($path)"
	fi

	# Create the device nodes
	echo -n "  Create nodes........ "
	rm -f $path/$name*
	mknod $path/$name c $major 255

	# Create additional nodes for non-service driver
	if [ "$bServiceDriver" = "0" ]; then
		mknod ${path}/$name-0  c $major 0
		mknod ${path}/$name-1  c $major 1
		mknod ${path}/$name-2  c $major 2
		mknod ${path}/$name-3  c $major 3
		mknod ${path}/$name-4  c $major 4
		mknod ${path}/$name-5  c $major 5
		mknod ${path}/$name-6  c $major 6
		mknod ${path}/$name-7  c $major 7
		mknod ${path}/$name-8  c $major 8
		mknod ${path}/$name-9  c $major 9
		mknod ${path}/$name-10 c $major 10
		mknod ${path}/$name-11 c $major 11
		mknod ${path}/$name-12 c $major 12
		mknod ${path}/$name-13 c $major 13
		mknod ${path}/$name-14 c $major 14
		mknod ${path}/$name-15 c $major 15
		mknod ${path}/$name-16 c $major 16
	fi

	chmod 777 $path/*
	echo "Ok ($path/$name)"
	echo
}

get_os_info() {
	if   [ -e /etc/debian_version ] || [ -e /etc/debian_release ]; then
		# Check Ubuntu or Debian
		if [ -e /etc/lsb-release ]; then
			# Ubuntu
			distri_name="ubuntu"
			distri_release=`cat /etc/lsb-release | grep RELEASE | cut -d= -f2`
		else
			# Debian
			distri_name="debian"
		fi
	elif [ -e /etc/fedora-release ]; then
		# Fedra
		distri_name="fedora"
	elif [ -e /etc/redhat-release ]; then
		if [ -e /etc/oracle-release ]; then
			# Oracle Linux
			distri_name="oracle"
		else
			# Red Hat Enterprise Linux
			distri_name="redhat"
		fi
	elif [ -e /etc/arch-release ]; then
		# Arch Linux
		distri_name="arch"
	elif [ -e /etc/turbolinux-release ]; then
		# Turbolinux
		distri_name="turbol"
	elif [ -e /etc/SuSE-release ]; then
		# SuSE Linux
		distri_name="openSUSE"
		distri_release=`cat /etc/SuSE-release | grep VERSION | cut -d= -f2`
	elif [ -e /etc/mandriva-release ]; then
		# Mandriva Linux
		distri_name="mandriva"
	elif [ -e /etc/vine-release ]; then
		# Vine Linux
		distri_name="vine"
	elif [ -e /etc/gentoo-release ]; then
		# Gentoo Linux
		distri_name="gentoo"
	else
		# Other
		echo "unkown distribution"
		distri_name="unkown"
	fi

	# Get OS bit and kernel version
	# 32bit : i686
	# 64bit : x86_64
	kver=`uname -r`
	kmod=`uname -m`
}

get_os_info

#echo ${distri_name} ${kver} ${kmod}

case ${distri_name} in
ubuntu)
	if [[ ${kmod} == "x86_64" ]]; then
        	echo 
	else
		echo   
		if [ ${distri_release} == "12.10" ] && [ ${kver} == "3.5.0-6-generic" ]; then
			pcid64hu_inst 'ubuntu/12.10/3.5.0-6-generic/32'
			#./drv_load 'ubuntu/12.10/32'
		else
			echo "The Ubuntu ${distri_release}(kernel ${kver}) unsupported"
		fi
	fi
	;;
openSUSE)
	if [[ ${kmod} == "x86_64" ]]; then
        	echo 
	else
		echo   
		if [ ${distri_release} == "12.1" ] && [ ${kver} == "3.1.0-1.2-default" ]; then
			pcid64hu_inst 'openSUSE/12.1/3.1.0-1.2-default/32'
		elif [ ${distri_release} == "12.1" ] && [ ${kver} == "3.1.0-1.2-desktop" ]; then
			pcid64hu_inst 'openSUSE/12.1/3.1.0-1.2-desktop/32'
		elif [ ${distri_release} == "12.1" ] && [ ${kver} == "3.1.10-1.29-desktop" ]; then
                        pcid64hu_inst 'openSUSE/12.1/3.1.10-1.29-desktop/32'
		else
			echo "The openSUSE ${distri_release}(kernel ${kver}) unsupported"
		fi
	fi
	;;
*)
	echo "The ${distri_name} unsupported"
	;;
esac
