#!/bin/bash

# Use only with kernel 2.6
# Load the APCI-3600 and create the device node 
# under /dev/apci3600 and the minor number of the board.

DRIVER_NAME="apci3600"
BOARD_NAME="apci3600"
MODULE_NAME="$BOARD_NAME.ko"
VENDOR_DEVICE_ID="15b8300f"

# checking privileges
if [ `id -u` -ne 0 ];
then
	echo "$0: You must have root privileges to run this script shell"
	exit 1
fi

# Test the kernel version
if [ `uname -r | cut -d'.' -f 2` -ne 6 ];
then
	echo "$0: This script is only for kernel 2.6.X"
	exit 1
fi

# Surch the number of apci-3600 in the computer
NBOARDS=$(grep -c $VENDOR_DEVICE_ID /proc/bus/pci/devices)

# Board found?
if [ $? -eq 1 ]; then
	echo "Fail to detectd $BOARD_NAME"
	exit 1
fi

if [ $NBOARDS -eq 0 ]; then
	echo "No $BOARD_NAME detected"
	exit 1
fi

echo "$NBOARDS $BOARD_NAME detected"

cat /proc/modules | grep $BOARD_NAME > /dev/null

if [ $? -eq 0 ]; then
	rmmod $MODULE_NAME > /dev/null
fi

insmod $MODULE_NAME

# Search the major number
MAJOR=$(grep $DRIVER_NAME /proc/devices | cut -f 1 -d\ )

DIR="/dev/$BOARD_NAME"

echo creating directory "$DIR"
mkdir -p "$DIR"
cd "$DIR"

# Create nodes
i=0
j=0
while [ $i -lt $NBOARDS ]; do

	for j in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
	do
		MINOR=$(( ($i * 14) + $j ))
		if [ -e "$DIR/$MINOR" ]; then
			rm "$DIR/$MINOR"
		fi

		mknod $DIR/$MINOR c $MAJOR $MINOR
		chmod 666 $DIR/$MINOR
		ls -al $DIR/$MINOR
	done
	i=$((i+1))
done

exit 0





