# /*
# Copyright (c) 2007, Intel Corp.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#    * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# */
#!/bin/bash
currentpath=$(pwd)
rpmbuildresidence=$(whereis rpmbuild)
rpmresidence=$(whereis rpm)
iommapalive=$(/sbin/lsmod | grep iommap)
iosize=${#iommapalive}
kernelrpm=$(ls /usr/src/linux*/kernel/fork.c | grep fork.c | grep No)
kernelrpmsize=${#kernelrpm}
endl="  See <installdir>/iommapdrvr/iommap.txt for help on installing the driver."

############################################################################
# check to see if the driver is loaded.                                    #
# if driver version less than 2.00.1.0803, install new driver              #
############################################################################
if [ "$iosize" -ne  "0" ]
then
	DRVVERSION=""
	MAJORVERSION=""
	MINORVERSION=""	

	mv *.so* ..
		
	if [ -e /proc/iommap ]
	then
		DRVVERSION=$(cat /proc/iommap | sed -n '/version_number=/s/.*version_number=//p;/(/s/(.*//p;')
		echo "Driver version is $DRVVERSION"
	elif [ -e /proc/iMTA/iommap ]
	then
		DRVVERSION=$(cat /proc/iMTA/iommap | sed -n '/version_number=/s/.*version_number=//p;/(/s/(.*//p;')
		DRVVERSION=$(echo $DRVVERSION | sed -n '/ (/s/ (.*//p;')
		echo "Driver version is $DRVVERSION"
	else
		DRVVERSION=""
		echo "**NOTE**Iommap proc file system is missing."
	fi

	if [ "$DRVVERSION" != "" ]
	then
		MAJORVERSION=$(echo $DRVVERSION | sed -n '/\../s/\..*//p;')
		echo "Iommap major version is $MAJORVERSION"
		if [ "$MAJORVERSION" = "1" ]
		then
			echo "**NOTE**Iommap driver is currently running."
			echo "**NOTE**But it is running at old driver version."
		else
			echo "**NOTE**Iommap driver is currently running."
			echo $endl
			echo ""
			exit
		fi
	fi
	
	echo "**NOTE**New Iommap driver version will be installed."
	echo "**NOTE**New Iommap driver upgrade is in progress."
fi

############################################################################
# verify the kernel source is installed                                    #
# without the kernel source we can't compile the driver                    #
# if we can't compile the driver we can try to install                     #
# the driver via binary rpms                                               #
############################################################################
if [ "$kernelrpmsize" -ne "0" ]
then
	echo "**NOTE**Kernel source is not installed."
	echo $endl
	echo ""
	exit
fi

############################################################################
# verify gcc is available                                                  #
# if it is available then we can build the driver                          #
# and install it                                                           #
############################################################################
if [ -e /usr/bin/gcc ]
then
	if [ -e /usr/src/linux/.config ]
	then
		echo "found config file" 1>/dev/null 
	else
		cd /usr/src/linux 2>/dev/null
		make oldconfig 1>/dev/null 2>/dev/null 
		cd $currentpath 2>/dev/null
	fi	
	
	 
	if [ "$rpmbuildresidence" = "rpmbuild:" ]
	then
		echo "rpmbuild not available" 1>/dev/null 
		
		if [ "$rpmresidence" = "rpm:" ]
		then
			echo "rpm not available" 1>/dev/null
			echo "**NOTE**RPM and RPMBUILD are not available"
			echo "**NOTE**Will not attempt to build driver"
			echo ""
		else
			rpm --rebuild *.src.rpm 1>/dev/null 2>/dev/null
		fi	 
	else
		echo "rpmbuild installed" 1>/dev/null
		rpmbuild --rebuild *.src.rpm 1>/dev/null  2>/dev/null
	fi
	
	mkdir ./driver archive 2>/dev/null 
	mv *.src.rpm ./driver archive 2>/dev/null

	iommapalive=$(lsmod | grep iommap)	
	iosize=${#iommapalive}
	
	if [ "$iosize" -ne "0" ]
	then
		echo "**STATUS**Iommap installed successfully"  
		mv *.so* ..
		echo ""
		exit
	else
		echo "**STATUS**Iommap could not be installed." 
		echo $endl
		echo ""
		exit 
	fi

else
	echo "**NOTE**The GCC compiler is not installed."
	echo $endl
	echo ""
	exit 
fi



