#!/bin/bash

# delete the tmpporary files after installation
function delet_tmp() {
	rm cmdout
	rm tmp_*
}

# check kernel version
function summary() {
	cat cmdout | awk '{
	getline;
	for (i=1;i<10;i=i+2) {
		if ($i==$(i+1))
	{
		if (i==5) {
			print "1";
			break;
		}
	}	
		if ($i<$(i+1)) {
			print "2";
			break;
		}
		if ($i>$(i+1)) {
			print "3";
			break;
		}
	}
	}'
}

##################################################################################################
# function "compare" and "version summary" check if the kernel version of system supports the driver
function compare() {
	rm tmp_2.$$
	for ((i=1;i<4;i++)) {
		cat tmp_1.$$ | sed 's/\./ /g' | awk '{print $v}' v=$i | tr '\n' ' ' 1>>tmp_2.$$
	}
	cat tmp_2.$$ | tr '\n' ' ' > cmdout
	summary > tmp_1.$$
	kernel_v=$(cat tmp_1.$$)
	case $kernel_v in
		1) ;;
		2) ;;
		3) echo "Sorry, the kernel should be at least 2.6.32." ; delet_tmp; exit 1 ;;
	esac
}


function version_summary() {
	touch tmp_1.$$,tmp_2.$$					      # creat temporary files
	touch cmdout
	echo "Check the system.."
	# get distribution name of Linux
	release_ver=$(cat /etc/*-release)
	echo $release_ver
	Linux_dis="Unkown"
	case "$release_ver" in
		*Ubuntu*) Linux_dis="Ubuntu";; 
		*Fedora*) Linux_dis="Fedora";;
		"*Red Hat*") Linux_dis="Red Hat";;
		*Red" "Hat*) Linux_dis="Red Hat";;
		*CentOS*) Linux_dis="Red Hat";;
	esac
	uname -r | egrep -o '[0-9]\.[0-9]\.[0-9]{1,}' 1> tmp_2.$$     # get kernel version of system
	kernel_v=(cat tmp_2.$$)
	bits=$(uname -m)					      # get the number of bits of system
	if [ "$bits" != "x86_64" ] && [ "$bits" != "ia64" ]; then
		bits="i686"
	else
		bits="x86_64"
	fi
	echo "system is: $Linux_dis $bits"
	echo  2.6.32 > tmp_1.$$
	echo $kernel_v >> tmp_1.$$
	compare;
}
######################################################################################################
# insltall the libs necessary for the driver and adobe air in some Linux distributions.
function install_libs() {
	if [ "$bits" = "i686" ]; then
		if [ "$Linux_dis" = "Fedora" ] || [ "$Linux_dis" = "Red Hat" ]; then 
			if ! rpm -qa | grep -qw rpm-build; then
				echo "Installing rpm-build package." 
				sudo yum install rpm-build
				if [ $? -ne 0 ]; then
					echo "Failed. Ignore the error and Continue?(y/n)"
					read input_
					if [ "$input_" != "y" ]; then
						echo "Exit installation."
						delet_tmp;
						exit 1
					fi
				fi
			fi
		fi
	fi #i686

# install the libs necessary for 64bit system.
	if [ "$bits" = "x86_64" ]; then
		if [ "$Linux_dis" = "Ubuntu" ]; then		# install libs for Ubuntu 64bit
			if ! dpkg -l | grep -qw ia32-libs; then	
	                echo "The system is Ubuntu 64bit. We're installing the necessary 32bit libs. It may take serveral minutes."
			sudo apt-get install ia32-libs
			if [ $? -ne 0 ]; then 
				sudo apt-get install libc6:i386 libstdc++6:i386 libgtk2.0-0:i386 libnss3:i386 libxml2:i386 libxt6:i386 libxslt1.1:i386
				if [ $? -ne 0 ]; then # manually install failed
                                     sudo dpkg --add-architecture i386
                                     sudo apt-get update
				     sudo apt-get install libc6:i386 libstdc++6:i386 libgtk2.0-0:i386 libnss3:i386 libxml2:i386 libxt6:i386 libxslt1.1:i386
                                fi
                                if [ $? -ne 0 ]; then
					echo "Install 32bit libs failed. Please install them manually and then continue. The command here is:
'sudo apt-get install libc6:i386 libstdc++6:i386 libgtk2.0-0:i386 libnss3:i386 libxml2:i386 libxt6:i386 libxslt1.1:i386'
Continue now?(y/n)"
					read input 
					if [ "$input" != "y" ]; then 
						echo "Exit installation."
						delet_tmp;
						exit 1
					fi # input
				fi #manually install
			fi #install ia32-libs
			fi #dpkg query ia32-libs 
		fi

		if [ "$Linux_dis" = "Fedora" ]; then    		# install libs for Fedora 64bit
			fedora_ver=$(uname -r)
			if [[ "$fedora_ver" == *".fc19."* ]]; then
				echo "[ERROR]: Fedora 19 64bit is still not supported in this version. Press any key to exit."	
				read input
				exit 1
			fi
			echo "The system is Fedora 64bit. We're installing necessary 32bit libs. It may take several minutes."
			sudo yum install -y glibc.i686  ld-linux.so.2 gtk2-devel.i686 libdbus-glib-1.so.2 libhal.so.1 rpm-devel.i686 libXt.so.6 gnome-keyring-devel.i686 libDCOP.so.4 libxml2-devel.i686 nss-devel.i686 libxslt.i686 xterm rpm-build libgnome-keyring.i686 --setopt=protected_multilib=false
			if [ $? -ne 0 ]; then
				echo "Install necessary 32bit libs failed. Please install them manually and then continue. The command here is: 
'sudo yum install -y glibc.i686  ld-linux.so.2 gtk2-devel.i686 libdbus-glib-1.so.2 libhal.so.1 rpm-devel.i686 libXt.so.6 gnome-keyring-devel.i686 libDCOP.so.4 libxml2-devel.i686 nss-devel.i686 libxslt.i686 xterm rpm-build libgnome-keyring.i686 --setopt=protected_multilib=false'.
Continue now?(y/n)"
				read input_
				if [ "$input_" != "y" ]; then
					echo "Exit installation."
					delet_tmp;
					exit 1
				fi
			fi
		fi

		if [ "$Linux_dis" = "Red Hat" ]; then			# install libs for RedHat 64bit
			echo "The system is RedHat/CentOS 64bit. We're installing necessary 32bit libs."	
			sudo yum install -y glibc.i686  ld-linux.so.2 gtk2-devel.i686 libdbus-glib-1.so.2 libhal.so.1 rpm-devel.i686 libXt.so.6 gnome-keyring-devel.i686 libDCOP.so.4 libxml2-devel.i686 nss-devel.i686 libxslt.i686 xterm rpm-build libgnome-keyring.i686 --setopt=protected_multilib=false 2> tmp_1.$$
			cat tmp_1.$$ | grep -o "Error: Nothing to do"
			if [ $? -eq 0 ]; then
				echo "The system maybe evaluation version. Please upgrade system or use centOS, and then try again. "
				delet_tmp;
				exit 1
			fi
			echo "Installation of necessary 32bit libs finished, Continue?(y/n)"
			read input_
			if [ "$input_" != "y" ]; then
				echo "Exit installation."
				delet_tmp;
				exit 1
			fi
		fi #Red Hat
	fi #x86_64

	delet_tmp;
}

echo "prepare necessary libs..."
version_summary;
install_libs;
