#!/bin/bash
# this script will install module and present it to system ( /dev/... )
# because my driver only supports one physical device and four logic partition,
# our name-rule is /dev/sdmmca, /dev/sdmmca1~4.
# where 'a' in the string,sdmmca, means first physical device
# if your driver support two, add 'b' in the same step if you want
#
# the input argu is your module name
# example1 : sh load sdmmc
# example2 : sd load msmspro
#
# cheng tsair-jinn <jinncheng@skymedo.com.tw>
#############################################################################
module=$1
dev_name=$1
dev_extend=a
module_file=$1
mode=664
############################################################################
#
#
if test -f $module_file
then
	echo "file exist"
else
	echo "file not exist"
	exit 0
fi
############################################################################
# Group selection
#
if grep '^staff:' /etc/group > /dev/null; then
	group=staff
else
	group=wheel
fi
#############################################################################
# install module 
#
#
#`/sbin/lsmod | awk "if \[ \\$1=\"$module\" \] then { echo "module already exist" } { exit 0 } fi"`
#

result=`/sbin/insmod ./$module_file`
if $result
then
	echo "insmod "${module_file}" ok"
else 
	echo "insmod "${module_file}" fail"
	exit 0
fi
#############################################################################
# get major number and report its value
#
major=`cat /proc/devices | awk "\\$2==\"$module\" {print \\$1}"`
echo "major name is :" $module
echo "major num  is :" $major
#############################################################################
# remove old device name
#
rm -f /dev/${dev_name}${dev_extend}[0-4] /dev/${dev_name}${dev_extend}
#############################################################################
# create new device name
#
for p in "" 1 2 3 4; do
	n=$p; if [ "$n" = "" ]; then n=0; fi
	mknod /dev/${dev_name}${dev_extend}${p} b $major $n
done
#############################################################################
# change directory attribution
#
chgrp $group /dev/${dev_name}${dev_extend}[0-4] /dev/${dev_name}${dev_extend}
chmod $mode  /dev/${dev_name}${dev_extend}[0-4] /dev/${dev_name}${dev_extend}
#############################################################################
# finally, report installed device status
#
ls -l /dev | grep ${dev_name}
exit 1
