#!/bin/sh

###############################################################################
# seaioload:  This is a simple script to load the SeaIO driver module and 
# create device files using dynamically allocated device majors.
###############################################################################
MODULE="seaio"
MODDIR="/lib/modules/$(uname -r)/kernel/drivers/misc" ## CHANGE IF NECESSARY##
SUTILDIR="/usr/local/sbin"                            ## CHANGE IF NECESSARY##
CONFIGDIR="/etc"                                      ## CHANGE IF NECESSARY##

if [ -f "/sbin/modprobe" ]
then
	INSMOD="/sbin/modprobe"                       ## Worst case, if we ## 
elif [ -f "/sbin/insmod" ]                            ## can't find modprobe##
then                                                  ## we try insmod ##
	INSMOD="/sbin/insmod"
else
	echo
	echo "ALERT:"
	echo "Could not dectect modprobe or insmod"
	echo "Please edit the INSMOD variable in"
	echo "the seaioload script"
	echo
	exit 1
fi

MODNAME="SeaIO"

DEVNAME="dio"

GROUP="root"
MODE="666"

###############################################################################
# Make sure this is the root, or we will fail.
###############################################################################
if [ "$(id -u)" != "0" ]
then
	echo
	echo "ALERT:"
	echo "You must be root to load or unload kernel modules!"
	echo
	exit 1
fi

###############################################################################
# Attempt to figure out what type the module is.  
###############################################################################
if [ $INSMOD = "/sbin/modprobe" ]
then
	MODDIR="$MODULE"
elif [ -f ${MODDIR}/${MODULE}.o ]
then
	MODDIR="${MODDIR}/${MODULE}.o"
elif [ -f ${MODDIR}/${MODULE}.ko ]
then
	MODDIR="${MODDIR}/${MODULE}.ko"
else
	echo
	echo "ALERT:"
	echo "No module found installed in $MODDIR"
	echo "Either install it, or change the MODDIR"
	echo "variable to reflect the correct location."
	echo
	exit 1
fi

###############################################################################
# Unload the module if it's already loaded... otherwise don't do anything.
###############################################################################
${SUTILDIR}/seaiounload

###############################################################################
# Actually attempt to load the module, with options if they exist.
###############################################################################
if [ -f ${CONFIGDIR}/seaio.conf ] ; then
	. ${CONFIGDIR}/seaio.conf
	$INSMOD $MODDIR $SEAIO_OPTIONS || exit 1
else
	echo
	echo "ALERT:"
	echo "I've noticed you don't have a ${CONFIGDIR}/seaio.conf"
	echo "I will assume you are using modutils, and have" 
	echo "set up the proper aliases in /etc/modules.conf"
	echo "(or /etc/conf.modules, depending on your version)"
	echo "You may, at your option, remove this annoying"
	echo "message from the seaioload script, now that you"
	echo "know =)"
	echo
	echo
	$INSMOD $MODDIR || exit 1
fi

###############################################################################
# Remove Stale Nodes
###############################################################################
rm -f /dev/${DEVNAME}[0-3]

###############################################################################
# create device special files using the dynamically
# assigned device major number. device minor numbers
# start at 0. If you need more than 4 devices then
# copy the lines below and increment the file name
# numbers and the device minor numbers.
###############################################################################
MAJOR=`cat /proc/devices | awk "\\$2==\"$MODNAME\" {print \\$1}"`

## Prevents a failout, but doesn't do anything helpful
if [ -z "$MAJOR" ] ; then
	echo "ERROR: No SeaIO devices detected!"
	${SUTILDIR}/seaiounload
	exit 1
fi

mknod /dev/${DEVNAME}0 c $MAJOR 0
mknod /dev/${DEVNAME}1 c $MAJOR 1
mknod /dev/${DEVNAME}2 c $MAJOR 2
mknod /dev/${DEVNAME}3 c $MAJOR 3

#give appropriate group/permissions
chgrp ${GROUP} /dev/${DEVNAME}[0-3]
chmod ${MODE}  /dev/${DEVNAME}[0-3]
