#!/bin/bash
#
# Eastman Kodak Company
# Document Imaging Scanner Driver Install Script
#

# This function prompts the user to press enter and waits until they do
# before exiting. The only argument is the optional error code for exit
exit_and_pause()
{
	# Exit the script and use 'read' to pause
	echo
	echo "Press \"Enter\" To Exit..."
	read
	exit $1
}

# This function prompts the user to tell them the TWAIN GUI will be disabled
print_gui_disabled()
{
	# Let the use know the GUI will be disabled
	echo "The TWAIN UI requires mono and mono-winforms version 1.2.6 or higher."
	echo ""
	echo "You can continue to install the KODAK scanner software, but the TWAIN UI"
	echo "will not be available. Otherwise you need to: 1) exit this installer; "
   	echo "2) install mono and mono-winforms 1.2.6 or greater; and 3) run this"
   	echo "installer again".
   	echo ""
   	echo "If you continue with this install and then later want to use the TWAIN"
   	echo "UI, you will need to run this installer again."
   	echo ""	
	echo "Press \"Enter\" to continue and not have the TWAIN UI available..."
	read
}


################################################################################
# Installer Main Body
################################################################################

# We need to find out:
#  1) If we're running as root
#  2) If we're running on a Debian based distribution

# 1) If we're running as root
runasroot=0
if [ `whoami` == "root" ]; then
	runasroot=1
fi

# 1) If we're running on a Debian based distribution
#	 We're going to try and make this simple by looking for "apt-get", we'll 
#	 assume that we are not a Debian based distribution if it's not there. This
#	 is far from foolproof, but should be "good enough"
dpkgpath=`which dpkg 2>> /dev/null` 
needalien=0
if [ "$dpkgpath" != "" ]; then
	needalien=1
fi
alienpath=`which alien 2>> /dev/null`


#
# Installer Main Body
#
# Splash Message
echo
echo
echo "KODAK Scanner Installation"
echo 
echo "This script will install ALL of the software on this CD."
echo
echo "Press \"Enter\" to continue..."
read

# Make sure we're root
if [ $runasroot == 0 ]; then
	echo "You must be root to install software!"
	exit_and_pause 1
fi

if [ $needalien == 1 ]; then
	echo "Your distribution appears to use the Debian package manager"
	echo "The RPM packages included on this CD will be converted using alien"
	echo ""
	if [ "$alienpath" == "" ]; then
		echo "Alien does not appear to be installed. Use apt-get install alien to"
		echo "to install this package."
		echo
		echo "Installation Failed!"
		exit_and_pause 1
	fi
fi


# We need to see if the multithreaded QT3 library is installed
# we do this with find rather than using the package manager since we don't
# know for sure which distro we're running on
echo "KODAK Scanner software requires QT v3.x. Searching for QT v3.x. Please wait..."
qt3mt=`find /usr/lib -name libqt-mt.so.3 -print 2>> /dev/null`
if [ "$qt3mt" == "" ]; then
	echo 
	echo "QT v3.x Not Found!"
	echo 
	echo "You must have the multithreaded QT v3 libraries installed."
	echo "Please install this package and run this setup script again."
	exit_and_pause 1
fi
echo "QT v3.x Detected!"
echo

# Check for mono (we need version 1.2.6 or higher with mono-winforms 2.0). The 
# rpm scripts contain almost exactly this same code and will endable the GUI if 
# the version we need is found. This is here just to let the user know whether
# or not the GUI will be enabled. If the GUI is not in rpm package, don't bother,
# we know we have rpm installed at this point even on Ubuntu because we've already
# checked for alien, which requires rpm.
isguiinrpm=`rpm -qlp *.rpm | grep twaingui 2>> /dev/null`
if [ "$isguiinrpm" != "" ]; then
	echo "Searching for mono and mono-winforms 1.2.6. Please wait..."
	monojit=`which mono 2>> /dev/null`
	if [ "$monojit" != "" ]; then
		monowinforms2=`find /usr/lib -name System.Windows.Forms.dll -print | grep '2\.[0-0]' 2>> /dev/null`
		monover=`$monojit -V | grep 'version 1\.2\.[6-9]' 2>> /dev/null`
		if [ "$monover" == "" ]; then
			# check for version 1.3+
			monover=`$monojit -V | grep 'version 1\.[3-9]' 2>> /dev/null`
		fi
		if [ "$monover" == "" ]; then
			# Check for version 2 or higher
			monover=`$monojit -V | grep 'version [2-9]\.[0-9]' 2>> /dev/null`
		fi
		if [ "$monover" == "" ]; then
			print_gui_disabled
		else
			if [ "$monowinforms2" == "" ]; then
				print_gui_disabled
			else
				echo "mono and mono-winforms 1.2.6 or greater Detected!"
				echo
			fi
		fi
	else
		print_gui_disabled
	fi
fi

# We need to make sure the /usr/local/lib is in the ld.so.conf
if ! grep "/usr/local/lib" /etc/ld.so.conf > /dev/null; then
	echo "/usr/local/lib" >> /etc/ld.so.conf
fi

# On newer distros we need to link libdbus-1.so.2 to libdbus-1.so.3. We need this
# until OpenUSB gets built for more than one distro
if [ -f /usr/lib/libdbus-1.so.3 ]; then
	ln -s /usr/lib/libdbus-1.so.3 /usr/lib/libdbus-1.so.2 2>> /dev/null
fi
if [ -f /lib/libdbus-1.so.3 ]; then
	ln -s /lib/libdbus-1.so.3 /lib/libdbus-1.so.2 2>> /dev/null
fi

# If we're on a Debian based system, check for alien
if [ $needalien == 1 ]; then
	if [ "$TMPDIR" == "" ]; then
		echo "TMPDIR is not defined, using /tmp"
		echo
		TMPDIR=/tmp
	fi

	# Create the temporary directory and copy the RPM packages
	echo "Copying and Converting Packages..."
	instdir=$TMPDIR/kds_inst
	mkdir -p $instdir
	cp *.rpm $instdir
	cp *.deb $instdir

	# Delete the twaindsm*.rpm as we have a .deb for that
	rm -f $instdir/twaindsm*.rpm
	
	# Convert the RPM packages
	currentdir=`pwd`
	cd $instdir
	alien --scripts --to-deb *.rpm 2>> /dev/null
	
	# Remove the original dsm, we can force this since we know we're
	# about to install it
	$dpkgpath --force-all --purge twaindsm  2>> /dev/null

	# Install the DEB packages
	echo "Installing Packages..."
	$dpkgpath -i *.deb

	# Remove the temporary directory
	cd "$currentdir"
	rm -rf $instdir

	# Done!
	exit_and_pause 0

else
	echo "Installing Packages..."

	# Remove the original dsm, we can force this since we know we're
	# about to install it
	rpm -e --nodeps twaindsm 2>> /dev/null

	# This is the normal RPM install path
	rpm -Uvh --replacepkgs --nodeps *.rpm

	# Done!
	exit_and_pause 0
fi
