#!/bin/bash
#
# The Dropline Installer
#
# Copyright 2002-2003 Todd Kulesza
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

VERSION=2.0.5
CACHE=/var/cache/dropline-installer
NODEPS="False"
UPGRADE="False"
AUTO="False"
EXIT="0"
DroplineFilesVersion=DroplineFiles2b

declare MIRROR
declare DROPLINE_FILES
declare -a DroplineFiles

DEP_APPS="dialog
grep
md5sum
perl
sed
wget"

DEP_LIBS="libz.so.1
libjpeg.so.62
libpng.so.3
libtiff.so.3
libgtk-1.2.so.0
libgdk-1.2.so.0
libgmodule-1.2.so.0
libglib-1.2.so.0
libcdda_paranoia.so.0
libcdda_interface.so.0"

DIALOG=dialog
DIALOG_OK=0
DIALOG_CANCEL=1

tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15

# disable console blanking since this process can take a while...
setterm -blank 0

OLD_UMASK=`umask`
if [ "$OLD_UMASK" != "0022" ]; then
	umask -S 0022 &> /dev/null
fi

if [ ! -d $CACHE ]; then
	mkdir -p $CACHE
fi

if [ "`whoami`" != "root" ]; then
	echo ""
	echo "Error: You must be ROOT to run the Dropline Installer.  Exiting..."
	echo ""
	
	umask -S $OLD_UMASK  &> /dev/null
	exit 1
fi

if [[ -n $1 && $1 != "" ]]; then
	if [ "$1" == "--nodeps" ]; then
		NODEPS="True"
	elif [[ "$1" == "--version" || "$1" == "-v" ]]; then
		echo ""
		echo "The Dropline Installer"
		echo "Copyright 2002-2003 Todd Kulesza"
		echo "Version $VERSION"
		echo ""
		umask -S $OLD_UMASK  &> /dev/null
		exit 0
	elif [[ "$1" == "--help" || "$1" == "-h" ]]; then
		echo ""
		echo "Usage: dropline-installer [options]"
		echo ""
		echo "The Dropline Installer is menu-based and does not require any options."
		echo ""
		echo "Options:"
		echo "  --nodeps        Disable dependancy checking."
		echo "  --upgrade       Go directly to the Upgrade menu."
		echo "  --autoupdate    Do an update without any user input."
		echo ""
		umask -S $OLD_UMASK  &> /dev/null
		exit 0
	elif [[ "$1" == "--upgrade" || "$1" == "-u" ]]; then
		UPGRADE="True"
	elif [[ "$1" == "--autoupdate" || "$1" == "-a" ]]; then
		AUTO="True"
	else
		echo ""
		echo "Error: Arguement '$1' unknown.  Try $0 --help."
		echo ""
		umask -S $OLD_UMASK  &> /dev/null
		exit 1
	fi
fi

if [[ -n $2 && $2 != "" ]]; then
	if [ "$2" == "--nodeps" ]; then
		NODEPS="True"
	elif [[ "$2" == "--version" || "$2" == "-v" ]]; then
		echo ""
		echo "The Dropline Installer"
		echo "Copyright 2002-2003 Todd Kulesza"
		echo "Version $VERSION"
		echo ""
		umask -S $OLD_UMASK  &> /dev/null
		exit 0
	elif [[ "$2" == "--help" || "$2" == "-h" ]]; then
		echo ""
		echo "Usage: dropline-installer [options]"
		echo ""
		echo "The Dropline Installer is menu-based and does not require any options."
		echo ""
		echo "Options:"
		echo "  --nodeps   Disable dependancy checking."
		echo "  --upgrade  Go directly to the Upgrade menu."
		echo ""
		umask -S $OLD_UMASK  &> /dev/null
		exit 0
	elif [[ "$2" == "--upgrade" || "$2" == "-u" ]]; then
		UPGRADE="True"
	elif [[ "$1" == "--autoupdate" || "$1" == "-a" ]]; then
		AUTO="True"
	else
		echo ""
		echo "Error: Arguement '$2' unknown.  Try '$0 --help'."
		echo ""
		umask -S $OLD_UMASK  &> /dev/null
		exit 1
	fi
fi

MEETS_REQUIREMENTS=0
echo "Checking requirements..."
echo ""

# check for binaries we're going to need...
for dep in $DEP_APPS
do
	echo -n "Checking for $dep... "
	$dep --version &> /dev/null
	if [ "$?" == "127" ]; then
		echo "not found."
		MEETS_REQUIREMENTS=1
	else
		echo "yes."
	fi
done

# check for the libraries GNOME needs...
ldconfig -v > $tempfile
for dep in $DEP_LIBS
do
	echo -n "Checking for $dep... "
	if [ "`grep $dep $tempfile`" == "" ]; then
		echo "not found."
		MEETS_REQUIREMENTS=1
	else
		echo "yes."
	fi
done

if [[ "$MEETS_REQUIREMENTS" == "1" && "$NODEPS" == "False" ]]; then
	echo ""
	echo "Error: Your system is missing required software to install Dropline GNOME."
	echo "The programs and libraries listed as 'not found' must be installed in order"
	echo "to proceed.  Please ensure the following Slackware packages are installed:"
	echo ""
	echo "    cdparanoia glib-1.2 grep gtk+-1.2 libjpeg libpng perl pkgtools sed "
	echo "    textutils wget zlib"
	echo ""
	echo "These can be downloaded from ftp.slackware.com and installed using installpkg."
	echo "Exiting..."
	exit 1
fi

show_dialog_success_install ()
{
	$DIALOG --backtitle "The Dropline Installer" \
			--title "Installation Complete" \
			--msgbox "Congratulations!  Your computer is now running the \
Dropline GNOME Desktop.  You may need to reboot for some changes to take \
effect.\n\nYou can enable \
a graphical login by opening the file /etc/inittab and changing the line \
which reads \"id:3:initdefault:\" to \"id:4:initdefault:\".\n\nIf you have any \
questions or comments, please feel free to visit the Dropline Forums at \
http://www.dropline.net/forums.\n\nThanks for using Dropline GNOME!" 17 66
}

show_dialog_success_upgrade ()
{
	$DIALOG --backtitle "The Dropline Installer" \
			--title "Upgrade Complete" \
			--msgbox "Congratulations!  Your computer is currently running the \
latest release of  the Dropline GNOME Desktop.  You may need to reboot for some \
changes to take effect.\n\nIf you have any \
questions or comments, please feel free to visit the Dropline Forums at \
http://www.dropline.net/forums.\n\nThanks for using Dropline GNOME!" 12 66
}

