# /*
# 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
#

TARGET=iommap.o
TARGET_OUTPUT=/lib/modules/`uname -r`/iMTA/iommap
DRVVERSION=""

if [ $# != 1 ]; then
	echo "Usage: $0 [install/uninstall]"
	echo "     : $0 install"
	echo "     : $0 uninstall"
	exit 0
fi

if [ $1 != "install" ] && [ $1 != "uninstall" ]; then
	echo "Usage: $0 [install/uninstall]"
	echo "     : $0 install"
	echo "     : $0 uninstall"
	exit 0	
fi

############################################
# Check Kernel version					   #
############################################
KERNELVER=$(cat /proc/version|sed -n '/Linux/s/.*version 2.6.*/KERNEL_2_6/p;')
if [ "$KERNELVER" != "KERNEL_2_6" ]; then
	KERNELVER=$(cat /proc/version|sed -n '/Linux/s/.*version 2.4.*/KERNEL_2_4/p;')
	if [ "$KERNELVER" != "KERNEL_2_4" ]; then
		KERNELVER=UNDEF
	else
		KERNELVER=KERNEL_2_4
	fi
else
	KERNELVER=KERNEL_2_6
fi

############################################
# Check OS Distribution					   #
############################################
#OSVER=$(cat /proc/version|sed -n '/SuSE/s/.*\(SuSE\).*/SUSE/p;')
OSVER =$(ls /etc/SuSE-release|sed -n '/SuSE/s/.*\/SuSE-release.*/SUSE/p;')
if [ "$OSVER" != "SUSE" ]; then
	#OSVER=$(cat /proc/version|sed -n '/Red Hat/s/.*\(Red Hat\).*/REDHAT/p;')
	OSVER =$(ls /etc/redhat-release|sed -n '/redhat/s/.*\/redhat-release.*/REDHAT/p;')
	if [ "$OSVER" != "REDHAT" ]; then
		OSVER =$(ls /etc/mvl-release|sed -n '/mvl/s/.*\/mvl-release.*/MVL/p;')			
		if [ "$OSVER" != "MVL" ]; then
			OSVER=$(cat /proc/version|sed -n '/Gentoo/s/.*\(Gentoo\).*/GENTOO/p;')
			if [ "$OSVER" != "GENTOO" ]; then
				OSVER=UNDEF
			else
				OSVER=GENTOO
			fi
		else
			OSVER=MVL
		fi
	else
		OSVER=REDHAT
	fi
else
	OSVER=SUSE
fi

############################################
# Start driver installation 			   #
############################################
echo "Installing driver:(iommap) and service to the default runlevel ..."
echo "Creating default Major/Minor number in /etc/iommapd.conf configuration files..."
echo "MAJOR=253" > /etc/iommapd.conf
echo "MINOR=0" >> /etc/iommapd.conf
echo "OSVER=$OSVER" >> /etc/iommapd.conf
echo "KERNELVER=$KERNELVER" >> /etc/iommapd.conf
echo "AUTO_DETECT=YES" >> /etc/iommapd.conf
chmod 644 /etc/iommapd.conf

if [ "$KERNELVER" == "KERNEL_2_4" ]; then
	TARGET=iommap.o
fi

if [ "$KERNELVER" == "KERNEL_2_6" ]; then
	TARGET=iommap.ko
fi

################################################
# Install iommap driver for driver version	   #
################################################
insmod $TARGET

if [ -e /proc/iommap ]; then
	cat /proc/iommap
	DRVVERSION=$(cat /proc/iommap | sed -n '/version_number=/s/.*version_number=//p;/(/s/(.*//p;')
	DRVVERSION=$(echo $DRVVERSION | sed -n '/ (/s/ (.*//p;')
	echo $DRVVERSION
fi

if [ -e /proc/iMTA/iommap ]; then
	cat /proc/iMTA/iommap
	DRVVERSION=$(cat /proc/iMTA/iommap | sed -n '/version_number=/s/.*version_number=//p;/(/s/(.*//p;')
	DRVVERSION=$(echo $DRVVERSION | sed -n '/ (/s/ (.*//p;')
	echo $DRVVERSION
fi

################################################
# Check whether to uninstall iommap driver	   #
################################################
if [ $1 = "uninstall" ]; then

	/etc/init.d/iommapd unlink

	rm -f /etc/iommapd.conf

	rm -f /etc/init.d/iommapd

	rm -f /usr/bin/iommap.o
	rm -f /usr/bin/iommap.ko	
	rm -f /usr/bin/iommap.txt
	rm -f /usr/bin/iommap.chm
	
	rm -f $TARGET_OUTPUT/iommap.o
	rm -f $TARGET_OUTPUT/iommap.ko
	rm -f $TARGET_OUTPUT/iommap.txt
	rm -f $TARGET_OUTPUT/iommap.chm

	rm -rf $TARGET_OUTPUT/$DRVVERSION/

	echo "****************************************"
	echo "* Successfully uninstall iommap driver *"
	echo "****************************************"

	exit 0
fi

################################################
# Create iommap driver installation directory  #
################################################
if [ ! -d $TARGET_OUTPUT/ ]; then
	mkdir -p $TARGET_OUTPUT/
fi 

if [ ! -d $TARGET_OUTPUT/$DRVVERSION ]; then
	mkdir -p $TARGET_OUTPUT/$DRVVERSION
fi 

################################################
# Install iommap driver to the right directory #
################################################
cp -f $TARGET $TARGET_OUTPUT/$DRVVERSION/$TARGET
chmod 644 $TARGET_OUTPUT/$DRVVERSION/$TARGET
ln -sf $TARGET_OUTPUT/$DRVVERSION/$TARGET $TARGET_OUTPUT/$TARGET

cp -f iommap.o $TARGET_OUTPUT/$DRVVERSION/iommap.o
chmod 644 $TARGET_OUTPUT/$DRVVERSION/iommap.o

cp -f iommap.txt $TARGET_OUTPUT/$DRVVERSION/iommap.txt
cp -f iommap.chm $TARGET_OUTPUT/$DRVVERSION/iommap.chm
chmod 644 $TARGET_OUTPUT/$DRVVERSION/iommap.txt
chmod 644 $TARGET_OUTPUT/$DRVVERSION/iommap.chm
ln -sf $TARGET_OUTPUT/$DRVVERSION/iommap.txt $TARGET_OUTPUT/iommap.txt
ln -sf $TARGET_OUTPUT/$DRVVERSION/iommap.chm $TARGET_OUTPUT/iommap.chm

cp -f iommapd /etc/init.d/iommapd
chmod 755 /etc/init.d/iommapd

################################################
# Reinstall iommap driver					   #
################################################
/etc/init.d/iommapd link
/etc/init.d/iommapd restart

################################################
# Update iommap driver configuration file	   #
################################################
echo "Updating Major number in iommapd.conf configuration files..."
if [ -e /proc/iommap ]; then
	grep "MAJOR=" /proc/iommap > /etc/iommapd.conf
	grep "MINOR=" /proc/iommap >> /etc/iommapd.conf
fi

if [ -e /proc/iMTA/iommap ]; then
	grep "MAJOR=" /proc/iMTA/iommap > /etc/iommapd.conf
	grep "MINOR=" /proc/iMTA/iommap >> /etc/iommapd.conf
fi

echo "OSVER=$OSVER" >> /etc/iommapd.conf
echo "KERNELVER=$KERNELVER" >> /etc/iommapd.conf
echo "AUTO_DETECT=YES" >> /etc/iommapd.conf
chmod 644 /etc/iommapd.conf

/etc/init.d/iommapd reload
echo "****************************************"
echo "* Successfully installed iommap driver *"
echo "****************************************"
