#!/bin/bash
#
# Eastman Kodak
# i1100 Scanner Driver Install Script
#
#
# Error Codes:
# --------------------------------------------------------------------
# 0  success
#
# 1  printed usage/help info
# 2  must be run from root account
# 3  invalid command-line option
# 4  invalid operating system
# 5  hal is not installed, but is required (user must install it)
# 6  scanner was connected
#
# 10 did not accept EULA
# 11 did not want to upgrade OpenUSB and/or TWAIN
# 12 mono not installed and did not want to run without an UI
# 13 mono-winforms not installed and did not want to run without an UI
# 14 SANE not installed and did not want to run without a SANE driver
# 15 did not want to install alien and/or QT v3.x and/or libudev0 from internet
# 16 did not want to do install
#
# 20 QT v3.x could not be installed (no installer program)
# 21 lubudev0 could not be installed (no installer program)
# 22 mono-winforms could not be installed (no installer program)
#
# 30 no internet so alien and/or QT v3.x and/or libudev0 not installed
#
# 40 alien not there after install (install failed or internet failure)
# 41 QT v3.x installation error (install failed or internet failure)
# 42 libudev0 installation error (install failed or internet failure)
# 43 mono-winforms installation error (install failed or internet failure)




################################################################################
# Initialize key variables
################################################################################
strdistro=""        # What Linux distro are we running on?
installOpenusb=0    # Do we have to install OpenUSB?
removeOpenusb=0     # Do we need to remove an older version of OpenUSB?
halInstalled=0      # Is 'hal' installed?
needlibudev=1       # Do we need to try to install 'libudev'?
libudevInstalled=0  # Is 'libudev' installed?
needNewOpenUSB=0    # Does this system require the newer version of OpenUSB (0/1)?
installTwainDsm=0   # Do we have to install a new version of the TWAIN DSM?
removeTwainDsm=0    # Do we have to remove an older version of the TWAIN DSM?
qtinstalled=0       # Is the correct version of QT currently installed?
needwinforms=0      # Do we need to install a new version of mono-winforms?
needalien=0         # Does this distro use alien?
alienInstalled=0    # Is alien currently installed (Ubuntu only)?
alienpath=""        # What is the path to alien?
saneInstalled=0     # Is 'sane' installed?
dpkgpath=""         # What is the path to dpkg (Ubuntu-only)?
aptgetpath=""       # What is the path to apt-get (Ubuntu installer)?
yumInstalled=0      # Is yum installed (Fedora, RedHat installer software)?
yumpath=""          # Path to 'yum' (Fedora, RedHat installer).
zypperInstalled=0   # Is zypper installed (OpenSUSE installer software)?
zypperpath=""       # Path to 'zypper' (SUSE installer).
useInternet=0       # Do we have an installer on this distro that can access
                    # the internet?



################################################################################
# Installer Functions
################################################################################