show_dialog_uptodate ()
{
	$DIALOG --backtitle "The Dropline Installer" \
			--title "No Updates Available" \
			--msgbox "Your system is currently up to date, so no \
upgrades are available at this time." 7 55
}

show_dialog_installer_upgrade ()
{
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Installer Updated" \
		--msgbox "The Dropline Installer has been updated to the \
most recent version and will now exit.  Please run the new installer with \
the command 'dropline-installer' to detect and install further updates." 9 60
	
	reset
	exit
}

checksums ()
{
	FileLocation="$1"
	dir=`dirname "$1"`
	pdir=`pwd`
	
	cd "$dir"
	
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Checking Package Integrity" \
		--infobox "Checking the MD5 sum of each package to ensure \
that none are corrupt.  This may take several minutes." 6 55

	count=${#DroplineFiles[@]}
	index=0
	
	while [ "$index" -le "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			if [ ! -e "${DroplineFiles[$index]}" ]; then
				$DIALOG --backtitle "The Dropline Installer" \
					--title "Package Missing" \
					--msgbox "The package '${DroplineFiles[$index]}' was not found.  \
Please try downloading the packages again." 8 55
				
				return 1
			else
				md5_real="`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 5 -d ':'`"
				md5_download="`md5sum ${DroplineFiles[$index]} | cut -f 1 -d ' '`"
				if [ "$md5_real" != "$md5_download" ]; then
					$DIALOG --backtitle "The Dropline Installer" \
						--title "Package Corrupt" \
						--msgbox "MD5 sum of ${DroplineFiles[$index]} is not \
correct.  Please delete this package before trying again." 8 55
					
					return 1
				fi
			fi
		fi
		let "index += 1"
	done
	
	cd "$pdir"
	
	return 0
}

remove_gnome1_packages ()
{
	$DIALOG --backtitle "The Dropline Installer" \
			--title "Uninstalling" \
			--infobox "The Dropline Installer is searching for and removing \
obsolete GNOME packages.  This may take several minutes." 6 55

	removepkg gnome-admin gnome-core gtop &> /dev/null
}

remove_obsolete_packages ()
{
	dir=$1
	
	if [ ! -e "$dir/$DroplineFilesVersion" ]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Error" \
			--msgbox "Could not open file list '$dir/$DroplineFilesVersion'.  You can \
download this file at http://www.dropline.net/gnome/$DroplineFilesVersion if you do not have it." 10 55
		return
	fi
	
	word=installed
	while [ -n "$word" ]
	do
		read word
		# check for ctl-c
		if [ "$?" == "130" ]; then
			umask -S $OLD_UMASK  &> /dev/null
			reset
			exit 130
		fi
		if [ "`echo "$word" | cut -f 2 -d ':'`" == "obsolete" ]; then
			word=`echo "$word" | cut -f 1 -d '.' | sed -e 's/-[0-9]//g'`
			if [ "$word" != "" ]; then
				removepkg $word &> /dev/null
			fi
		fi
	done < "$dir/$DroplineFilesVersion"
}

clean_up ()
{
	for file in *.tgz
	do
		if [ "$file" == "*.tgz" ]; then
			continue;
		fi
		if [ "`grep \"$file\" $DROPLINE_FILES`" == "" ]; then
			rm -f $file
		fi
	done
}

remove_files ()
{
	$DIALOG --backtitle "The Dropline Installer" \
			--title "Uninstall?" \
			--yesno "Are you sure you want to uninstall the complete Dropline GNOME Desktop?" \
			6 45 2> $tempfile
	retval=$?
	
	if [ "$retval" != "0" ]; then
		return
	fi
	
	if [ ! -e "$CACHE/$DroplineFilesVersion" ]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Error" \
			--msgbox "Could not open file list '$CACHE/$DroplineFilesVersion'.  You can \
download this file at http://www.dropline.net/gnome/$DroplineFilesVersion if you do not have it." 10 55
		return
	fi
	
	$DIALOG --backtitle "The Dropline Installer" \
			--title "In Progress" \
			--infobox "Creating list of packages to remove..." 4 45
	
	count=0
	index=1
	percent=0
	word="test"
	while [ -n "$word" ]
	do
		read word
		if [ "`echo "$word" | cut -f 4 -d ':'`" == "remove" ]; then
			let "count += 1"
		fi
	done < "$CACHE/$DroplineFilesVersion"
	
	word="test"
	(
	while [ -n "$word" ]
	do
		read word
		
		if [ "`echo "$word" | cut -f 4 -d ':'`" == "remove" ]; then
			word=`echo "$word" | cut -f 1 -d '.' | sed -e 's/-[0-9]//g'`
			if [ "$word" != "" ]; then
				echo "XXX"
				let "percent = index * 100 / count"
				echo $percent
				echo "Removing $word..."
				echo "XXX"
				removepkg $word &> /dev/null
				# check for ctl-c
				if [ "$?" == "130" ]; then
					umask -S $OLD_UMASK  &> /dev/null
					reset
					exit 130
				fi
			fi
			let "index += 1"
		fi
	done < "$CACHE/$DroplineFilesVersion"
	) |
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Removing Packages" \
		--gauge "" 10 50
	
	$DIALOG --backtitle "The Dropline Installer" \
			--title "Uninstall Complete" \
			--msgbox "The Dropline GNOME Desktop has been successfully removed from your system.  \
Certain system-wide packages such as FreeType and LibXML have been left installed so that standard Slackware \
software will continue to function." 10 55
}

install_files ()
{
	FileLocation="$1"
	dir=`dirname "$1"`
	TYPE="$2"

	# Remove the older -dropline- packages first	
	current=`pwd`
	cd /var/log/packages
	percent=0
	install_count=0
	install_index=1
	for file in *-dropline-*
	do
		if [ "$file" != "*-dropline-*" ]; then
			let "install_count += 1"
		fi
	done
	
	if [[ "$install_count" != "0" && "$TYPE" == "Selective" ]]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Obsolete Packages Detected" \
			--yesno "You have $install_count obsolete Dropline GNOME packages which must \
be removed before Dropline GNOME will work properly.  If you have not choosen to uprade \
all of the files marked 'Required', your system may not run correctly after the update. \
Would you like to proceed with this upgrade? " 10 65 2> $tempfile
	
		retval=$?
		if [ "$retval" != "0" ]; then
			return
		fi
	fi
	
	(
	for file in *-dropline-*
	do
		if [ "$file" == "*-dropline-*" ]; then
			continue;
		fi
		echo "XXX"
		let "percent = install_index * 100 / install_count"
		echo $percent
		echo "Before installing, the Dropline Installer will remove obsolete Dropline GNOME packages.  \
This may take several minutes.\n\nRemoving $file..."
		echo "XXX"
		if [ "open-office-dropline-1.0.1-i386-1" != "$file" ]; then
			removepkg $file &> /dev/null
	
			# check for ctl-c
			if [ "$?" == "130" ]; then
				exit_status="$?"
				break;
			fi
		fi
		let "install_index += 1"				
	done
	) |
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Removing Obsolete Packages" \
		--gauge "" 12 50
	
	if [ "$exit_status" == "130" ]; then
		umask -S $OLD_UMASK  &> /dev/null
		reset
		exit 130
	fi
	
	cd $current
	
	cd $dir
	count=${#DroplineFiles[@]}
	index=0
	percent=0
	install_count=0
	install_index=1
	while [ "$index" -lt "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			let "install_count += 1"
		fi
		let "index += 1"
	done
	index=0
	cat /dev/null > $tempfile
	(
	while [ "$index" -le "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			echo "XXX"
			let "percent = install_index * 100 / install_count"
			echo $percent
			echo "Installing ${DroplineFiles[$index]}..."
			echo "XXX"
#			if [ ! -e "${DroplineFiles[$index]}" ]; then
#				echo "Could not open file: ${DroplineFiles[$index]}" > $tempfile
#				break;
#			else
#				md5_real="`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 5 -d ':'`"
#				md5_download="`md5sum ${DroplineFiles[$index]} | cut -f 1 -d ' '`"
#				if [ "$md5_real" != "$md5_download" ]; then
#					echo "MD5 sum of ${DroplineFiles[$index]} is not correct: '$md5_real' != '$md5_download'" > $tempfile
#					break;
#				fi
#			fi
			
			upgradepkg --install-new --reinstall "${DroplineFiles[$index]}" >/dev/null 2>$tempfile
			# check for ctl-c
			# check for ctl-c
			if [ "$?" == "130" ]; then
				break
			fi
			if [ "`cat $tempfile`" != "" ]; then
				# FAM does this a lot, it's ok
				if [ "`grep \"Configuration changes to rpc\" $tempfile`" == "" ]; then
					break;
				fi
			fi
			let "install_index += 1"
		fi
		let "index += 1"
	done
	) |
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Installing Packages" \
		--gauge "" 8 50
	
	if [ "$?" == "130" ]; then
		umask -S $OLD_UMASK  &> /dev/null
		reset
		exit 130
	fi
	
	if [ "`cat $tempfile`" != "" ]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Installation Error" \
			--msgbox "`cat $tempfile`" 12 65
	else		
		remove_gnome1_packages
		remove_obsolete_packages $dir
		clean_up
		show_dialog_success_install
	fi
	
	cd $CACHE
}

upgrade_files ()
{
	dir=`dirname "$1"`
	TYPE="$2"
	upgrade_installer="$3"
	
	# Remove the older -dropline- packages first	
	current=`pwd`
	cd /var/log/packages
	percent=0
	install_count=0
	install_index=1
	for file in *-dropline-*
	do
		if [ "$file" != "*-dropline-*" ]; then
			let "install_count += 1"
		fi
	done

	if [[ "$install_count" != "0" && "$TYPE" == "Selective" ]]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Obsolete Packages Detected" \
			--yesno "You have $install_count obsolete Dropline GNOME packages which must \
be removed before Dropline GNOME will work properly.  If you have not choosen to uprade \
all of the files marked 'Required', your system may not run correctly after the update. \
Would you like to proceed with this upgrade? " 10 65 2> $tempfile
	
		retval=$?
		if [ "$retval" != "0" ]; then
			return
		fi
	fi
	
	(
	for file in *-dropline-*
	do
		if [ "$file" == "*-dropline-*" ]; then
			continue;
		fi
		echo "XXX"
		let "percent = install_index * 100 / install_count"
		echo $percent
		echo "Before upgrading, the Dropline Installer will remove obsolete Dropline GNOME packages.  \
This may take several minutes.\n\nRemoving $file..."
		echo "XXX"
		if [ "open-office-dropline-1.0.1-i386-1" != "$file" ]; then
			removepkg $file &> /dev/null
			
			# check for ctl-c
			if [ "$?" == "130" ]; then
				break;
			fi
		fi
		let "install_index += 1"				
	done
	) |
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Removing Obsolete Packages" \
		--gauge "" 12 50
	if [ "$?" == "130" ]; then
		umask -S $OLD_UMASK  &> /dev/null
		reset
		exit 130
	fi
	
	cd $dir
	count=${#DroplineFiles[@]}
	index=0
	percent=0
	install_count=0
	install_index=1
	while [ "$index" -lt "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			let "install_count += 1"
		fi
		let "index += 1"
	done
	index=0
	cat /dev/null > $tempfile
	(
	while [ "$index" -le "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			echo "XXX"
			let "percent = install_index * 100 / install_count"
			echo $percent
			echo "Upgrading to ${DroplineFiles[$index]}..."
			echo "XXX"
#			if [ ! -e "${DroplineFiles[$index]}" ]; then
#				echo "Could not open file: ${DroplineFiles[$index]}" > $tempfile
#				break;
#			else
#				md5_real="`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 5 -d ':'`"
#				md5_download="`md5sum ${DroplineFiles[$index]} | cut -f 1 -d ' '`"
#				if [ "$md5_real" != "$md5_download" ]; then
#					echo "MD5 sum of ${DroplineFiles[$index]} is not correct: '$md5_real' != '$md5_download'" > $tempfile
#					break;
#				fi
#			fi
			
			upgradepkg --install-new --reinstall "${DroplineFiles[$index]}" >/dev/null 2>$tempfile
			# check for ctl-c
			if [ "$?" == "130" ]; then
				break
			fi
			if [ "`cat $tempfile`" != "" ]; then
				# FAM does this a lot, it's ok
				if [ "`grep \"Configuration changes to rpc\" $tempfile`" == "" ]; then
					break;
				fi
			fi
			let "install_index += 1"
		fi
		let "index += 1"
	done
	) |
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Upgrading Packages" \
		--gauge "" 8 50 0
	
	if [ "$?" == "130" ]; then
		umask -S $OLD_UMASK  &> /dev/null
		reset
		exit 130
	fi
	
	if [ "`cat $tempfile`" != "" ]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Installation Error" \
			--msgbox "`cat $tempfile`" 12 65
	else		
		remove_gnome1_packages
		remove_obsolete_packages $dir
		clean_up
		
		if [ "$upgrade_installer" == "1" ]; then
			show_dialog_installer_upgrade
		else
			show_dialog_success_upgrade
		fi
	fi
	
	cd $CACHE
}

download_files ()
{		
	count=${#DroplineFiles[@]}
	index=0
	download_count=0
	download_index=1
	
	while [ "$index" -le "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			let "download_count += 1"
		fi
		let "index += 1"
	done
	index=0;
	while [ "$index" -le "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			echo "" > $CACHE/wget_output
			
			$DIALOG --backtitle "The Dropline Installer" \
				--title "Downloading ${DroplineFiles[$index]} ($download_index of $download_count)" \
				--no-kill --begin 3 0 \
				--tailboxbg $CACHE/wget_output 19 78 2>$tempfile
			
			read proxy_user < ~/.dropline/proxy_username
			read proxy_pass < ~/.dropline/proxy_password
			
			wget -w 1 -N -t 0 -c -F --proxy-user=$proxy_user --proxy-passwd=$proxy_pass \
				$MIRROR/${DroplineFiles[$index]} \
				-o $CACHE/wget_output
			
			# check for ctl-c
			if [ "$?" == "130" ]; then
				umask -S $OLD_UMASK  &> /dev/null
				reset
				exit 130
			fi
			
			kill -3 `cat $tempfile`
			let "download_index += 1"
		fi
		let "index += 1"
	done
}

install_select_files ()
{
	index=1
	word="installed"
	unset DroplineFiles
	
	if [ ! -e $CACHE/DroplineFilesDisliked ]; then
		cat /dev/null > $CACHE/DroplineFilesDisliked
	fi
	
	$DIALOG --backtitle "The Dropline Installer" \
			--clear --title "Install - Select Installation Type" \
    	    --menu "What type of installation would you like?\n" 12 70 3 \
    	    "Full" "Install the complete Dropline GNOME system" \
    	    "Selective" "Choose which packages to install" \
    	    "Minimal"  "Only install the basic GNOME packages" 2> $tempfile

	retval=$?

	choice=`cat $tempfile`

	if (($retval != 0)); then
		return
	fi
	
	if [ "$1" == "Internet" ]; then
		rm -f $CACHE/$DroplineFilesVersion
		cat /dev/null > $CACHE/wget_output
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Retrieving File Lists" \
			--no-kill --begin 3 4 \
			--tailboxbg $CACHE/wget_output 15 70 2>$tempfile
		
		read proxy_user < ~/.dropline/proxy_username
		read proxy_pass < ~/.dropline/proxy_password
		
		wget -t 0 -O $CACHE/$DroplineFilesVersion --proxy-user=$proxy_user --proxy-passwd=$proxy_pass \
			http://www.dropline.net/gnome/$DroplineFilesVersion -o $CACHE/wget_output
		# check for ctl-c
		if [ "$?" == "130" ]; then
			umask -S $OLD_UMASK  &> /dev/null
			reset
			exit 130
		fi
		kill -3 `cat $tempfile`
		FileLocation=$CACHE/$DroplineFilesVersion
	elif [ "$1" == "CD-ROM" ]; then
		FileLocation=/mnt/cdrom/dropline-gnome/$DroplineFilesVersion
	elif [ "$1" == "Hard Disk" ]; then
		FileLocation=$CACHE/$DroplineFilesVersion
	else
		FileLocation=/
	fi
	
	DROPLINE_FILES=$FileLocation
	
	if [ ! -e $FileLocation ]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Error" \
			--msgbox "Could not open file list '$FileLocation'." 6 45
		return
	fi
	
	$DIALOG --backtitle "The Dropline Installer" \
			--title "In Progress" \
			--infobox "Creating list of packages to install..." 4 45
			
	ls /var/log/packages > package_list
	installer_package="`grep dropline-installer $FileLocation | cut -f 1 -d ':'`"
	while [ -n "$word" ]
	do
		read word
		required="`echo $word | cut -f 2 -d ':'`"
		word="`echo $word | cut -f 1 -d ':'`"
		if [ "$required" == "obsolete" ]; then
			word="installed"
		elif [[ "$choice" == "Minimal" && "$required" != "" && "$required" != "required" ]]; then
			word="installed"
		elif [ "$installer_package" == "$word" ]; then
			word="installed"
		fi
		DroplineFiles[$index]="$word"
		let "index += 1"
	done < "$FileLocation"
	
	if [ "$choice" == "Selective" ]; then
		index=0
		count=${#DroplineFiles[@]}
		if [ "$count" -lt "11" ]; then
			display=$count
		else
			display=11
		fi
		echo "$DIALOG --backtitle \"The Dropline Installer\" \
			--title \"Select Packages\" --item-help --begin 3 10 \
			--checklist \"Use the space-bar to select which packages will be installed:\" 19 58 $display \\" > ./package_selection
		while [ "$index" -le "$count" ]
		do
			if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
				size=`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 6 -d ':'`
				let "size /= 1024"
				desc=`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 3 -d ':'`
				desc="$desc ($size KB)"
				name=`echo ${DroplineFiles[$index]} | cut -f 1 -d '.' | sed -e 's/-[0-9]//g'`
				disliked=`grep "^$name" $CACHE/DroplineFilesDisliked`
				if [ "$disliked" == "" ]; then
					echo "\"${DroplineFiles[$index]}\" \"\" on \"$desc\" \\" >> ./package_selection
				else
					echo "\"${DroplineFiles[$index]}\" \"\" off \"$desc\" \\" >> ./package_selection
				fi
			fi
			let "index += 1"
		done
		echo " 2> $tempfile" >> ./package_selection
		echo "echo \$? > $CACHE/package_select_result" >> ./package_selection
		sh ./package_selection
		retval=`cat $CACHE/package_select_result`
		if [ "$retval" == "0" ]; then
			cp $tempfile $CACHE/DroplineFilesSelect
			sed -e 's/\"//g' $CACHE/DroplineFilesSelect >$CACHE/DroplineFilesSelect2
			index=1
			cat /dev/null > $CACHE/DroplineFilesSelect
			while [ ! "`cat $CACHE/DroplineFilesSelect2 | cut -f $index -d ' '`" = "" ]; do
				cat $CACHE/DroplineFilesSelect2 | cut -f $index -d ' ' >> $CACHE/DroplineFilesSelect
				let "index += 1"
			done
			ls /var/log/packages > package_list
			index=0
			word="installed"
			unset DroplineFiles
			while [ -n "$word" ]
			do
				read word
				DroplineFiles[$index]="$word"
				let "index += 1"
			done < $CACHE/DroplineFilesSelect
			word="installed"
			cat /dev/null > $CACHE/DroplineFilesDisliked
			while [ -n "$word" ]
			do
				read word
				word="`echo \"$word\" | cut -f 1 -d ':'`" 
				if [ "`grep \"$word\" $CACHE/DroplineFilesSelect`" == "" ]; then
					echo $word >> $CACHE/DroplineFilesDisliked
				fi
			done < $FileLocation
		else
			return
		fi
	fi
	
	if [ "$1" == "Internet" ]; then
		download_files $DroplineFiles
	fi
	
	checksums "$FileLocation"
	if [ "$?" == "0" ]; then
		install_files "$FileLocation" "$choice"
	fi
}

upgrade_select_files ()
{
	index=1
	word="installed"
	declare -a LocalFiles
	unset DroplineFiles
	unset LocalFiles
	
	if [ ! -e $CACHE/DroplineFilesDisliked ]; then
		cat /dev/null > $CACHE/DroplineFilesDisliked
	fi
	
	$DIALOG --backtitle "The Dropline Installer" \
			--clear --title "Upgrade - Select Upgrade Type" \
    	    --menu "What type of upgrade would you like to perform?\n" 12 70 3 \
    	    "Full" "Upgrade to the latest Dropline GNOME release" \
    	    "Selective" "Choose which packages to upgrade" \
    	    "Minimal"  "Only upgrade the basic GNOME packages" 2> $tempfile

	retval=$?

	choice=`cat $tempfile`

	if (($retval != 0)); then
		return
	fi
	
	if [ "$1" == "Internet" ]; then
		rm -f $CACHE/$DroplineFilesVersion
		cat /dev/null > $CACHE/wget_output
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Retrieving File Lists" \
			--no-kill --begin 3 4 \
			--tailboxbg $CACHE/wget_output 15 70 2>$tempfile
		
		read proxy_user < ~/.dropline/proxy_username
		read proxy_pass < ~/.dropline/proxy_password
			
		wget -t 0 -O $CACHE/$DroplineFilesVersion --proxy-user=$proxy_user --proxy-passwd=$proxy_pass \
			http://www.dropline.net/gnome/$DroplineFilesVersion -o $CACHE/wget_output
		# check for ctl-c
		if [ "$?" == "130" ]; then
			umask -S $OLD_UMASK  &> /dev/null
			reset
			exit 130
		fi
		kill -3 `cat $tempfile`
		FileLocation=$CACHE/$DroplineFilesVersion
	elif [ "$1" == "CD-ROM" ]; then
		FileLocation=/mnt/cdrom/dropline-gnome/$DroplineFilesVersion
	elif [ "$1" == "Hard Disk" ]; then
		FileLocation=$CACHE/$DroplineFilesVersion
	else
		FileLocation=/
	fi
	
	DROPLINE_FILES=$FileLocation
		
	if [ ! -e $FileLocation ]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Error" \
			--msgbox "Could not open file list '$FileLocation'." 7 45
		return
	fi
	
	$DIALOG --backtitle "The Dropline Installer" \
			--title "In Progress" \
			--infobox "Creating list of packages to upgrade..." 4 45
			
	cat /dev/null > $CACHE/DroplineFilesUpgrade
	ls /var/log/packages > package_list
	items=0
	while [ -n "$word" ]
	do
		read word
		required="`echo $word | cut -f 2 -d ':'`"
		word="`echo $word | cut -f 1 -d ':'`"
		package=${word/".tgz"/""}
		found=`grep "^$package" package_list`
		
		if [ "$required" == "obsolete" ]; then
			DroplineFiles[$index]="installed"
		elif [[ "$choice" == "Minimal" && "$required" != "" && "$required" != "required" ]]; then
			DroplineFiles[$index]="installed"
		elif [ "$found" == "$package" ]; then
			DroplineFiles[$index]="installed"
		else
			if [ "$word" != "" ]; then
				DroplineFiles[$index]="$word"
				echo "$word" | cut -f 1 -d '.' | sed -e 's/-[0-9]//g' >> $CACHE/DroplineFilesUpgrade
				let "items += 1"
			fi
		fi
		let "index += 1"
	done < "$FileLocation"
	
	if [ "`grep dropline-installer $CACHE/DroplineFilesUpgrade`" != "" ]; then
		upgrade_installer="1"
		$DIALOG --backtitle "The Dropline Installer" \
			--title "New Installer Available" \
			--msgbox "An updated version of the Dropline Installer is available \
and must be installed before further updates can proceed." 8 55
		index=2
		count=${#DroplineFiles[@]}
		while [ "$index" -le "$count" ]
		do
			DroplineFiles[$index]="installed"			
			let "index += 1"
		done
	else
		upgrade_installer="0"
	fi
	
	if [[ "$choice" == "Selective" && "$items" -gt "0" ]]; then
		index=0
		count=${#DroplineFiles[@]}
		if [ "$items" -lt "11" ]; then
			display=$items
		else
			display=11
		fi
		echo "$DIALOG --backtitle \"The Dropline Installer\" \
			--title \"Select Packages\" --item-help --begin 3 10 \
			--checklist \"Use the space-bar to select which packages will be upgraded:\" 19 58 $display \\" > ./package_selection
		while [ "$index" -le "$count" ]
		do
			if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
				size=`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 6 -d ':'`
				let "size /= 1024"
				desc=`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 3 -d ':'`
				desc="$desc ($size KB)"
				name=`echo ${DroplineFiles[$index]} | cut -f 1 -d '.' | sed -e 's/-[0-9]//g'`
				disliked=`grep "^$name" $CACHE/DroplineFilesDisliked`
				if [ "$disliked" == "" ]; then
					echo "\"${DroplineFiles[$index]}\" \"\" on \"$desc\" \\" >> ./package_selection
				else
					echo "\"${DroplineFiles[$index]}\" \"\" off \"$desc\" \\" >> ./package_selection
				fi
			fi
			let "index += 1"
		done
		
		echo " 2> $tempfile" >> ./package_selection
		echo "echo \$? > $CACHE/package_select_result" >> ./package_selection
		
		if [ "$items" -gt "0" ]; then
			sh ./package_selection
			retval=`cat $CACHE/package_select_result`
			if [ "$retval" == "0" ]; then
				cp $tempfile $CACHE/DroplineFilesSelect
				sed -e 's/\"//g' $CACHE/DroplineFilesSelect >$CACHE/DroplineFilesSelect2
				index=1
				cat /dev/null > $CACHE/DroplineFilesSelect
				while [ ! "`cat $CACHE/DroplineFilesSelect2 | cut -f $index -d ' '`" = "" ]; do
					cat $CACHE/DroplineFilesSelect2 | cut -f $index -d ' ' >> $CACHE/DroplineFilesSelect
					let "index += 1"
				done
				ls /var/log/packages > package_list
				index=0
				word="installed"
				unset DroplineFiles
				while [ -n "$word" ]
				do
					read word
					DroplineFiles[$index]="$word"
					let "index += 1"
				done < $CACHE/DroplineFilesSelect
				word="installed"
				cat /dev/null > $CACHE/DroplineFilesDisliked
				while [ -n "$word" ]
				do
					read word
					if [ "`grep \"$word\" $CACHE/DroplineFilesSelect`" == "" ]; then
						echo $word >> $CACHE/DroplineFilesDisliked
					fi
				done < $CACHE/DroplineFilesUpgrade
			else
				return
			fi
		fi
	fi
	
	if [[ "$1" == "Internet" && "$items" -gt "0" ]]; then
		download_files
	fi
	
	if [ "$items" == "0" ]; then
		show_dialog_uptodate
	else
		checksums "$FileLocation"
		if [ "$?" == "0" ]; then
			upgrade_files "$FileLocation" "$choice" "$upgrade_installer"
		fi
	fi
}

install_menu ()
{
$DIALOG --backtitle "The Dropline Installer" \
			--clear --title "Install - Select Source" \
    	    --menu "Select where you would like to install the Dropline GNOME \
packages from:\n" 12 70 3 \
    	    "Internet" "Download the latest packages from the Internet" \
    	    "CD-ROM" "Install using an official CD-ROM" \
    	    "Hard Disk"  "Install using packages on your local disk" 2> $tempfile

	retval=$?

	choice=`cat $tempfile`

	case $retval in
	  0)
		if [ "$choice" == "Internet" ]; then
			install_select_files "$choice";
			return;
		elif [ "$choice" == "CD-ROM" ]; then
			install_select_files "$choice";
			return;
		elif [ "$choice" == "Hard Disk" ]; then
			install_select_files "$choice";
			return;
		fi
		
		;;
	
	esac
}

upgrade_menu ()
{
$DIALOG --backtitle "The Dropline Installer" \
			--clear --title "Upgrade - Select Source" \
    	    --menu "Select where you would like to upgrade the Dropline GNOME \
packages from:\n" 12 70 3 \
    	    "Internet" "Download the latest packages from the Internet" \
    	    "CD-ROM" "Upgrade using an official CD-ROM" \
    	    "Hard Disk"  "Upgrade using packages on your local disk" 2> $tempfile

	retval=$?

	choice=`cat $tempfile`

	case $retval in
	  0)
		if [ "$choice" == "Internet" ]; then
			upgrade_select_files "$choice";
			return;
		elif [ "$choice" == "CD-ROM" ]; then
			upgrade_select_files "$choice";
			return;
		elif [ "$choice" == "Hard Disk" ]; then
			upgrade_select_files "$choice";
			return;
		fi
		
		;;
	esac
}

view_updates ()
{
	unset DroplineFiles

	rm -f $CACHE/$DroplineFilesVersion
	cat /dev/null > $CACHE/wget_output
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Retrieving File Lists" \
		--no-kill --begin 3 4 \
		--tailboxbg $CACHE/wget_output 15 70 2>$tempfile
		
	read proxy_user < ~/.dropline/proxy_username
	read proxy_pass < ~/.dropline/proxy_password
			
	wget -t 0 -O $CACHE/$DroplineFilesVersion --proxy-user=$proxy_user --proxy-passwd=$proxy_pass \
		http://www.dropline.net/gnome/$DroplineFilesVersion -o $CACHE/wget_output
	# check for ctl-c
	if [ "$?" == "130" ]; then
		umask -S $OLD_UMASK  &> /dev/null
		reset
		exit 130
	fi
	kill -3 `cat $tempfile`
	
	if [ ! -e $CACHE/$DroplineFilesVersion ]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Error" \
			--msgbox "Could not open file list '$CACHE/$DroplineFilesVersion'." 8 45
		return
	fi
	
	$DIALOG --backtitle "The Dropline Installer" \
		--title "In Progress" \
		--infobox "Creating list of packages to upgrade..." 4 45
			
	cat /dev/null > $CACHE/DroplineFilesUpgrade
	ls /var/log/packages > package_list
	items=0
	word=installed
	cat /dev/null > $CACHE/DroplineFilesUpgrade
	while [ -n "$word" ]
	do
		read word
		required="`echo $word | cut -f 2 -d ':'`"
		word="`echo $word | cut -f 1 -d ':'`"
		package=${word/".tgz"/""}
		found=`grep "^$package" package_list`
		
		if [ "$required" == "obsolete" ]; then
			DroplineFiles[$index]="installed"
		elif [[ "$choice" == "Minimal" && "$required" != "" && "$required" != "required" ]]; then
			DroplineFiles[$index]="installed"
		elif [ "$found" == "$package" ]; then
			DroplineFiles[$index]="installed"
		else
			if [ "$word" != "" ]; then
				DroplineFiles[$index]="$word"
				echo "$word" >> $CACHE/DroplineFilesUpgrade
				let "items += 1"
			fi
		fi
		let "index += 1"
	done < "$CACHE/$DroplineFilesVersion"
	
	if [ "$items" == "0" ]; then
		show_dialog_uptodate
	else
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Available Packages" \
			--exit-label "Back" \
			--textbox $CACHE/DroplineFilesUpgrade 19 50
	fi

	return;
}

set_proxy ()
{
	$DIALOG --backtitle "The Dropline Installer" \
			--clear --title "Set Proxy Server" \
    	    --menu "Select an action to perform:" 11 65 4 \
    	    "Set Address" "Specify your HTTP proxy server's address" \
    	    "Set Username" "Specify your proxy username, if needed" \
			"Set Password" "Specify your proxy password, if needed" \
    	    "Back"  "Go back to the main menu" 2> $tempfile

	retval=$?
	choice=`cat $tempfile`
	if [ "$retval" == "0" ]; then
		if [ "$choice" == "Set Address" ]; then
			if [ -e ~/.wgetrc ]; then
				grep http_proxy ~/.wgetrc > $CACHE/proxy
				sed -e 's/http_proxy//g' -e 's/=//g' -e 's/ //g' $CACHE/proxy &> $CACHE/proxy2
				read proxy < $CACHE/proxy2
			else
				proxy=""
			fi
			
			$DIALOG --backtitle "The Dropline Installer $VERSION" \
					--title "HTTP Proxy Server" \
					--inputbox "Enter the HTTP proxy server you use to connect to the Internet. \
If you do not use a proxy server, leave this line blank.\n\nExample: 192.168.1.1:1080\n" \
					12 65 $proxy 2> $tempfile
			
			retval=$?
			proxy=`cat $tempfile`
			if [ "$retval" == "0" ]; then
				if [ -e ~/.wgetrc ]; then
					sed /http_proxy/d ~/.wgetrc &> $CACHE/proxy
				else
					cat /dev/null > $CACHE/proxy
				fi
				echo "http_proxy=$proxy" >> $CACHE/proxy
				cp $CACHE/proxy ~/.wgetrc
			fi
		elif [ "$choice" == "Set Username" ]; then
			if [ -e ~/.dropline/proxy_username ]; then
				read proxy < ~/.dropline/proxy_username
			else
				proxy=""
			fi
			
			$DIALOG --backtitle "The Dropline Installer $VERSION" \
					--title "HTTP Proxy Username" \
					--inputbox "Enter your username for your proxy server.  \
If you do not have a username, leave this line blank.\n\nExample: molly\n" \
					12 65 $proxy 2> $tempfile
			
			retval=$?
			proxy=`cat $tempfile`
			if [ "$retval" == "0" ]; then
				echo "$proxy" > ~/.dropline/proxy_username
			fi
		elif [ "$choice" == "Set Password" ]; then
			if [ -e ~/.dropline/proxy_password ]; then
				read proxy < ~/.dropline/proxy_password
			else
				proxy=""
			fi
			
			$DIALOG --backtitle "The Dropline Installer $VERSION" \
					--title "HTTP Proxy Password" \
					--inputbox "Enter your password for your proxy server.  \
If you do not have a password, leave this line blank.  \n\nExample: shuriken\n" \
					12 65 $proxy 2> $tempfile
			
			retval=$?
			proxy=`cat $tempfile`
			if [ "$retval" == "0" ]; then
				echo "$proxy" > ~/.dropline/proxy_password
			fi
		elif [ "$choice" == "Back" ]; then
			return
		fi
		retval=0
	else
		return
	fi
	
	set_proxy
}

set_directory ()
{
	if [ -e ~/.dropline/cache_dir ]; then
		read dir < ~/.dropline/cache_dir
	else
		dir="/var/cache/dropline-installer"
	fi
	
	$DIALOG --backtitle "The Dropline Installer $VERSION" \
			--title "Download Directory" \
			--inputbox "Enter the directory where you would like the Dropline \
Installer to save files during a network-based installation.  This \
directory should reside on a share with at least 300 megabytes of free disk \
space." \
			12 70 $dir 2> $tempfile
	
	retval=$?
	dir=`cat $tempfile`
	if [ "$retval" == "0" ]; then
		echo "$dir" > ~/.dropline/cache_dir
	fi
}

main_menu ()
{
	if [ ! -d $CACHE ]; then
		mkdir $CACHE
	fi
	cd $CACHE
	
	if [ "$UPGRADE" == "True" ]; then
		UPGRADE="False"
		upgrade_menu
		retval=0
	else
		$DIALOG --backtitle "The Dropline Installer" \
			--clear --title "The Dropline Installer $VERSION" \
    			--menu "Welcome to the Dropline Installer!\n\n\
Please select an action to perform:" 16 70 7 \
		"Install" "Install a fresh copy of Dropline GNOME" \
		"Upgrade" "Upgrade an existing Dropline GNOME installation" \
		"View Updates" "Show a list of available updates" \
		"Remove" "Uninstall all of the Dropline GNOME packages" \
		"Set Proxy" "Specify an HTTP proxy server" \
		"Set Directory" "Specify a directory to hold downloaded files" \
		"Exit"  "Exit the Dropline Installer" 2> $tempfile

		retval=$?

		choice=`cat $tempfile`

		case $retval in
		  0)
			if [ "$choice" == "Install" ]; then
				install_menu;
			elif [ "$choice" == "Upgrade" ]; then
				upgrade_menu;
			elif [ "$choice" == "View Updates" ]; then
				view_updates;
			elif [ "$choice" == "Remove" ]; then
				remove_files;
			elif [ "$choice" == "Set Proxy" ]; then
				set_proxy;
			elif [ "$choice" == "Set Directory" ]; then
				set_directory;
			elif [ "$choice" == "Exit" ]; then
				$DIALOG --clear
				umask -S $OLD_UMASK  &> /dev/null
				reset
				exit 0
			fi
			retval=0
			;;
		esac
	fi
	
	if (($retval == 0)); then
		CACHE=`cat ~/.dropline/cache_dir`
		main_menu
	fi
	
	$DIALOG --clear
	umask -S $OLD_UMASK  &> /dev/null
	reset
	exit 0
}

auto_update ()
{
	unset DroplineFiles
	dir=`pwd`
	FileLocation=$CACHE/$DroplineFilesVersion
	cd $CACHE
	
	rm -f $CACHE/$DroplineFilesVersion
	echo "Retrieving file list..."
	
	read proxy_user < ~/.dropline/proxy_username
	read proxy_pass < ~/.dropline/proxy_password
			
	wget -t 0 -O $CACHE/$DroplineFilesVersion --proxy-user=$proxy_user --proxy-passwd=$proxy_pass \
		http://www.dropline.net/gnome/$DroplineFilesVersion
	# check for ctl-c
	if [ "$?" == "130" ]; then
		umask -S $OLD_UMASK  &> /dev/null
		reset
		exit 130
	fi
			
	if [ ! -e $CACHE/$DroplineFilesVersion ]; then
		echo "Error: Could not open '$CACHE/$DroplineFilesVersion'"
		return
	fi
	
	echo "Removing obolete Dropline packages..."
	cd /var/log/packages
	for file in *-dropline-*
	do
		if [ "$file" == "*-dropline-*" ]; then
			continue;
		fi
		if [ "open-office-dropline-1.0.1-i386-1" != "$file" ]; then
			removepkg $file
			# check for ctl-c
			if [ "$?" == "130" ]; then
				umask -S $OLD_UMASK  &> /dev/null
				reset
				exit 130
			fi
		fi			
	done
	cd "$dir"
	
	echo "Creating package list..."
			
	ls /var/log/packages > package_list
	let "items = 0"
	let "updates = 0"
	word=installed
	while [ -n "$word" ]
	do
		read word
		required="`echo $word | cut -f 2 -d ':'`"
		word="`echo $word | cut -f 1 -d ':'`"
		package=${word/".tgz"/""}
		found=`grep "^$package" package_list`
		
		if [ "$required" == "obsolete" ]; then
			DroplineFiles[$index]="installed"
		elif [ "$found" == "$package" ]; then
			DroplineFiles[$index]="installed"
		else
			if [ "$word" != "" ]; then				
				DroplineFiles[$index]="$word"
				let "updates += 1"
			fi
		fi
		let "index += 1"
	done < "$CACHE/$DroplineFilesVersion"
	
	new_installer="`grep dropline-installer $CACHE/$DroplineFilesVersion | cut -f 1 -d ':' | sed -e 's/\.tgz//g'`"
	if [ "`grep $new_installer package_list`" == "" ]; then
		upgrade_installer=1
		index=2
		count=${#DroplineFiles[@]}
		while [ "$index" -le "$count" ]
		do
			DroplineFiles[$index]="installed"			
			let "index += 1"
		done		
		index=1
	else
		upgrade_installer=0
	fi
	
	if [ "$updates" == "0" ]; then
		echo ""
		echo "There are no updates currently available."
		echo ""

		exit 0
	fi
	
	let "items = $index"
	let "index = 0"
	read proxy_user < ~/.dropline/proxy_username
	read proxy_pass < ~/.dropline/proxy_password
	
	while [ "$index" -le "$items" ]
	do
		if [ "${DroplineFiles[$index]}" != "installed" ]; then
			wget -w 1 -N -t 0 -c -F --proxy-user=$proxy_user --proxy-passwd=$proxy_pass \
				$MIRROR/${DroplineFiles[$index]}
			# check for ctl-c
			if [ "$?" == "130" ]; then
				umask -S $OLD_UMASK  &> /dev/null
				reset
				exit 130
			fi
		fi
		let "index += 1"
	done
	echo "Download complete."
	let "index = 0"
	while [ "$index" -le "$items" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			if [ ! -e "${DroplineFiles[$index]}" ]; then
				echo "Could not open file: ${DroplineFiles[$index]}"
				exit 1;
			else
				md5_real="`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 5 -d ':'`"
				md5_download="`md5sum ${DroplineFiles[$index]} | cut -f 1 -d ' '`"
				if [ "$md5_real" != "$md5_download" ]; then
					echo "MD5 sum of ${DroplineFiles[$index]} is not correct:"
					echo " '$md5_real' != '$md5_download'"
					exit 1;
				fi
			fi
			
			upgradepkg --install-new --reinstall "${DroplineFiles[$index]}"
			# check for ctl-c
			if [ "$?" == "130" ]; then
				umask -S $OLD_UMASK  &> /dev/null
				reset
				exit 130
			fi
		fi
		let "index += 1"
	done
	
	echo ""
	if [ "$upgrade_installer" == "1" ]; then
		echo "The Dropline Installer has been updated to the most recent version and will "
		echo "now exit.  Please run the installer again to detect and install further "
		echo "updates."
	else
		echo "Your system is now up to date."
	fi
	echo ""

	exit 0
}

if [ ! -d ~/.dropline ]; then
	mkdir -p ~/.dropline
fi
if [ ! -e ~/.dropline/cache_dir ]; then
	echo "/var/cache/dropline-installer" > ~/.dropline/cache_dir
fi
if [ ! -e ~/.dropline/proxy_username ]; then
	cat /dev/null > ~/.dropline/proxy_username
fi
if [ ! -e ~/.dropline/proxy_password ]; then
	cat /dev/null > ~/.dropline/proxy_password
fi
if [ ! -e ~/.dropline/download_mirror ]; then
	echo "http://telia.dl.sourceforge.net/sourceforge/dropline-gnome/" > ~/.dropline/download_mirror
fi

MIRROR=`cat ~/.dropline/download_mirror`

cd $CACHE
#ls /var/log/packages > package_list
#for dep in $DEPENDANCIES
#do
#	found=`grep "$dep-" package_list`
#	if [[ "$found" == "" && "$NODEPS" == "False" ]]; then
#		echo ""
#		echo "Error: Required package '$dep' not found.  You must install it before"
#		echo "installing Dropline GNOME.  If you installed $dep from a source tarball,"
#		echo "pass the --nodeps option to the Dropline Installer to disable the"
#		echo "dependancy checking."
#		echo ""
#		exit 1
#	fi
#	if [ "$dep" == "pkgtools" ]; then
#		read SLACK_STRING < /etc/slackware-version
#		SLACK_VERSION=${SLACK_STRING/"Slackware "/""}
#		SLACK_VERSION=${SLACK_VERSION/"."/"0"}
#		if (("$SLACK_VERSION" < 801)); then
#			echo ""
#			echo "Error: Dropline GNOME is designed for Slackware 8.1 and newer systems."
#			echo "Your system appears to be running $SLACK_STRING."
#			echo ""
#			exit 1
#		fi
#	fi
#done

if [ "$AUTO" == "True" ]; then
	auto_update
else
	main_menu
fi
