#!/bin/sh
#
#######################################################################
# Shell script for loading the GSC driver modules during kernel booting
# pass the paramter "clear" to clear the log file for debugging.
#
# "module_name" below is set for the name of the .o file produced by
# the makefile.
#
# This script loads the driver, and if successful looks in the /proc
# file created by the driver to determine how many supported boards
# have been installed.  It then loops through and creates a node for
# each board.
#######################################################################

module_name=gsc24dsi32pll

#######################################################################

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

YELLOW="\033[33;1m"
GRAY="\033[37;m"
ERROR="${YELLOW}ERROR${GRAY}"

# wipe the log..useful for debug.

if [ $1 ] 
then
if [ $1 == "clear" ]
then
	echo "clearing log"
	cat /dev/null > /var/log/messages
fi
fi

# 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

os_ver=`uname -r | cut -d . -f 1-2`
echo "Loading on kernel version" $os_ver

# Compute the module file name.
if [ $os_ver == 2.6 ]
then
	module_file=`echo $0 | sed -e "s/[^/]*\$/${module_name}.ko/g"`
else
	module_file=`echo $0 | sed -e "s/[^/]*\$/${module_name}.o/g"`
fi

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
	chmod 666 /dev/${module_name}$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