# clean out the install directory, but leave install log
clean_instdir()
{
	# is there still a temp folder?
	if [ -d $instdir ]; then
		# clean out the temp directory, but leave any leftover install log
		for file in "$instdir"/*
		do
			if [ ! -f "$file" ] || [ "$file" != "$installlogfile" ]; then
				rm -rf "$file"
			fi
		done
	fi
}

# create install log and temp folder
install_init()
{
	# Figure out the temporary directory and copy all the packages there
	if [ "$TMPDIR" == "" ]; then
		TMPDIR=/tmp
	fi

	instdir="$TMPDIR"/kds_i1100

	# create name of install log
	installlogfile="$instdir"/install.log

	# make sure the folder is cleaned out (but leave and leftover install log)
	clean_instdir

	# make sure directory exists
	mkdir -p "$instdir"
}

# copy the install log to product specific one
install_end()
{
	scannerpath="/opt/kodak/kds_i1100"
	installedpath="$scannerpath/install"

	# if opt folder, then driver installed (At some point),
	# so copy the log file contents
	if [ -d $scannerpath ]; then
		if [ ! -d "$installedpath" ]; then
			# no install folder, so create it and just file
			mkdir -p "$installedpath"
			chmod 666 "$installedpath"
			cp "$installlogfile" "$installedpath/install.log"
			chmod 644 "$installedpath/install.log"
		else
			# need to append contents to the existing file
			cat "$installlogfile" >> "$installedpath/install.log"
		fi

		# done with install log, so remove everything
		# (don't want to append the log again)
		rm -rf "$instdir"
	else
		# get rid of everything except the install log
		# (want to try and append the data next install)
		clean_instdir
	fi

	exit $1
}

# check for command-line parameters (and they can be in any place)
check_commandline()
{
	# do we need to print the usage?
	if [ "$1" == "-h" ] || [ "$1" == "--h" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ] || [ "$1" == "-H" ] || [ "$1" == "--H" ]; then
		echo ""
		echo "Usage: $0 [--default] [--nostatus]"
		echo "       or"
		echo "       $0 [--silent]"
		echo ""
		echo "    --default       will automatically select default responses so the installation"
		echo "                    will continue. any problems will be echo'd to the terminal."
		echo ""
		echo "    --nostatus      will not echo any status messages to the terminal."
		echo ""
		echo "    --silent        nothing is echo'd to the terminal window and it will act as if"
		echo "                    --default was also on the command line. must use the return"
		echo "                    code to see if there is any error."
		echo "                    NOTE: package install status will be echo'd"
		echo ""
		exit 1
	fi

	echo_installfile ""
	echo_installfile "[*** `date` ***   command-line: $*]"

	# assume no bad arguments
	badargs=""

	# initialize all options, then parse the command line
	defaultinstall=0
	statusinstall=0
	silentinstall=0
	for cmdOpt in ${*:1}
	do
		if [ "$cmdOpt" == "--default" ]; then
			defaultinstall=1
		elif [ "$cmdOpt" == "--nostatus" ]; then
			statusinstall=1
		elif [ "$cmdOpt" == "--silent" ]; then
			silentinstall=1
		elif [ "$cmdOpt" != "" ]; then
			badargs="$badargs $cmdOpt"
		fi
	done

	# if any invalid optionarguments, then just exit.
	if [ "$badargs" != "" ]; then
		echo_silent ""
		echo_silent ""
		echo_silent "******************************"
		echo_silent "Invalid command-line options: $badargs"
		echo_silent "For list of options, type: $0 --help"
		echo_silent ""
		install_end 3
	fi
}

# echo line to the install log, even if the line was hidden by
# the user; that way we can see all the status.
# if the line is hidden, it will be prefixed with given charater
# in $1:
#    d - line hidden because --default is on
#    s - line hidden because --nostatus is on
#    * - line hidden because --silent is on
# 
lastlinehaven=0
echo_installfile()
{
	# if line hidden from user, then pull out the text into $origstring
	if [ "$1" == "d" ] || [ "$1" == "s" ] || [ "$1" == "*" ]; then
		lineprefix="$1 "
		origstring="${*:2}"
	else
		lineprefix=""
		origstring="$*"
	fi

	# depending on how we get called, the "-n" maybe the entire 1st
	# parameter OR it maybe the first part of the 1st parameter
	echowithn=0
	if [ "$origstring" == "-n" ]; then
		echowithn=1
		echostring="${origstring:2}"
	elif [ "${origstring:0:3}" == "-n " ]; then
		echowithn=1
		echostring="${origstring:3}"
	elif [ "${origstring:0:3}" == "-n." ]; then
		echowithn=1
		echostring="${origstring:3}"
	else
		echostring="$origstring"
	fi

	# if the last line did not include a new-line, then don't re-echo the prefix
	if [ $lastlinehaven == 1 ]; then
		lineprefix=""
	fi

	# echo with the "-n" if needed
	if [ $echowithn == 1 ]; then
		lastlinehaven=1
		echo -n "$lineprefix$echostring" >> "$installlogfile"
	else
		lastlinehaven=0
		echo "$lineprefix$echostring" >> "$installlogfile"
	fi
}

# This function will echo given all items if NOT in default mode
echo_default()
{
	if [ $defaultinstall == 1 ]; then
		echo_installfile "d" "$*"
		return
	fi

	# in case silent mode is also on, we need to check that
	echo_silent "$*"
}

# This function will echo given all items if NOT in status mode
echo_status()
{
	if [ $statusinstall == 1 ]; then
		echo_installfile "s" "$*"
		return
	fi

	# in case silent mode is also on, we need to check that
	echo_silent "$*"
}

# This function will echo given all items if NOT in silent mode
echo_silent()
{
	if [ $silentinstall == 1 ]; then
		echo_installfile "*" "$*"
		return
	fi
	echo_installfile "$*"

	# depending on how we get called, the "-n" maybe the entire 1st
	# parameter OR it maybe the first part of the 1st parameter
	if [ "$1" == "-n" ]; then
		echo -n "${*:2}"
		return;
	fi
	if [ "${1:0:3}" == "-n " ]; then
		echo -n "${1:3}"
		return;
	fi
	if [ "${1:0:3}" == "-n." ]; then
		echo -n "${1:3}"
		return;
	fi
	echo "$*"
}

# This function will read from the command-line and put into the given
# variable name if NOT in default mode; otherwise, it will set the
# given variable name so the 'default' is used
read_default()
{
	# see if given a default value
	readValue=$2
	if [ "$readValue" == "" ]; then
		readValue=use_default
	fi

	# if in default mode, then use default value
	if [ $defaultinstall == 1 ]; then
		eval $1=$readValue
		echo_installfile $readValue
		return
	fi

	# even if not in default mode, check to see
	# if in silent mode
	if [ $silentinstall == 1 ]; then
		eval $1=$readValue
		echo_installfile $readValue
		return
	fi

	read readsilenttmp
	eval $1=$readsilenttmp
	echo_installfile $readsilenttmp
}

# 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_default
	echo_default "Press the 'Enter' key to exit this installation..."
	read_default installOption
	install_end $1
}

# This function prints the 'completed message' and exits
print_completed_message()
{
	# Print the 'completed message' and exit
	echo_status ""
	echo_status ""
	echo_status "******************************"
	echo_status "Software installation is complete"
	echo_status ""
	install_end 0
}

# This function prints the 'not completed message' and exits
print_not_completed_message()
{
	# Print the 'not completed message' and exit
	echo_status ""
	echo_status ""
	echo_status "******************************"
	echo_status "Software installation has not been completed."
	echo_status ""
	install_end $1
}

# This function prints the 'cancelled message' and exits
print_cancelled_message()
{

	# Print the 'cancelled message' and exit
	echo_status ""
	echo_status ""
	if [ "$2" != "" ]; then		# if cancel message given, then print it always
		echo_silent "Cancelled: $2"
		echo_silent ""
	fi
	echo_status "******************************"
	echo_status "Software installation has been cancelled."
	echo_status "No software has been installed or updated."
	echo_status ""
	install_end $1
}

# This function prints the License Agreement and prompts
# the user to continue or exit.
print_license_agreement()
{
	echo_default "******************************"
	echo_default "EASTMAN KODAK COMPANY"
	echo_default "SOFTWARE LICENSE AGREEMENT"
	echo_default ""
	echo_default "Read the following terms and conditions carefully before"
	echo_default "using this Software.  Use of this Software indicates your"
	echo_default "acceptance of these terms and conditions.  If you do not"
	echo_default "agree with them, you should promptly return the package"
	echo_default "in its entirety for a full refund."
	echo_default ""
	echo_default "LICENSE"
	echo_default ""
	echo_default "Grant of License:  Eastman Kodak Company ('Kodak')"
	echo_default "grants you a license to use one copy of the enclosed"
	echo_default "software program(s) (the 'Software') subject to the"
	echo_default "license restrictions set forth below."
	echo_default ""
	echo_default "Restrictions on Use:  You may use the Software only on"
	echo_default "one computer at a time.  For each additional computer on"
	echo_default "which the Software is running at the same time, you will"
	echo_default "need an additional licensed copy of the Software.  You"
	echo_default "may copy the Software as necessary to enable you to"
	echo_default "use the Software as described above."
	echo_default ""
	echo_default "Transfer of software:  You may permanently transfer the"
	echo_default "Software to another party if the other party agrees to"
	echo_default "accept the terms and conditions of this license and you"
	echo_default "retain no copies of the Software."
	echo_default ""
	echo_default "Copyright:  The Software is owned by Kodak or its"
	echo_default "suppliers and protected by copyright laws and"
	echo_default "international treaties.  You may not copy the Software"
	echo_default "other than as expressly provided in this license.  You may"
	echo_default "not reverse engineer, decompile, or disassemble the"
	echo_default "Software.  If this Software is used within a country of the"
	echo_default "European Union, nothing in this Agreement shall be"
	echo_default "construed as restricting any rights available under the"
	echo_default "European Community Software Directive (91/250/EEC)."
	echo_default ""
	echo_default "Term:  This license is effective until terminated. You may"
	echo_default "terminate it at any time by destroying the Software"
	echo_default "together with all copies in any form.  It will also terminate if"
	echo_default "you fail to comply with any term or condition of this"
	echo_default "Agreement.  You agree upon such termination to destroy"
	echo_default "the Software together with all copies in any form."
	echo_default ""
	echo_default "LIMITED WARRANTY"
	echo_default ""
	echo_default "For a period of 90 days after the date of delivery of the"
	echo_default "Software to you, as evidenced by a copy of your"
	echo_default "purchase receipt, Kodak warrants (i) the Software will"
	echo_default "perform substantially in accordance with the"
	echo_default "accompanying documentation, and (ii) the media on"
	echo_default "which the Software is furnished will be free from defects"
	echo_default "in materials and workmanship under normal use."
	echo_default ""
	echo_default "Kodak does not warrant that the functions contained in"
	echo_default "the Software will meet your requirements or that the"
	echo_default "operation of the Software will be uninterrupted or error"
	echo_default "free.  You assume responsibility for operation of the"
	echo_default "Software to achieve your intended results, and for the"
	echo_default "installation, use, and results obtained from the Software."
	echo_default ""
	echo_default "Subject to any applicable legislation which prohibits the"
	echo_default "following exclusions, KODAK MAKES NO OTHER"
	echo_default "WARRANTIES OF ANY KIND, EITHER EXPRESS OR"
	echo_default "IMPLIED, INCLUDING THE IMPLIED WARRANTIES"
	echo_default "OF MERCHANTABILITY AND FITNESS FOR A"
	echo_default "PARTICULAR PURPOSE.  Some states and countries,"
	echo_default "including Australia, do not allow the exclusion of implied"
	echo_default "warranties, or have legislation that imposes certain"
	echo_default "statutory warranties that cannot be excluded, so the"
	echo_default "above exclusion may not apply to you.  This warranty"
	echo_default "gives you specific legal rights and you may also have"
	echo_default "other rights."
	echo_default ""
	echo_default "LIMITATIONS OF REMEDIES"
	echo_default ""
	echo_default "Subject to any applicable legislation which prohibits the"
	echo_default "following limitations, Kodak's entire liability and your"
	echo_default "exclusive remedy shall be, at Kodak's option either (a) the"
	echo_default "repair or replacement of the Software or any media not"
	echo_default "meeting Kodak's 'Limited Warranty' that is returned to"
	echo_default "Kodak or your dealer with a copy of your receipt, or (b)"
	echo_default "the return of the price you paid for the Software, provided"
	echo_default "you have proof of the purchase price you paid.  These"
	echo_default "remedies are not available if failure of the Software or"
	echo_default "media is the result of misuse, abuse, or a failure to follow"
	echo_default "the operating instructions in the accompanying written"
	echo_default "materials."
	echo_default ""
	echo_default "IN NO EVENT WILL KODAK OR ITS SUPPLIERS OR"
	echo_default "DEALERS BE LIABLE TO YOU FOR ANY INCIDENTAL"
	echo_default "OR CONSEQUENTIAL DAMAGES, INCLUDING ANY"
	echo_default "LOST PROFITS, LOST SAVINGS, OR OTHER"
	echo_default "DAMAGES ARISING OUT OF THE USE OR INABILITY"
	echo_default "TO USE THE SOFTWARE EVEN IF ADVISED OF THE"
	echo_default "POSSIBILITY OF SUCH DAMAGES.  Some states and"
	echo_default "countries, including Australia, do not allow the limitation or"
	echo_default "exclusion of liability for incidental or consequential"
	echo_default "damages, or have legislation which restricts the limitation"
	echo_default "or exclusion of liability, so the above limitation may not"
	echo_default "apply to you."
	echo_default ""
	echo_default "GENERAL"
	echo_default ""
	echo_default "If the Software was purchased in the United States, this"
	echo_default "Agreement is governed by the laws of the State of New"
	echo_default "York.  If purchased outside the United States, this"
	echo_default "Agreement is governed by the laws of the country in"
	echo_default "which it was purchased."
	echo_default ""
	echo_default "__________________________________________"
	echo_default "U.S. GOVERNMENT RESTRICTED RIGHTS"
	echo_default ""
	echo_default "The SOFTWARE and documentation are provided with"
	echo_default "RESTRICTED RIGHTS.  Use, duplication, or disclosure"
	echo_default "by the Government is subject to restrictions as set forth in"
	echo_default "subdivision (c)(1)(ii) of The Rights in Technical Data and"
	echo_default "Computer Software clause DFAR 252.227-7013 or such"
	echo_default "other applicable government or agency regulation"
	echo_default "providing equivalent protection.  Contractor /"
	echo_default "manufacturer is Eastman Kodak Company, 343 State"
	echo_default "Street, Rochester, New York, 14650."
	echo_default ""
	echo_default ""
	echo_default "******************************"
	echo_default "KODAK ScanMate i1120 Scanner v2.2"
	echo_default ""
	echo_default -n "Do you agree with the terms of the above software license agreement? (y/N) "
	read_default installOption Y		# force Yes when silent
	case "$installOption" in
		Y|y|YES|yes|Yes)
			# User wants to continue with the installation.
			;;
		n|N|NO|No|no)
			# User wants to cancel the install.
			print_cancelled_message 10 "license agreement not accepted"
			;;
		*)
			# Default to "no" - cancel out of the installer.
			print_cancelled_message 10 "license agreement not accepted"
			;;
	esac
	echo_default ""
}

# This function checks if tools are available to install from the internet
check_for_internet()
{
	if [ "$aptgetpath" != "" ] || [ $yumInstalled == 1 ] || [ $zypperInstalled == 1 ]; then
		useInternet=1
	fi
}

# This function checks if the correct version of QT is installed
check_qt_installed()
{
	# We need to see if the multithreaded QT libraries are 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
	qtinstalled=0
	qtmt=`find /usr/lib -name libqt-mt.so.3 -print 2>> /dev/null`
	if [ "$qtmt" != "" ]; then
		qtinstalled=1
	fi
}

# This function checks if mono-winforms is installed
check_mono_winforms_installed()
{
	# Check mono-winforms
	needwinforms=0
	monowinformsA=`find /usr/lib -name System.Windows.Forms.dll -print | grep '[2-9]\.[0-9]' 2>> /dev/null`
	monowinformsB=`find /usr/lib -name System.Windows.Forms.dll -print | grep '[3-9]\.[0-9]' 2>> /dev/null`
	if [ -z "$monowinformsA" ] && [ -z "$monowinformsB" ]; then
		# We didn't find a valid version of 'mono-winforms'
		# mono-winforms 2.0 not found...
		needwinforms=1
	fi
}

# This function checks if alien is installed
check_alien_installed()
{
	#
	# Check if Alien is installed
	#
	dpkgpath=`which dpkg 2>> /dev/null`
	needalien=0
	alienInstalled=0
	alienpath=""
	if [ "$dpkgpath" != "" ]; then
		needalien=1
		alienpath=`which alien 2>> /dev/null`
		if [ "$alienpath" != "" ]; then
			# Alien is already installed
			alienInstalled=1
		fi
	fi
}

# This function checks if sane is installed
check_sane_installed()
{
	# Check for folder '/etc/sane.d'
	saneInstalled=0
	if [ -f "/etc/sane.d/dll.conf" ]; then
		saneInstalled=1
	fi
}

# This function figures out if 'hal' is installed
check_hal_installed()
{
	# What distro are we running on (Ubuntu, other)?
	if [ "$strdistro" == "Ubuntu" ]; then
		# Ubuntu: use dpkg to check for install status.
		# Assume 'hal' is not installed.
		halInstalled=0
		needlibudev=1
		# If 'hal' has never been installed, we will get an empty string.
		tmpString1=`$dpkgpath -s hal 2>> /dev/null`
		if [ -n "$tmpString1" ]; then
			# 'hal' is installed, or has been removed from the system.
			# Check if it is currently installed.
			tmpString2=`$dpkgpath -s hal | grep -i 'Status: install' 2>> /dev/null`
			if [ -n "$tmpString2" ]; then
				# Found the string we were looking for. 'hal' is installed.
				halInstalled=1
				needlibudev=0
			fi
		fi
	else
		# For all other distros, use 'rpm -qa'
		tmpString=`rpm -qa | grep 'hal-[0-9]' 2>> /dev/null`
		halInstalled=0
		needlibudev=1
		if [ -n "$tmpString" ]; then
			halInstalled=1
			needlibudev=0
		fi
	fi
}

# This function figures out if 'libudev' is installed
check_libudev_installed()
{
	# What distro are we running on (Ubuntu, other)?
	if [ "$strdistro" == "Ubuntu" ]; then
		# Ubuntu: use dpkg to check for install status.
		# Assume 'libudev0' is not installed.
		libudevInstalled=0
		# If 'libudev0' has never been installed, we will get an empty string.
		tmpString1=`$dpkgpath -s libudev0 2>> /dev/null`
		if [ -n "$tmpString1" ]; then
			# 'libudev0' is installed, or has been removed from the system.
			# Check if it is currently installed.
			tmpString2=`$dpkgpath -s libudev0 | grep -i 'Status: install' 2>> /dev/null`
			if [ -n "$tmpString2" ]; then
				# Found the string we were looking for. 'libudev0' is installed.
				libudevInstalled=1
			fi
		fi
	else
		# For all other distros, use 'rpm -qa'
		tmpString=`rpm -qa | grep 'libudev0' 2>> /dev/null`
		libudevInstalled=0
		if [ -n "$tmpString" ]; then
			libudevInstalled=1
		fi
	fi
}


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


# Before we do ANYTHING, make sure they are running as root
if [ `id -u` != "0" ]; then
	echo ""
	echo ""
	echo "******************************"
	echo "You must be logged into the root account (i.e. super user) in order"
	echo "to install this scanner."
	echo ""
	exit 2
fi


# setup install log; this MUST be called before anything else (besides root check)
install_init

# check command line
check_commandline $*


################################################################################
# Introduction
################################################################################
echo_silent
echo_silent "KODAK ScanMate i1120 Scanner Installation"
echo_silent
print_license_agreement
echo_status
echo_status "This scanner uses OpenUSB and TWAIN Data Source Manager open source"
echo_status "software, which are already included in this package."
echo_status
echo_status "Source code can be downloaded from:"
echo_status "- OpenUSB: http://openusb.wiki.sourceforge.net"
echo_status "- TWAIN Data Source Manager: http://sourceforge.net/projects/twain-dsm"


################################################################################
# Verify Distribution
################################################################################

# What distro are we running on (Ubuntu, Fedora, SUSE, or RedHat)?
# For Ubuntu, check for "dpkg".
# For Fedora, SUSE, and RedHat, we'll look in /etc/*-release.

# First, check for Ubuntu.
strdistro=""
dpkgpath=`which dpkg 2>> /dev/null`
aptgetpath=`which apt-get 2>> /dev/null`
needalien=0
alienpath=""
if [ "$dpkgpath" != "" ]; then
	needalien=1
	strdistro="Ubuntu"
	alienpath=`which alien 2>> /dev/null`
else
	# Are we Fedora, RedHat, or SUSE?
	for distrotemp in Fedora SUSE RedHat
	do
		strtmpdistro=$(ls /etc/*-release | grep -i "$distrotemp")
		if [ -n "$strtmpdistro" ]; then
			strdistro="$distrotemp"
			break
		fi
	done
fi

# If we didn't find a valid Linux distribution name, leave now.
if [ -z "$strdistro" ]; then
	# Illegal distro
	echo_silent ""
	echo_silent ""
	echo_silent "******************************"
	echo_silent "This installation is not compatible with your operating system."
	exit_and_pause 4
fi

# Setup key variables depending on what distro we're running
#   - Ubuntu - we've already set the Ubuntu variables above
#   - Fedora, RedHat - check for 'yum'
#   - SUSE - check for 'zypper'
if [ "$strdistro" == "Fedora" ] || [ "$strdistro" == "RedHat" ]; then
	# Check if 'yum' is installed (Fedora, RedHat installer)
	yumInstalled=0
	yumpath=`which yum 2>> /dev/null`
	if [ -n "$yumpath" ]; then
		yumInstalled=1
	fi
fi
if [ "$strdistro" == "SUSE" ]; then
	# Check if 'zypper' is installed (OpenSUSE installer)
	zypperInstalled=0
	zypperpath=`which zypper 2>> /dev/null`
	if [ -n "$zypperpath" ]; then
		zypperInstalled=1
	fi
fi


################################################################################
# Check versions
################################################################################

if [ $silentinstall == 0 ]; then
	echo_status
	echo_status "Checking software versions, please wait..."

	# Status Message
	echo_status    "   Distribution:        $strdistro"
	echo_status -n "   Kernel:              "
fi

#
# Get the 'full' kernel release number
#
strfullkernel=$(uname -r)

# Extract the short kernel version (e.g. "2.6.30"):
#	- Starting at the left, look for the first character that
#	  isn't a numeric character or a "." character.
#	- Once we have that, take the substring leading up to that.
i=0
while (( i <= ${#strfullkernel} ))
do
	chartemp=${strfullkernel:$i:1}
	# Look for numeric character
	chartemp1=`expr match "$chartemp" '\([0-9]\)'`
	# Look for decimal point
	chartemp2=`expr match "$chartemp" '\([.]\)'`
	if [ -z "$chartemp1" ] && [ -z "$chartemp2" ]; then
		# Found a non-numeric character. Pull out the substring.
		strkernel=${strfullkernel:0:$i}
		break
	fi
	((i=i+1))
done

if [ $silentinstall == 0 ]; then
	# Status Message
	echo_status "v$strkernel"
fi


# Split the kernel version into its sections
IFS="."
kernelparts=( $strkernel )

#
# Check if SELinux is installed
# [If distro is Fedora or RedHat, assume 'yes']
#
seLinuxInstalled=0
if [ "$strdistro" == "Fedora" ] || [ "$strdistro" == "RedHat" ]; then
	chconpath=`which chcon 2>> /dev/null`
	if [ -n "$chconpath" ]; then
		seLinuxInstalled=1
		# Status Message
		echo_status "   SELinux:             installed"
	fi
fi


# Check if we are running with an 'old' kernel (pre 2.6.30), or 'new' kernel.
newKernel=0
if [ ${kernelparts[0]} -gt 2 ]; then
	# new kernel (v3.x or greater)
	newKernel=1
else
	if [ ${kernelparts[0]} == 2 ] && [ ${kernelparts[1]} -gt 6 ]; then
		# new kernel (v2.7 or greater)
		newKernel=1
	else
		if [ ${kernelparts[0]} == 2 ] && [ ${kernelparts[1]} == 6 ] && [ ${kernelparts[2]} -gt 29 ]; then
			# new kernel (v2.6.30 or greater)
			newKernel=1
		fi
	fi
fi

#
# Check if Alien is installed
#
check_alien_installed
missingalien=0
if [ $needalien == 1 ]; then
	echo_status -n "   alien:               "
	if [ $alienInstalled == 1 ]; then
		echo_status "installed"
	else
		echo_status "not installed"
		missingalien=1
	fi
fi

#
# We need to see if the multithreaded QT libraries are installed.
#
echo_status -n "   QT v3.x:             "
check_qt_installed
if [ $qtinstalled == 1 ]; then
	echo_status "installed"
else
	echo_status "not installed"
fi


#
# Check for mono (we need version 2.0 or higher with mono-winforms 2.0).
# This piece of script code will only be here if the driverui (TWAIN GUI)
# is present in the rpm file.
#
echo_status -n "   mono v2.0:           "
needmono=0
monojit=`which mono 2>> /dev/null`
if [ -n "$monojit" ]; then
	# Check for mono version 2.0 or higher
	monoverA=`$monojit -V | grep 'version [2-9]\.[0-9]' 2>> /dev/null`
	monoverB=`$monojit -V | grep 'version [3-9]\.[0-9]' 2>> /dev/null`
	if [ -z "$monoverA" ] && [ -z "$monoverB" ]; then
		# We didn't find a valid version of 'mono'
		needmono=1
	fi
else
	# mono 2.0 not found...
	needmono=1
fi

# Status Message
if [ $needmono == 0 ]; then
	echo_status "installed"
else
	echo_status "not installed"
fi

# Check mono-winforms. Only print the message if mono v2.0 is installed.
if [ $needmono == 0 ]; then
	echo_status -n "   mono-winforms v2.0:  "
	check_mono_winforms_installed
	if [ $needwinforms == 0 ]; then
		echo_status "installed"
	else
		echo_status "not installed"
	fi
fi

# Check if 'sane' is installed
echo_status -n "   SANE:                "
check_sane_installed
if [ $saneInstalled == 1 ]; then
	echo_status "installed"
else
	echo_status "not installed"
fi

#
# Check if 'hal' is installed
# (NOTE: If no 'hal' on an old distro (kernel < 2.6.30), then error)
#
check_hal_installed
echo_status -n "   hal:                 "
if [ $halInstalled == 1 ]; then
	echo_status "installed"
else
	# No "hal" installed. If old kernel (kernel < 2.6.30), then error...
	if [ $newKernel == 0 ]; then
		echo_status "not installed"
		echo_silent ""
		echo_silent ""
		echo_silent "******************************"
		echo_silent "We require 'hal' to be installed for this version of the kernel."
		echo_silent "NOTE: you will need to exit this installer and install the above"
		echo_silent "package yourself."
		exit_and_pause 5
	else
		echo_status "not installed (using libudev0)"
	fi
fi

#
# Check if 'libudev0' is installed.
#
missinglibudev=0
echo_status -n "   libudev0:            "
check_libudev_installed
if [ $libudevInstalled == 1 ]; then
	echo_status "installed"
else
	echo_status "not installed"
fi

#
# If we are using a newer kernel (kernel >= 2.6.30):
#    - if "udev" is installed, use the newer version of OpenUSB.
#    - if no "udev", but "hal" is installed, use the older version of OpenUSB.
#    - else set the flag to try and install udev later.
#
needNewOpenUSB=0
if [ $newKernel == 1 ]; then
	if [ $libudevInstalled == 1 ]; then
		# Newer kernal with "udev" installed. We need the new OpenUSB.
		needNewOpenUSB=1
	else
		# Newer kernel with no "udev" installed. Is "hal" installed?
		if [ $halInstalled == 0 ]; then
			# Newer kernel - no "hal" or "udev" installed:
			#   - set flag so we try to install udev later
			needNewOpenUSB=1
			missinglibudev=1
		fi
	fi
fi

#
# Determine if OpenUSB is already installed and if it's the correct version.
# For older distros with 'hal' installed, we need OpenUSB version 1.1.0.
# For newer distros using 'libudev0', we need OpenUSB version 1.1.11.
# If it is installed, but it's not the correct version, then remove it before
# installing.
echo_status -n "   OpenUSB:             "
installOpenusb=0
removeOpenusb=0
openusbFound=`find /usr -name libopenusb.so -print`
if [ -z "$openusbFound" ]; then
	# Didn't find any OpenUSB installations. Let's plan to install it.
	installOpenusb=1
else
	# We found an OpenUSB. Check if we need to remove it ("upgrade").
	if [ $needNewOpenUSB == 0 ]; then
		# We need the older version of OpenUSB ('hal'):
		#   We found an OpenUSB. However, due to inconsistent versioning, we can't
		#   tell if its old or new. So, mark for "upgrade" to version 1.1.0.
		removeOpenusb=1
		installOpenusb=1
	else
		# This machine requires the newer version of OpenUSB. Check the installed
		# version to see if it needs to be upgraded.
		openusbFoundA=`find /usr -name 'libopenusb.so\.[0-9]\.[0-9]\.[1-9][1-9]' -print`
		openusbFoundB=`find /usr -name 'libopenusb.so\.[1-9]\.*\.*' -print`
		if [ -z "$openusbFoundA" ] && [ -z "$openusbFoundB" ]; then
			removeOpenusb=1
			installOpenusb=1
		fi
	fi
fi

# Status Message
if [ $removeOpenusb == 1 ]; then
	echo_status "needs upgrade"
else
	if [ $installOpenusb == 1 ]; then
		echo_status "not installed"
	else
		echo_status "installed"
	fi
fi


# Determine if the TWAIN DSM is already installed and if it's version 2.2.1 or
# higher. If it is installed, but it's not version 2.2.1 or higher remove it
# before installing
echo_status -n "   TWAIN DSM:           "
installTwainDsm=0
removeTwainDsm=0
twainDsmFound=`find /usr -name libtwaindsm.so -print`
if [ -z "$twainDsmFound" ]; then
	installTwainDsm=1
else
	twainDsmFoundA=`find /usr -name 'libtwaindsm.so\.[2-9]\.[2-9]\.[1-9]' -print`
	twainDsmFoundB=`find /usr -name 'libtwaindsm.so\.[3-9]\.[0-9]\.[0-9]' -print`
	if [ -z "$twainDsmFoundA" ] && [ -z "$twainDsmFoundB" ]; then
		removeTwainDsm=1
		installTwainDsm=1
	fi
fi

# Status Message
if [ $removeTwainDsm == 1 ]; then
	echo_status "needs upgrade"
else
	if [ $installTwainDsm == 1 ]; then
		echo_status "not installed"
	else
		echo_status "installed"
	fi
fi


################################################################################
# Display the Upgrade Message, if appropriate
################################################################################
if [ $removeOpenusb == 1 ] || [ $removeTwainDsm == 1 ]; then
	# Prompt the user
	echo_default ""
	echo_default ""
	echo_default "******************************"
	echo_default "There are package(s) that are already on your computer that must be"
	echo_default "upgraded in order for this scanner to work. The following is a list"
	echo_default "of the package(s) and the version they will be upgraded to:"
	if [ $removeOpenusb == 1 ]; then
		# Which version to upgrade to? Depends on whether 'hal' is installed.
		if [ $needNewOpenUSB == 0 ]; then
			# 'hal': Install OpenUSB version 1.1.0 (older version)
			echo_default "- OpenUSB v1.1.0"
		else
			# No 'hal, using 'libudev'. Install OpenUSB version 1.1.11
			echo_default "- OpenUSB v1.1.11"
		fi
	fi
	if [ $removeTwainDsm == 1 ]; then
		echo_default "- TWAIN Data Source Manager v2.2.1"
	fi
	echo_default    ""
	echo_default -n "Do you want to upgrade the above package(s)? (Y/n) "
	read_default installOption
	case "$installOption" in
		Y|y|YES|yes|Yes)
			# User wants to upgrade. Proceed to the next step.
			;;
		n|N|NO|No|no)
			# User wants to skip the upgrade. Cancel the install.
			print_cancelled_message 11 "do not upgrade packages"
			;;
		*)
			# Default to "yes". User wants to upgrade.
			;;
	esac
fi


################################################################################
# Display the Mono Message(s)
################################################################################
skipUI=0
if [ $needmono == 1 ]; then
	# If distro has no Mono v2.0, user can only continue without the UI
	echo_default    ""
	echo_default    ""
	echo_default    "******************************"
	echo_default    "mono v2.0 is not on your computer (most likely it is not available"
	echo_default    "for your distribution). mono v2.0 is required by the TWAIN driver's"
	echo_default    "user interface (UI). However, the UI is not required in order to"
	echo_default    "scan. This means you can continue this installation, but the UI will"
	echo_default    "be disabled and you will need to rely on your scanning application"
	echo_default    "for scan setup."
	echo_default    ""
	echo_default -n "Do you want to continue this installation, without the UI? (y/N) "
	read_default installOption
	case "$installOption" in
		Y|y|YES|yes|Yes)
			# User wants to continue without the UI. Proceed to the next step.
			skipUI=1
			;;
		n|N|NO|No|no)
			# User wants to cancel the install.
			print_cancelled_message 12 "mono v2.0 not installed"
			;;
		*)
			# Default to "no". User does not want to continue.
			print_cancelled_message 12 "mono v2.0 not installed"
			;;
	esac
fi


if [ $needmono == 0 ] && [ $needwinforms == 1 ]; then
	check_for_internet
	if [ $useInternet == 1 ]; then
		# If distro has Mono v2.0 but does not have Mono-winforms v2.0, and is a distro that can
		# install packages, then let user decide whether to install winforms or not.
		echo_default    ""
		echo_default    ""
		echo_default    "******************************"
		echo_default    "The mono-winforms v2.0 package is not installed on your computer."
		echo_default    "This package is required by the TWAIN driver's user interface (i.e."
		echo_default    "UI). However, the UI is not required in order to scan. This means"
		echo_default    "you can skip installing this package if your scanning application"
		echo_default    "does not need the UI or you do not want to install this package. If"
		echo_default    "you do not install this package, the UI will be disabled and you"
		echo_default    "will need to re-run this installer in order to enable the UI."
		echo_default    ""
		echo_default    "NOTE: this installer will get the package from the internet, so you"
		echo_default    "must be connected to the internet or the installation will fail."
		echo_default    ""
		echo_default -n "Do you want to install mono-winforms v2.0? (Y/n) "
		read_default installOption
		case "$installOption" in
			Y|y|YES|yes|Yes)
				# User wants to install mono-winforms. Proceed to the next step.
				;;
			n|N|NO|No|no)
				# User wants to skip the mono-winforms install.
				needwinforms=0
				skipUI=1
				;;
			*)
				# Default to "yes". User wants to install mono-winforms and continue.
				;;
		esac
	else
		# If distro has Mono v2.0 but does not have Mono-winforms v2.0, and is a distro that we cannot
		# install packages, then let user decide whether to continue without the UI.
		echo_default    ""
		echo_default    ""
		echo_default    "******************************"
		echo_default    "The mono-winforms v2.0 package is not installed on your computer."
		echo_default    "This package is required by the TWAIN driver's user interface (i.e."
		echo_default    "UI). However, the UI is not required in order to scan. This means"
		echo_default    "you do not need to install this package if your scanning application"
		echo_default    "does not need the UI. In order to install this package, you will"
		echo_default    "need to exit this installer and install the package yourself."
		echo_default    ""
		echo_default    "NOTE: if you continue with this installation, the UI will be"
		echo_default    "disabled. To enable the UI, you will need to install mono-winforms"
		echo_default    "v2.0 and re-run this installer."
		echo_default    ""
		echo_default -n "Do you want to continue this installation, without the UI? (y/N) "
		read_default installOption
		case "$installOption" in
			Y|y|YES|yes|Yes)
				# User wants to continue without the UI.
				skipUI=1
				;;
			n|N|NO|No|no)
				# User wants to cancel out of the install.
				print_cancelled_message 13 "mono-winforms v2.0 not installed"
				;;
			*)
				# Default to "no". User wants to cancel out of the install.
				print_cancelled_message 13 "mono-winforms v2.0 not installed"
				;;
		esac
	fi
fi


################################################################################
# Display the Sane Message(s)
################################################################################
# Check if 'sane' is installed
check_sane_installed
if [ $saneInstalled == 0 ]; then
	# If the current distro has no 'sane' installed, ask the user if they
	# want to continue without it. If so, they can't scan using xsane.
	echo_default    ""
	echo_default    ""
	echo_default    "******************************"
	echo_default    "The SANE package is not installed on your computer. This package is"
	echo_default    "required by the scanner's SANE driver. However, you can scan using"
	echo_default    "the scanner's TWAIN driver. This means you can continue this"
	echo_default    "installation, but you will not have a SANE driver."
	echo_default    ""
	echo_default	"NOTE: if you install SANE later, you will need to re-run this"
	echo_default    "installer to get the SANE driver."
	echo_default    ""
	echo_default -n "Do you want to continue this installation, without the SANE driver? (y/N) "
	read_default installOption
	case "$installOption" in
		Y|y|YES|yes|Yes)
			# User wants to continue without sane. Proceed to the next step.
			;;
		n|N|NO|No|no)
			# User wants to cancel the install.
			print_cancelled_message 14 "SANE not installed"
			;;
		*)
			# Default to "no". User does not want to continue.
			print_cancelled_message 14 "SANE not installed"
			;;
	esac
fi


################################################################################
# Display the Prerequisites Message
################################################################################


#
# Ask the user what to install
#
if [ $missingalien == 1 ] || [ $qtinstalled == 0 ] || [ $missinglibudev == 1 ]; then
	check_for_internet
	if [ $useInternet == 1 ]; then
		# We are missing at least one required package. Are we on a distro where we can
		# install packages? If so, ask the user if they want to install them.
		echo_default    ""
		echo_default    ""
		echo_default    "******************************"
		echo_default    "The following package(s) are not installed on your computer and must"
		echo_default    "be installed in order for this scanner to work:"
		if [ $missingalien == 1 ]; then
			echo_default    "- alien (for converting RPM packages)"
		fi
		if [ $qtinstalled == 0 ]; then
			echo_default    "- QT v3.x"
		fi
		if [ $missinglibudev == 1 ]; then
			echo_default    "- libudev0"
		fi
		echo_default    ""
		echo_default    "NOTE: this installer will get the package(s) from the internet, so"
		echo_default    "you must be connected to the internet or the installation will fail."
		echo_default    ""
		echo_default -n "Do you want to install the above packages? (Y/n) "
		read_default installOption
		case "$installOption" in
			Y|y|YES|yes|Yes)
				# User wants to install the required packages.
				;;
			n|N|NO|No|no)
				# User wants to cancel out of the install.
				print_cancelled_message 15 "do not get alien/QT/libudev0 packages from internet"
				;;
			*)
				# Default to "yes" - install the required packages.
				;;
		esac
	else
		# If we cannot install packages on this distro, then tell the user to install them first
		# and re-run the installer.
		echo_silent    ""
		echo_silent    ""
		echo_silent    "******************************"
		echo_silent    "The following package(s) are not installed on your computer and must"
		echo_silent    "be installed in order to for this scanner to work:"
		if [ $missingalien == 1 ]; then
			echo_silent    "- alien (for converting RPM packages)"
		fi
		if [ $qtinstalled == 0 ]; then
			echo_silent    "- QT v3.x"
		fi
		if [ $missinglibudev == 1 ]; then
			echo_silent    "- libudev0"
		fi
		echo_silent    ""
		echo_silent    "NOTE: you will need to exit this installer and install the above"
		echo_silent    "package(s) yourself."
		exit_and_pause 30
	fi
fi


################################################################################
# Prompt for Install
################################################################################


# see if the scanner is not connected
# first check lsusb, then check lshal
scannerlist="forceonepassinwhileloop"
while [ "$scannerlist" != "" ]; do
	scannerlist=`lsusb 2>> /dev/null | grep ': ID 040a' 2>> /dev/null`
	if [ "$scannerlist" == "" ]; then
		scannerlist=`lshal 2>> /dev/null | grep '_40a_' 2>> /dev/null`
	fi

	if [ "$scannerlist" != "" ]; then
		echo_default    ""
		echo_default    ""
		echo_default    "******************************"
		echo_default    "Cannot install because a scanner is connected to the PC."
		echo_default    "Please unplug or turn off the scanner."
		echo_default    ""
		echo_default -n "Do you want to retry and then continue with the installation? (Y/n) "
		read_default installOption N		# abort if silent
		case "$installOption" in
			Y|y|YES|yes|Yes)
				# User wants to continue with the installation.
				;;
			n|N|NO|No|no)
				# User wants to cancel out of the install.
				print_cancelled_message 6 "scanner connected"
				;;
			*)
				# Default to "yes" - retry
				;;
		esac
	fi
done

# Display a confirmation message before installing/upgrading any software.
echo_default    ""
echo_default    ""
echo_default    "******************************"
echo_default    "The installation is ready to begin (no software was been installed yet)."
echo_default    ""
echo_default    "NOTE: scanner must remain unplugged or off during install"
echo_default    ""
echo_default -n "Do you want to continue with the installation? (y/N) "
read_default installOption Y
case "$installOption" in
	Y|y|YES|yes|Yes)
		# User wants to continue with the installation.
		;;
	n|N|NO|No|no)
		# User wants to cancel out of the install.
		print_cancelled_message 16
		;;
	*)
		# Default to "no" - cancel out of the installer.
		print_cancelled_message 16
		;;
esac


################################################################################
# Install alien
################################################################################
if [ $missingalien == 1 ]; then
	echo_status ""
	echo_status "Installing alien, please wait..."
	echo_status ""
	"$aptgetpath" install -y -qq alien
	sleep 1

	# Make sure the install succeeded
	check_alien_installed
	if [ $alienInstalled == 1 ]; then
		echo_status ""
		echo_status "alien installed successfully"
		echo_status ""
	else
		echo_silent ""
		echo_silent "alien installation FAILED"
		echo_silent ""
		echo_silent "The problem could be with your internet connection"
		echo_silent "or your PC's ability to install the package."
		exit_and_pause 40
	fi
fi

################################################################################
# Install QT
################################################################################
if [ $qtinstalled == 0 ]; then
	echo_status ""
	echo_status "Installing QT v3.x, please wait..."
	echo_status ""
	if [ $needalien == 1 ]; then
		"$aptgetpath" install -y -qq libqt3-mt
	else
		if [ $yumInstalled == 1 ]; then
			$yumpath -y -d1 install qt3
		else
			if [ $zypperInstalled == 1 ]; then
				$zypperpath -n install qt3
			else
				echo_silent ""
				echo_silent "Error - could not find an appropriate installer program!"
				echo_silent "Please install QT v3.x manually and run this setup script again."
				print_not_completed_message 20
			fi
		fi
	fi
	sleep 1
	
	# Check if the QT install succeeded
	check_qt_installed
	if [ $qtinstalled == 1 ]; then
		echo_status ""
		echo_status " QT v3.x installed successfully"
		echo_status ""
	else
		# CR3949: Ubuntu 12.10
		echo_default ""
		echo_default ""
		echo_default "******************************"
		echo_default "QT v3.x installation FAILED"
		echo_default ""
		echo_default "The problem could be with your internet connection"
		echo_default "or your PC's ability to install the package."
		echo_default ""
		echo_default ""
		echo_default "QT v3.x is required by the ScanTWAIN application. This means"
		echo_default "you can continue this installation if you will not use ScanTWAIN."
		echo_default ""
		echo_default -n "Do you want to continue this installation, without QT v3.x? (y/N) "
		read_default installOption
		case "$installOption" in
			Y|y|YES|yes|Yes)
				# User wants to continue without the ScanTWAIN. Proceed to the next step.
				#skipScanTWAIN=1
				;;
			n|N|NO|No|no)
				# User wants to cancel the install.
				exit_and_pause 41
				;;
			*)
				# Default to "no". User does not want to continue.
				exit_and_pause 41
				;;
		esac
	fi
fi

################################################################################
# Install 'libudev'
################################################################################
if [ $missinglibudev == 1 ]; then
	echo_status ""
	echo_status "Installing libudev0, please wait..."
	echo_status ""
	if [ $needalien == 1 ]; then
		"$aptgetpath" install -y -qq libudev0
	else
		if [ $yumInstalled == 1 ]; then
			$yumpath -y -d1 install libudev0
		else
			if [ $zypperInstalled == 1 ]; then
				$zypperpath -n install libudev0
			else
				echo_silent ""
				echo_silent "Error - could not find an appropriate installer program!"
				echo_silent "Please install 'libudev0' manually and run this setup script again."
				print_not_completed_message 21
			fi
		fi
	fi
	sleep 1
	
	# Check if the 'libudev' install succeeded
	check_libudev_installed
	if [ $libudevInstalled == 1 ]; then
		echo_status ""
		echo_status "libudev0 installed successfully"
		echo_status ""
	else
		echo_silent ""
		echo_silent "libudev0 installation FAILED"
		echo_silent ""
		echo_silent "The problem could be with your internet connection"
		echo_silent "or your PC's ability to install the package."
		exit_and_pause 42
	fi
fi

################################################################################
# Install mono-winforms
################################################################################
if [ $needwinforms == 1 ] && [ $skipUI == 0 ]; then
	echo_status ""
	echo_status "Installing mono-winforms, please wait..."
	echo_status ""
	if [ $needalien == 1 ]; then
		"$aptgetpath" install -y -qq libmono-winforms2.0-cil
	else
		if [ $yumInstalled == 1 ]; then
			$yumpath -y -d1 install mono-winforms
		else
			if [ $zypperInstalled == 1 ]; then
				$zypperpath -n install mono-winforms
			else
				echo_silent ""
				echo_silent "Error - could not find an appropriate installer program!"
				echo_silent "Please install mono-winforms manually and run this setup script again."
				print_not_completed_message 22
			fi
		fi
	fi
	sleep 1
	
	# Check if the mono-winforms install succeeded
	check_mono_winforms_installed
	if [ $needwinforms == 0 ]; then
		echo_status ""
		echo_status "mono-winforms installed successfully"
		echo_status ""
	else
		echo_silent ""
		echo_silent "mono-winforms installation FAILED"
		echo_silent ""
		echo_silent "The problem could be with your internet connection"
		echo_silent "or your PC's ability to install the package."
		exit_and_pause 43
	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

################################################################################
# Copy RPM Packages
################################################################################

# Print message
echo_status    ""
echo_status    ""
echo_status    "******************************"
echo_status    "Copying installation packages to temporary directory: $instdir"
echo_status    "Please wait..."
echo_status    ""

# Copy the packages to the temp directory
cp *.rpm "$instdir"
cp *.deb "$instdir" 2>> /dev/null

currentdir=`pwd`
cd "$instdir"

################################################################################
# Convert the RPM packages
################################################################################
# If we're on a Debian based system...
if [ $needalien == 1 ]; then
	# Print message
	echo_status    ""
	echo_status    ""
	echo_status    "******************************"
	echo_status    "Converting installation packages, please wait..."
	echo_status    ""

	# Delete the twaindsm*.rpm as we have a .deb for that
	rm -f twaindsm*.rpm

	# Convert the remaining RPM packages
	alien --scripts --to-deb *.rpm 2>> /dev/null
fi

################################################################################
# Install OpenUSB
################################################################################
# If we need to remove OpenUSB do it now
if [ $removeOpenusb == 1 ]; then
	# Print message
	echo_status    ""
	echo_status    ""
	echo_status    "******************************"
	echo_status    "Removing OpenUSB in order to perform the upgrade, please wait..."
	echo_status    ""

	# Note: ignore errors if we don't find the really old 'openusb'
	if [ $needalien == 1 ]; then
		$dpkgpath --force-all --purge openusb 2>> /dev/null
		$dpkgpath --force-all --purge libopenusb
	else
		rpm -e --nodeps openusb 2>> /dev/null
		rpm -e --nodeps libopenusb
	fi
fi

# Install OpenUSB, if necessary
if [ $installOpenusb == 1 ]; then
	# Print message
	echo_status    ""
	echo_status    ""
	echo_status    "******************************"
	echo_status    "Installing OpenUSB, please wait..."
	echo_status    ""
	if [ $needalien == 1 ]; then
		# Which version to install? Depends on if 'hal' is installed.
		# Ubuntu uses dpkg to install OpenUSB.
		if [ $needNewOpenUSB == 0 ]; then
			# 'hal' is installed: Install OpenUSB version 1.1.0
			$dpkgpath -i libopenusb*1.1.0*.deb
		else
			# No 'hal'. Install OpenUSB version 1.1.11
			$dpkgpath -i libopenusb*1.1.11*.deb
		fi
	else
		# Which version to install? Depends on if 'hal' is installed.
		# Other distros use rpm to install OpenUSB.
		if [ $needNewOpenUSB == 0 ]; then
			# 'hal' is installed: Install OpenUSB version 1.1.0
			rpm -Uvh --replacepkgs --nodeps libopenusb-1.1.0*.rpm
		else
			# No 'hal'. Install OpenUSB version 1.1.11
			rpm -Uvh --replacepkgs --nodeps libopenusb-1.1.11*.rpm
		fi
	fi
fi
	
# Clean up
if [ $needalien == 1 ]; then
	rm libopenusb*.deb
else
	rm libopenusb*.rpm
fi

################################################################################
# Install TWAIN DSM
################################################################################
# If we need to remove the TWAIN DSM do it now
if [ $removeTwainDsm == 1 ]; then
	# Print message
	echo_status    ""
	echo_status    ""
	echo_status    "******************************"
	echo_status    "Removing TWAIN Data Source Manager in order to perform the upgrade,"
	echo_status    "please wait..."
	echo_status    ""
	if [ $needalien == 1 ]; then
		$dpkgpath --force-all --purge twaindsm
	else
		rpm -e --nodeps twaindsm
	fi
fi

#Install the TWAIN DSM, if necessary
if [ $installTwainDsm == 1 ]; then
	# Print message
	echo_status    ""
	echo_status    ""
	echo_status    "******************************"
	echo_status    "Installing TWAIN Data Source Manager, please wait..."
	echo_status    ""
	if [ $needalien == 1 ]; then
		$dpkgpath -i twaindsm*.deb
	else
		rpm -Uvh --replacepkgs twaindsm*.rpm
	fi
fi

# Clean up
if [ $needalien == 1 ]; then
	rm twaindsm*.deb
else
	rm twaindsm*.rpm
fi

################################################################################
# Install Scanner Packages
################################################################################
# Print message
echo_status    ""
echo_status    ""
echo_status    "******************************"
echo_status    "Installing scanner packages, please wait..."
echo_status    ""
if [ $needalien == 1 ]; then
	# Install the remaining DEB packages
	$dpkgpath -i *.deb
else
	# Fedora, RedHat, or SUSE:
	rpm -Uvh --replacepkgs *.rpm
fi

# Everything is installed...

# If running without the UI (i.e. no mono), then delete the driver's UI and execute file
if [ $skipUI == 1 ]; then
	rm -f '/opt/kodak/kds_i1100/lib/twaingui.exe'
	rm -f '/opt/kodak/kds_i1100/lib/twaingui'
fi

# If we're running SELinux, then fix up Hippo to make SELinux happy.
if [ $seLinuxInstalled == 1 ]; then
	chcon -t textrel_shlib_t '/opt/kodak/kds_i1100/lib/hippo.so' 2>> /dev/null
fi

# Remove the temporary directory
cd "$currentdir"

print_completed_message

