#!/bin/sh
# This software is covered by the GNU GENERAL PUBLIC LICENSE (GPL).
# HPDI32 - Driver - Linux - gcc
# $Workfile:$
# $Revision:$
# $Date:$
#
# Shell script for loading the HPDI32 driver module during kernel booting
#

module_name=hpdi32

#######################################################################
#
# Introduction.
#

echo "Loading module ${module_name} ... "

YELLOW="\033[33;1m"
GRAY="\033[37;m"
ERROR="${YELLOW}ERROR${GRAY}"
KERNELVER=`uname -r | cut -d . -f1-2`

# Make sure the module name is validly set.
if [ -z ${module_name} ]
then
	echo -e ${ERROR}: invalid variable \"module_name\": ${module_name}
	exit 1
fi

# Make sure we're running the script in a satisfactory manner.
script=`echo $0 | sed -e "s/[^/]*\///g"`

if [ ".${script}" != ".${module_name}_start" ]
then
	echo -e ${ERROR}: invalid invocation
	exit 1
fi

# Compute the module file name.

if [ "${KERNELVER}" != "2.6" ]
then
	EXT="o"
else
	EXT="ko"
fi

module_file=`echo $0 | sed -e "s/[^/]*\$/${module_name}.${EXT}/g"`

if [ `echo $0 | grep "^\/" | wc -l` -le 0 ]
then
	module_file=`pwd`/${module_file}
fi

# Make sure the module file exists.
if [ ! -f ${module_file} ]
then
	echo -e ${ERROR}: module file does not exist: ${module_file}
	exit 1
fi

#######################################################################
#
# Utility functions.
#

# function to see if the module is loaded
check_loaded() {
	lsmod | grep -w ${module_name} | wc -l
}

# function to see if the module is loaded
check_running() {
	cat /proc/devices | grep -w ${module_name} | wc -l
}

# Get the device's major number.
get_major_number() {
	cat /proc/devices | grep -w ${module_name} | cut -d" " -f1
}

# Count the number of existing device nodes.
count_device_nodes() {
	ls -al /dev/${module_name}-* 2> /dev/nul | wc -l
}

#######################################################################
#
# Remove any existing device nodes.
#

rm -f /dev/${module_name}-*

if [ `count_device_nodes` -gt 0 ]
then
	echo -e ${ERROR}: unable to remove existing device nodes.
	exit 1
fi

#######################################################################
#
# Unload the current module to make sure the latest gets loaded.
#

if [ `check_loaded` -gt 0 ]
then
	rmmod ${module_name}
fi

if [ `check_loaded` -gt 0 ]
then
	echo -e ${ERROR}: unable to unload module.
	exit 1
fi

#######################################################################
#
# Load the existing module.
#

insmod ${module_file}

# Make sure the module got loaded.
if [ `check_loaded` -le 0 ]
then
	echo -e ${ERROR}: unable to load module.
	exit 0
fi

#######################################################################
#
# Get the major device number.
#

if [ `check_running` -le 0 ]
then
	echo -e ${ERROR}: module is not running.
	exit 1
fi

major_no=`get_major_number`

if [ $major_no -le 0 ]
then
	echo -e ${ERROR}: invalid major number: $major_no
	exit 1
fi

#######################################################################
#
# See how many boards are installed.
#

# Make sure the proc file exists
if [ ! -f /proc/${module_name} ]
then
	echo -e ${ERROR}: file does not exist: /proc/${module_name}
	exit 1
fi

# Get the board count from the proc file.
board_count=`grep "^boards: [0-9]*$" /proc/${module_name} | cut -d" " -f2`
board_count=`expr 0${board_count}`

# Make sure the number of boards is valid

if [ ${board_count} -le 0 ]
then
	echo -e ${ERROR}: file /proc/${module_name} is invalid
	exit 0
fi

#######################################################################
#
# Install a node for each installed board.
#

count=0

while [ $count -lt ${board_count} ]
do
	mknod /dev/${module_name}-$count c $major_no $count
	count=`expr $count + 1`
done

if [ `count_device_nodes` -ne ${board_count} ]
then
	echo -e ${ERROR}: failed to install the required number of device nodes.
	exit 1
fi

#######################################################################
#
# We're done.
#

exit 0

