#!/bin/sh
###############################################################################
# Program:
# 		MOXA Linux driver installer
#	
# History:
# 2009/09/11
#
# Author:
# Ken Huang, ken.huang@moxa.com
#
# Discription:
# The script install and load the MOXA Linux driver.
#
###############################################################################

TARGET_DRIVER="mxuport"
MXVER=`awk '{if($1=="Version" && $2=="Number:"){print $3}}' version.txt`

for DRIVER in $TARGET_DRIVER
do
	if lsmod | grep -qw $DRIVER ; then
		rm -f tmp1 tmp2
		echo "Found driver in system..."
		echo "Unloading driver..."
		rmmod $DRIVER > tmp1 2>&1
		if [ -s tmp1 ]
		then
			cat tmp1
			grep "is in use" tmp1 > tmp2 2>&1
			if [ -s tmp2 ]
			then
				rm -f tmp1 tmp2
				exit 1
			fi
			rm -f tmp2
		fi
		rm -f tmp1
		echo "Unload driver successfully."
		echo ""
	fi
done

if make install ; then
	echo ""
	echo "Loading driver..."
	modprobe $TARGET_DRIVER
	echo "************************************************************************"
	echo " MOXA UPort 1200/1400/1600 series driver ver $MXVER loaded successfully."
	echo "************************************************************************"
fi
