#!/bin/bash
#
# The Dropline Installer
#
# A package distribution and update system originaly designed for
# Slackware Linux.
#
# Copyright 2002-2004 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.

NAME=dropline-installer
TITLE="Dropline Installer"
CONFIGDIR="dropline"
VERSION=2.4.9
CACHE=/var/cache/$NAME
FORCE="false"
NODEPS="false"
UPGRADE="false"
AUTO="false"
ERRORLOG="/var/log/dropline-installer"
DEFAULT_MIRROR="http://umn.dl.sourceforge.net/sourceforge/dropline-gnome/"
BACKUP_MIRROR_1="http://unc.dl.sourceforge.net/sourceforge/dropline-gnome/"
BACKUP_MIRROR_2="http://aleron.dl.sourceforge.net/sourceforge/dropline-gnome/"
BACKUP_MIRROR_3="http://osdn.dl.sourceforge.net/sourceforge/dropline-gnome/"
# "MIRROR_N" should be the total number of mirror locations
MIRROR_N="4"
MIRROR_CURRENT="0"
DroplineFilesSite=http://www.dropline.net/gnome
DroplineFilesVersion=DroplineFiles2d

# This allows strings to be localized
export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=$NAME

declare MIRROR
declare SOURCE
declare INSTALLTYPE
declare PKGNAME
declare KEEP_OBSOLETE_APPS
declare DROPLINE_FILES
declare -a DroplineFiles

DEP_APPS="dialog
grep
md5sum
perl
sed
sort
wget"

DEP_LIBS="libz.so.1
libjpeg.so.62
libpng.so.3
libtiff.so.3
libcdda_paranoia.so.0
libcdda_interface.so.0
libaspell.so.15
libpcre.so.0
libcups.so.2
libpisync.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

# Clear the error log
cat /dev/null > $ERRORLOG

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

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

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

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

exit_installer ()
{
	umask -S $OLD_UMASK  &> /dev/null
	reset
	exit $1
}

#
# Fill $PKGNAME with the name (no version info) of the $1 parameter
#

get_pkgname ()
{
	PKGNAME=""
	pkg="$1"
	pkg_i="1"
	while [ "`echo "$pkg" | cut -d '-' -f $pkg_i`" != "" ]
	do
		let "pkg_i += 1"
	done
	let "pkg_count = pkg_i - 3"
	for (( pkg_i = 1; pkg_i < $pkg_count; pkg_i++))
	do
		if [ "$pkg_i" == "1" ]; then
			PKGNAME="`echo $pkg | cut -d '-' -f $pkg_i`"
		else
			PKGNAME="$PKGNAME""-`echo $pkg | cut -d '-' -f $pkg_i`"
		fi
	done
}

print_help ()
{
	echo ""
	echo $"Usage: $NAME [options]"
	echo ""
	echo $"The $TITLE is menu-based, so none of these options are required."
	echo ""
	echo $"Options:"
	echo $"  --autoupdate    Do an update without any user input."
	echo $"  --force         Do not check for a supported OS."
	echo $"  --nodeps        Disable dependancy checking."
	echo $"  --upgrade       Go directly to the Upgrade menu."
	echo $"  --view          View available updates."
	echo ""
	umask -S $OLD_UMASK  &> /dev/null
	exit 0
}

print_version ()
{
	echo ""
	echo $"The $TITLE"
	echo $"Copyright 2002-2003 Todd Kulesza"
	echo $"Version $VERSION"
	echo ""
	umask -S $OLD_UMASK  &> /dev/null
	exit 0
}

OPTS=`getopt -o afhnuvw -l autoupdate,force,help,nodeps,upgrade,version,view \
	-n '$NAME' -- "$@"`
if [ "$?" != "0" ]; then
	echo $"Try '$NAME --help'."
	umask -S $OLD_UMASK  &> /dev/null
	exit 1
fi
eval set -- "$OPTS"

while true; do
	case $1 in
		-a|--autoupdate) AUTO="true"; shift;;
		-f|--force) FORCE="true"; shift;;
		-h|--help) print_help; shift;;
		-n|--nodeps) NODEPS="true"; shift;;
		-u|--upgrade) UPGRADE="true"; shift;;
		-v|--version) print_version; shift;;
		-w|--view) VIEW="true"; shift;;
		--) shift; break;;
	esac
done

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...
echo $"Building library list..."
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 ""
	printf $"Error: Your system is missing required software to install \
Dropline GNOME.  The programs and libraries listed as 'not found' must be \
installed in order to proceed.  Please ensure the following Slackware \
packages are installed:"
	printf "\n\n\taspell cdparanoia elflibs grep perl \
\n\tpilot-link pkgtools sed textutils wget\n\n"
	printf $"These can be downloaded from ftp.slackware.com and installed using \
installpkg."
	printf "\n\n"
	printf $"Exiting..."
	exit 1
fi

download_file_list ()
{
	rm -f $CACHE/$DroplineFilesVersion
	cat /dev/null > $CACHE/wget_output
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"Retrieving File List" \
		--no-kill --begin 3 4 \
		--tailboxbg $CACHE/wget_output 15 70 2>$tempfile
	
	read proxy_user < /etc/$CONFIGDIR/proxy_username
	read proxy_pass < /etc/$CONFIGDIR/proxy_password
		
	wget --progress=dot:binary -t 0 -O $CACHE/$DroplineFilesVersion --proxy-user=$proxy_user --proxy-passwd=$proxy_pass \
		$DroplineFilesSite/$DroplineFilesVersion -o $CACHE/wget_output
	# check for ctl-c
	if [ "$?" == "130" ]; then
		exit_installer $?
	fi
	kill -3 `cat $tempfile`
	
	return
}

show_dialog_success_install ()
{
	touch /etc/$CONFIGDIR/dropline_installed
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Installation Complete" --aspect 6 --trim --cr-wrap \
			--msgbox $"Congratulations!  Your computer is now running the \
Dropline GNOME Desktop.  You may need to reboot for some changes to take \
effect.

You can enable a graphical login by opening the file /etc/inittab and changing \
the line which reads 'id:3:initdefault:' to 'id:4:initdefault:'.

If you have any questions or comments, please feel free to visit the Dropline \
Forums at http://www.dropline.net/forums.

Thanks for using Dropline GNOME!" 0 0

	exit_installer 0
}

show_dialog_success_upgrade ()
{
	touch /etc/$CONFIGDIR/dropline_installed
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Upgrade Complete" --aspect 6 --trim --cr-wrap \
			--msgbox $"Congratulations!  Your computer is now running the \
latest release of  the Dropline GNOME Desktop.  You may need to reboot for some \
changes to take effect.

If you have any questions or comments, please feel free to visit the Dropline \
Forums at http://www.dropline.net/forums.

Thanks for using Dropline GNOME!" 0 0

	exit_installer 0
}

show_dialog_uptodate ()
{
	touch /etc/$CONFIGDIR/dropline_installed
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"No Updates Available" --aspect 25 --trim \
			--msgbox $"Your system is currently up to date.  No \
upgrades are available at this time." 0 0
	
	exit_installer 0
}

show_dialog_installer_upgrade ()
{
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"Installer Updated" --aspect 15 --trim \
		--msgbox $"The $TITLE has been updated to the \
most recent version and will now exit.  Please run the new installer with \
the command '$NAME' to detect and install further updates." 0 0
	
	exit_installer 0
}

show_dialog_success_download ()
{
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"Download Complete" --aspect 21 --trim \
		--msgbox $"The complete set of packages have been downloaded to \
'$CACHE'." 0 0
}

show_dialog_remove_tmp ()
{
        local tmpval
        local retval

        # -n in case someone renamed the root account like a "dayum foo"
        if [ "`ls -dlan /tmp | awk '{ print $1 $3 $4 }'`" = 'drwxrwxrwt00' ]; then
                return
        fi

        $DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
                --title $"Download Complete" --trim \
                --yesno $"It appears that your /tmp directory has it's permissions set \
incorrectly.  If you wish, we can reset the permissions to the proper \
values and avoid some potential problems with X.  It is safe to say yes, \
even if you don't know what this is all about--this screen would not \
appear if there weren't a problem." 0 0

        retval=$?

        if [ $retval -neq $DIALOG_OK ]; then
                return
        fi

        for tmpval in /tmp /tmp/.ICE-unix /tmp/.X11-unix ; do
                chmod 1777 $tmpval &> /dev/null
                chown 0:0 $tmpval &> /dev/null
        done
}

checksums ()
{
	FileLocation="$1"
	dir=`dirname "$1"`
	pdir=`pwd`
	declare -a bad_packs
	n_bad_packs=0
	
	cd "$dir"
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"Checking Package Integrity" --aspect 18 --trim \
		--infobox $"Checking the MD5 sum of each package to ensure \
that none are corrupt.  This may take several minutes, depending on the \
speed of your machine." 0 0

	count=${#DroplineFiles[@]}
	index=0
	
	while [ "$index" -le "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			if [ ! -e "${DroplineFiles[$index]}" ]; then
				bad_packs[$n_bad_packs]="${DroplineFiles[$index]} "$"(missing)"
				let "n_bad_packs += 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
					bad_packs[$n_bad_packs]="${DroplineFiles[$index]} "$"(corrupt)"
					let "n_bad_packs += 1"
				fi
			fi
		fi
		let "index += 1"
	done
	
	if [ "$n_bad_packs" -gt "0" ]; then
		cat /dev/null > $CACHE/bad_packs
		echo -e $"\nThe following packages are missing or corrupt:\n" >> $CACHE/bad_packs
		
		index=0
		while [ "$index" -le "$n_bad_packs" ]
		do
			echo "${bad_packs[$index]}" >> $CACHE/bad_packs
			let "index += 1"
		done
		
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Packages Missing or Corrupt" --trim \
			--exit-label "OK" \
			--textbox $CACHE/bad_packs 18 70
					
		return 1
	fi
	
	cd "$pdir"
	
	return 0
}

package_is_obsolete ()
{
	local_package=$1
	
	local_status="`echo "$local_package" | cut -f 2 -d ':'`"
	if [ "$local_status" == "obsolete_lib" ]; then
		return 1
	elif [ "$KEEP_OBSOLETE_APPS" != "true" ]; then
		if [ "$local_status" == "obsolete_app" ]; then
			return 1
		fi
	fi
	
	return 0
}

remove_obsolete_packages ()
{
	dir=$1
	
	if [ ! -e "$dir/$DroplineFilesVersion" ]; then
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Error" --aspect 21 --trim \
			--msgbox $"Could not open file list '$dir/$DroplineFilesVersion'.  You can \
download this file at $DroplineFilesSite/$DroplineFilesVersion, save it in $dir, \
and then try again." 0 0
		return
	fi

	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Removing Obsolete Software" --aspect 21 --trim \
			--infobox $"The Dropline Installer is removing obsolete packages \
from your system.  This may take a few minutes." 0 0

	ls /var/log/packages > $CACHE/package_list
	word=installed
	while [ -n "$word" ]
	do
		read word
		# check for ctl-c
		if [ "$?" == "130" ]; then
			exit_installer $?
		fi
		
		package_is_obsolete "$word"
		obsolete=$?
		if [ "$obsolete" == "1" ]; then
			get_pkgname `echo "$word" | cut -d ':' -f 1`
			word=$PKGNAME
			if [ "$word" != "" ]; then
				blacklisted=`grep "^$PKGNAME$" /etc/$CONFIGDIR/blacklist`
				if [ "$blacklisted" == "" ]; then
					removepkg $word &> /dev/null
				fi
			fi
		elif [ "`echo "$word" | cut -f 2 -d ':'`" == "replaced" ]; then
			get_pkgname `echo "$word" | cut -d ':' -f 1`
			required="`echo "$word" | cut -f 3 -d ':'`""-"
			word=$PKGNAME
			found_required=`grep -c "$required" $CACHE/package_list`
			if [[ "$word" != "" && "$found_required" != "0" ]]; then
				removepkg $word &> /dev/null
			fi
		fi
	done < "$dir/$DroplineFilesVersion"
}

#
# Remove older .tgz's from $CACHE from previous installs which have been 
# replaced by newer packages
#

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

clean_up_old ()
{
	(
	cd $CACHE
	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 $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Uninstall?" --aspect 24 --trim \
			--yesno $"Are you sure you want to uninstall the complete Dropline GNOME Desktop?" \
			0 0 2> $tempfile
	retval=$?
	
	if [ "$retval" != "0" ]; then
		return
	fi
	
	if [ ! -e "$CACHE/$DroplineFilesVersion" ]; then
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Error" --aspect 21 --trim \
			--msgbox $"Could not open file list '$dir/$DroplineFilesVersion'.  You can \
download this file at $DroplineFilesSite/$DroplineFilesVersion, save it in $dir, \
and then try again." 0 0
		return
	fi
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"In Progress" --aspect 50 --trim \
			--infobox $"Creating list of packages to remove..." 0 0
	
	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
			get_pkgname `echo "$word" | cut -d ':' -f 1`
			word=$PKGNAME
			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
					exit_installer $?
				fi
			fi
			let "index += 1"
		fi
	done < "$CACHE/$DroplineFilesVersion"
	) |
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"Removing Packages" \
		--gauge "" 7 65
	
	rm -f /etc/$CONFIGDIR/dropline_installed
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Uninstall Complete" --aspect 15 --trim \
			--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." 0 0
}

#
# Install or upgrade the selected files
#

install_files ()
{
	INSTALL="$1"
	dir=`dirname "$2"`
	TYPE="$3"
	upgrade_installer="$4"
	
	# 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 $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Obsolete Packages Detected" --aspect 12 --trim \
			--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? " 0 0 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
		printf $"Before installing, the $TITLE will remove obsolete Dropline GNOME packages.  \
This may take several minutes."
		printf "\n\n"
		printf $"Removing $file..."
		echo "XXX"
		
		removepkg $file &> /dev/null
		
		# check for ctl-c
		if [ "$?" == "130" ]; then
			break;
		fi
		
		let "install_index += 1"				
	done
	) |
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"Removing Obsolete Packages" \
		--gauge "" 11 65
	if [ "$?" == "130" ]; then
		exit_installer $?
	fi
	
	cd $dir
	count=${#DroplineFiles[@]}
	index=0
	percent=0
	install_count=0
	install_index=1
	while [ "$index" -le "$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
			size=`grep ^${DroplineFiles[$index]} $DROPLINE_FILES | cut -f 6 -d ':'`
			let "size /= 1024"
			desc=`grep ^${DroplineFiles[$index]} $DROPLINE_FILES | cut -f 3 -d ':' | cut -f 1 -d '('`
			echo "XXX"
			let "percent = install_index * 100 / install_count"
			echo $percent
			echo $"Installing package $install_index of $install_count"
			echo ""
			echo $"Name: ${DroplineFiles[$index]}"
			echo $"Size: $size KB"
			echo $"Description: $desc"
			echo "XXX"
			
			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 $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"Installing Packages" \
		--gauge "" 11 65 0
	
	if [ "$?" == "130" ]; then
		exit_installer $?
	fi
	
	if [ "`cat $tempfile`" != "" ]; then
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Installation Error" --aspect 21 --trim \
			--msgbox "`cat $tempfile`" 0 0
	else		
		if [ "$INSTALL" == "1" ];then
			remove_obsolete_packages $dir
			show_dialog_remove_tmp
			show_dialog_success_install
		else
			if [ "$upgrade_installer" == "1" ]; then
				show_dialog_installer_upgrade
			else
				remove_obsolete_packages $dir
				show_dialog_remove_tmp
				show_dialog_success_upgrade
			fi
		fi
	fi

	cd $CACHE
}

next_mirror ()
{
	case "$MIRROR_CURRENT" in
		"0" )
			MIRROR="${BACKUP_MIRROR_1}"
			MIRROR_CURRENT="1"
			;;
		"1" )
			MIRROR="${BACKUP_MIRROR_2}"
			MIRROR_CURRENT="2"
			;;
		"2" )
			MIRROR="${BACKUP_MIRROR_3}"
			MIRROR_CURRENT="3"
			;;
		* )
			MIRROR="${DEFAULT_MIRROR}"
			MIRROR_CURRENT="0"
			;;
	esac
}

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;
	retval="0"
	attempt=0
	while [[ "$index" -le "$count" ]]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			echo "" > $CACHE/wget_output
			
			$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
				--title $"Downloading ${DroplineFiles[$index]} ($download_index of $download_count)" \
				--no-kill \
				--tailboxbg $CACHE/wget_output 21 78 2>$tempfile
			
			read proxy_user < /etc/$CONFIGDIR/proxy_username
			read proxy_pass < /etc/$CONFIGDIR/proxy_password
			rm -f "$CACHE/${DroplineFiles[$index]}\?download\&failedmirror*"
			wget --progress=dot:binary -w 1 -N -t 0 -c -F --proxy-user=$proxy_user --proxy-passwd=$proxy_pass \
				$MIRROR/${DroplineFiles[$index]} \
				-o $CACHE/wget_output
			retval="$?"
			
			# check for ctl-c
			if [ "$retval" == "130" ]; then
				exit_installer $?
			fi
			
			kill -3 `cat $tempfile`
			output=`ls $CACHE | grep "^${DroplineFiles[$index]}?download\&failedmirror"`
			# verify that the file was downloaded successfully; if not, try another mirror
			if [[ ( ! -e "${DroplineFiles[$index]}" || "$output" != "" ) && "${attempt}" -lt "${MIRROR_N}" ]]; then
				next_mirror
				let "attempt += 1"
				continue
			else
				attempt=0
			fi
			
			let "download_index += 1"
		fi
		let "index += 1"
	done
	
	return "$retval"
}

fill_file_list ()
{
	index=0
	word="foo"
	unset DroplineFiles
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"In Progress" --aspect 50 --trim \
			--infobox $"Building file list..." 0 0
	
	while [ -n "$word" ]
	do
		read word
		word="`echo $word | cut -f 1 -d ':'`"
		DroplineFiles[$index]="$word"
		let "index += 1"
	done < "$DROPLINE_FILES"
}

display_category_list ()
{	
	FileLocation=$1
	index=0
	display=0
	count=${#DroplineFiles[@]}
	
	echo -e "-----\nentering display_category_list..." >> $ERRORLOG
	echo "count = $count" >> $ERRORLOG
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"In Progress" --aspect 50 --trim \
			--infobox $"Building category list..." 0 0
	
	cat /dev/null > $CACHE/DroplineFilesCats
	while [ "$index" -le "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			category="`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 8 -d ':'`"
			os="`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 7 -d ':'`"
			found_os=`echo $os | grep $LOCAL_OS_ID`
			found=`grep -c "$category" $CACHE/DroplineFilesCats`
			if [[ "$category" != "" && "$found" == "0" && "$found_os" != "" ]]; then
				echo "adding '$category' to $CACHE/DroplineFilesCats" >> $ERRORLOG
				echo "$category" >> $CACHE/DroplineFilesCats
				let "display += 1"
			fi
		fi
		let "index += 1"
	done
	
	echo "display = $display" >> $ERRORLOG
	echo "###" >> $ERRORLOG
	cat $CACHE/DroplineFilesCats >> $ERRORLOG
	
	if [ "$display" == "0" ]; then
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Error" --aspect 24 --trim \
			--msgbox $"No categories were detected." 0 0
		return 1
	fi
	
	echo "sorting $CACHE/DroplineFilesCats..." >> $ERRORLOG
	sort -o $CACHE/DroplineFilesCats2 $CACHE/DroplineFilesCats
	mv $CACHE/DroplineFilesCats2 $CACHE/DroplineFilesCats
	echo "###" >> $ERRORLOG
	cat $CACHE/DroplineFilesCats >> $ERRORLOG
	
	echo '#!/bin/bash' > $CACHE/category_selection
	echo -e "DIALOG=\"$DIALOG\"" >> $CACHE/category_selection
	echo -e "TITLE=\"$TITLE\"" >> $CACHE/category_selection
	echo -e "LOCAL_OS=\"$LOCAL_OS\"" >> $CACHE/category_selection
	echo -e "LOCAL_VERSION=\"$LOCAL_VERSION\"" >> $CACHE/category_selection
	echo -e "display=\"$display\"" >> $CACHE/category_selection
	echo '$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Select Categories" --separate-output \
			--checklist $"Use the space-bar to select which categories you wish \
to use packages from:" 15 70 $display \' >> $CACHE/category_selection
	
	if [ ! -e $CACHE/DroplineFilesDislikedCats ]; then
		cat /dev/null > $CACHE/DroplineFilesDislikedCats
	fi
	
	word="foo"
	while [ -n "$word" ]
	do
		read word
		echo "processing '$word'..." >> $ERRORLOG
		case "$word" in
			"Application Development" ) desc=$"Packages for compiling software";
			;;
			"Desktop Extras" ) desc=$"Utilities to enhance your desktop";
			;;
			"GNOME Desktop" ) desc=$"The core GNOME platform";
			;;
			"Internet Software" ) desc=$"Communication and network programs";
			;;
			"Media Software" ) desc=$"Audio, video, and image packages";
			;;
			"Office Applications" ) desc=$"Office and productivity packages";
			;;
			"System Software" ) desc=$"Required system packages";
			;;
			* ) desc=$"No description available";
		esac
		if [ "$word" != "" ]; then
			disliked=`grep "$word" $CACHE/DroplineFilesDislikedCats`
			if [ "$disliked" == "" ]; then
				echo "\"$word\" \"$desc\" on \\" >> $CACHE/category_selection
			else
				echo "\"$word\" \"$desc\" off \\" >> $CACHE/category_selection
			fi
		else
			echo "this category was blank.  this is probably an error." >> $ERRORLOG
		fi
		
	done < $CACHE/DroplineFilesCats
	
	echo " 2> $tempfile" >> $CACHE/category_selection
	echo "echo \$? > $CACHE/category_select_result" >> $CACHE/category_selection
	echo "running $CACHE/category_selection..." >> $ERRORLOG
	bash $CACHE/category_selection
	retval=`cat $CACHE/category_select_result`
	echo "retval = '$retval'" >> $ERRORLOG
	if [ "$retval" == "0" ]; then
		cp $tempfile $CACHE/DroplineFilesSelectCats
	else
		echo "returning with error 1" >> $ERRORLOG
		return 1
	fi
	
	word="foo"
	cat /dev/null > $CACHE/DroplineFilesDislikedCats
	while [ -n "$word" ]
	do
		read word
		found=`grep "$word" $CACHE/DroplineFilesSelectCats`
		if [ "$found" == "" ]; then
			echo $word >> $CACHE/DroplineFilesDislikedCats
		fi
	done < $CACHE/DroplineFilesCats
	
	echo "returning without error" >> $ERRORLOG
	
	return 0
}

#
# Run through the given category, appending the user's package choices to a file
#

display_package_list ()
{
	FileLocation=$1
	Category=$2
	index=0
	display=0
	count=${#DroplineFiles[@]}
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"In Progress" --aspect 65 --trim \
			--infobox $"Building package list for $Category..." 0 0
	
	cat /dev/null > $CACHE/DroplineFilesSelect
	while [ "$index" -le "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			category="`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 8 -d ':'`"
			os="`grep ^${DroplineFiles[$index]} $FileLocation | cut -f 7 -d ':'`"
			found_os=`echo $os | grep $LOCAL_OS_ID`
			
			if [[ "$category" == "$Category" && "$found_os" != "" ]]; then
				echo "${DroplineFiles[$index]}" >> $CACHE/DroplineFilesSelect
				let "display += 1"
			fi
		fi
		let "index += 1"
	done
	
	if [ "$display" -gt "10" ]; then
		display=10
	fi
	
	echo '#!/bin/bash' > ./package_selection
	echo -e "DIALOG=\"$DIALOG\"" >> ./package_selection
	echo -e "TITLE=\"$TITLE\"" >> ./package_selection
	echo -e "LOCAL_OS=\"$LOCAL_OS\"" >> ./package_selection
	echo -e "LOCAL_VERSION=\"$LOCAL_VERSION\"" >> ./package_selection
	echo -e "display=\"$display\"" >> ./package_selection
	echo -e "Category=\"$Category\"" >> ./package_selection
	echo '$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title "$Category" --separate-output --item-help \
			--checklist $"Use the space-bar to select packages:" 17 55 $display \' >> ./package_selection
	word="foo"
	while [ -n "$word" ]
	do
		read word
		if [ "$word" != "" ]; then
			size=`grep ^$word $FileLocation | cut -f 6 -d ':'`
			let "size /= 1024"
			desc=`grep ^$word $FileLocation | cut -f 3 -d ':'`
			desc="$desc ($size KB)"
			get_pkgname "$word"
			name=$PKGNAME
			disliked=`grep "^$name" $CACHE/DroplineFilesDisliked`
			if [ "$disliked" == "" ]; then
				echo "\"$word\" \"\" on \"$desc\" \\" >> ./package_selection
			else
				echo "\"$word\" \"\" off \"$desc\" \\" >> ./package_selection
			fi
		fi
	done < $CACHE/DroplineFilesSelect
	
	echo " 2> $tempfile" >> ./package_selection
	echo "echo \$? > $CACHE/package_select_result" >> ./package_selection
	bash ./package_selection
	retval=`cat $CACHE/package_select_result`
	if [ "$retval" == "0" ]; then
		cat $tempfile $CACHE/DroplineFilesSelect2 > $CACHE/DroplineFilesSelect3
		mv $CACHE/DroplineFilesSelect3 $CACHE/DroplineFilesSelect2
	else
		if [ "$display" != "0" ]; then
			return 1
		fi
	fi
	
	return 0
}

#
# Figure out where DroplineFiles resides
#

set_file_location ()
{
	if [ "$SOURCE" == $"Internet" ]; then
		download_file_list
		FileLocation=$CACHE/$DroplineFilesVersion
	elif [ "$SOURCE" == $"CD-ROM" ]; then
		FileLocation=/mnt/cdrom/dropline-gnome/$DroplineFilesVersion
	elif [ "$SOURCE" == $"Hard Disk" ]; then
		FileLocation=$CACHE/$DroplineFilesVersion
	else
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Error" --aspect 24 --trim \
			--msgbox $"Invalid source media.  Defaulting to 'Internet'." 0 0
		SOURCE=$"Internet"
		set_file_location
		
		return 0
	fi
	
	DROPLINE_FILES=$FileLocation
	
	if [ ! -e $FileLocation ]; then
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Error" --aspect 24 --trim \
			--msgbox $"Could not open file list '$FileLocation'." 0 0
		return 1
	fi
	
	return 0
}

install_select_files ()
{
	INSTALL=$1
	
	unset DroplineFiles
	
	if [ "$INSTALL" == "1" ]; then
		verb1_caps=$"Installation"
		verb1=$"installation"
		verb1_past=$"installed"
		verb2=$"install"
		menu1=$"Install the complete Dropline GNOME system"
		menu2=$"Choose which packages to install"
		menu3=$"Only install the core GNOME packages"
	else
		verb1_caps=$"Upgrade"
		verb1=$"upgrade"
		verb1_past=$"upgraded"
		verb2=$"upgrade"
		menu1=$"Install all available updates"
		menu2=$"Choose which updates to install"
		menu3=$"Only update the core GNOME packages"
	fi
	
	set_file_location
	if [ "$?" != "0" ]; then
		return 1
	fi
	
	fill_file_list $FileLocation
	
	display_category_list $FileLocation
	if [ "$?" != "0" ]; then
		return 1
	fi
	
	if [ ! -e $CACHE/DroplineFilesDisliked ]; then
		cat /dev/null > $CACHE/DroplineFilesDisliked
	fi
	
	default=`cat /etc/$CONFIGDIR/install_type`
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Select $verb1_caps Type" --cr-wrap \
			--default-item "$default" \
    	    --menu $"What type of $verb1 would you like?  In all \
cases, only packages in the categories previously selected will be $verb1_past.
" 11 70 3 \
    	    $"Full" "$menu1" \
    	    $"Selective" "$menu2" \
    	    $"Minimal" "$menu3" 2> $tempfile

	retval=$?
	choice=`cat $tempfile`
	if (($retval != 0)); then
		return 1
	fi
	echo "$choice" > /etc/$CONFIGDIR/install_type
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"In Progress" --aspect 50 --trim \
			--infobox $"Creating list of packages to $verb2..." 0 0
	
	cat /dev/null > $CACHE/DroplineFilesUpgrade
	ls /var/log/packages > $CACHE/package_list
	installer_package="`grep $NAME $FileLocation | cut -f 1 -d ':'`"
	items=0
	index=0
	unset DroplineFiles
	word="foo"
	while [ -n "$word" ]
	do
		read word
		if [ "$word" != "" ]; then
			required="`echo $word | cut -f 2 -d ':'`"
			os="`echo $word | cut -f 7 -d ':'`"
			category="`echo $word | cut -f 8 -d ':'`"
			word="`echo $word | cut -f 1 -d ':'`"
			package=${word/".tgz"/""}
			found=`grep "^$package" $CACHE/package_list`
			found_os=`echo $os | grep $LOCAL_OS_ID`
			cat_found=`grep "$category" $CACHE/DroplineFilesSelectCats`
			
			package_is_obsolete "$word"
			obsolete=$?
			if [ "$obsolete" == "1" ]; then
				DroplineFiles[$index]="installed"
			elif [ "$required" == "replaced" ]; then
				DroplineFiles[$index]="installed"
			elif [[ "$choice" == "Minimal" && "$required" != "" && "$required" != "required" ]]; then
				DroplineFiles[$index]="installed"
			elif [[ "$INSTALL" == "1" && "$installer_package" == "$word" ]]; then
				DroplineFiles[$index]="installed"
			elif [[ "$INSTALL" != "1" && "$found" == "$package" ]]; then
				DroplineFiles[$index]="installed"
			elif [ "$found_os" == "" ]; then
				DroplineFiles[$index]="installed"
			elif [ "$cat_found" == "" ]; then
				DroplineFiles[$index]="installed"
			else
				get_pkgname `echo "$word" | cut -d ':' -f 1`
				blacklisted=`grep "^$PKGNAME$" /etc/$CONFIGDIR/blacklist`
				if [ "$blacklisted" == "" ]; then
					DroplineFiles[$index]="$word"
					echo $PKGNAME >> $CACHE/DroplineFilesUpgrade
					let "items += 1"
				else
					DroplineFiles[$index]="installed"
				fi
			fi
			let "index += 1"
		fi
	done < "$FileLocation"
	
	if [[ "$INSTALL" != "1" && "`grep $NAME $CACHE/DroplineFilesUpgrade`" != "" ]]; then
		upgrade_installer="1"
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"New Installer Available" --aspect 24 --trim \
			--msgbox $"An updated version of the $TITLE is available \
and must be installed before further updates can proceed." 0 0
		index=1
		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" && "$upgrade_installer" == "0" ]]; then 
		cat=foo
		cat /dev/null > $CACHE/DroplineFilesSelect2
		while [ -n "$cat" ]
		do
			read cat
			if [ "$cat" != "" ]; then
				display_package_list $FileLocation "$cat"
				if [ "$?" != "0" ]; then
					return 1
				fi
			fi
		done < $CACHE/DroplineFilesSelectCats
		
		index=0
		count=${#DroplineFiles[@]}
		while [ "$index" -le "$count" ]
		do
			found=`grep ^${DroplineFiles[$index]} $CACHE/DroplineFilesSelect2`
			if [ "$found" == "" ]; then
				DroplineFiles[$index]="installed"
			fi
			let "index += 1"
		done
		
		# Save the unwanted package list
		
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"In Progress" --aspect 50 --trim \
			--infobox $"Parsing selections..." 0 0
		
		# make a list of every pkg not selected in the categories which were 
		# selected
		
		cat /dev/null > $CACHE/DroplineFilesDisliked2
		word="foo"
		while [ -n "$word" ]
		do
			read word
			if [ "$word" != "" ]; then
				category="`echo \"$word\" | cut -f 8 -d ':'`" 
				package="`echo \"$word\" | cut -f 1 -d ':'`"
				get_pkgname "`echo \"$word\" | cut -f 1 -d ':'`" 
				cat_found=`grep "$category" $CACHE/DroplineFilesSelectCats`
				word=$PKGNAME
				
				if [ "$cat_found" == "$category" ]; then
					pkg_found=`grep "$package" $CACHE/DroplineFilesSelect2`
					if [ "$pkg_found" != "$package" ]; then
						# don't mark the dropline-installer as disliked automatically
						if [ "$word" != "dropline-installer" ]; then
							echo "Marking '$word' in '$category' as disliked" >> $ERRORLOG
							echo $word >> $CACHE/DroplineFilesDisliked2
						fi
					fi
				fi
			fi
		done < $FileLocation
		
		# search the old list for pkgs in not-selected categories
		
		cat /dev/null > $CACHE/DroplineFilesDisliked3
		word="foo"
		while [ -n "$word" ]
		do
			read word
			if [ "$word" != "" ]; then
				category="`echo \"$word\" | cut -f 8 -d ':'`" 
				get_pkgname "`echo \"$word\" | cut -f 1 -d ':'`" 
				cat_found=`grep "$category" $CACHE/DroplineFilesSelectCats`
				word=$PKGNAME
				
				if [ "$cat_found" != "$category" ]; then
					pkg_found=`grep "^$word" $CACHE/DroplineFilesDisliked`
					
					if [ "$pkg_found" == "$word" ]; then
						echo "$word" >> $CACHE/DroplineFilesDisliked
					fi
				fi
			fi
		done < $FileLocation
		
		# concat the lists together
		
		cat $CACHE/DroplineFilesDisliked2 $CACHE/DroplineFilesDisliked3 > $CACHE/DroplineFilesDisliked
	else
		# If we're doing a full or minimal install, disliked packages don't matter
		cat /dev/null > $CACHE/DroplineFilesDisliked
	fi
	
	if [[ "$SOURCE" == $"Internet" && "$items" -gt "0" ]]; then
		download_files
	fi
	
	if [ "$items" == "0" ]; then
		show_dialog_uptodate
	else
		checksums "$FileLocation"
		if [ "$?" == "0" ]; then
			install_files "$INSTALL" "$FileLocation" "$choice" "$upgrade_installer"
		fi
	fi
}

download_only ()
{
	unset DroplineFiles
		
	set_file_location
	if [ "$?" != "0" ]; then
		return 1
	fi
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"In Progress" --aspect 50 --trim \
			--infobox $"Building file list..." 0 0
	
	word="foo"
	index=0
	while [ -n "$word" ]
	do
		read word
		if [ "$word" != "" ]; then
			type="`echo $word | cut -f 2 -d ':'`"
			if [[ "$type" = "required" || "$type" = "optional" ]]; then
				word="`echo $word | cut -f 1 -d ':'`"
				DroplineFiles[$index]="$word"
				let "index += 1"
			fi
		fi
	done < "$DROPLINE_FILES"
		
	download_files
	
	clean_up_old
	
	checksums "$DROPLINE_FILES"
	if [ "$?" == "0" ]; then
		show_dialog_success_download
	fi
}

view_updates ()
{
	unset DroplineFiles

	download_file_list
	
	if [ ! -e $CACHE/$DroplineFilesVersion ]; then
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Error" --aspect 50 --trim \
			--msgbox $"Could not open file list '$CACHE/$DroplineFilesVersion'." 0 0
		return
	fi
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"In Progress" --aspect 50 --trim \
		--infobox $"Creating list of packages to upgrade..." 0 0
			
	cat /dev/null > $CACHE/DroplineFilesUpgrade
	ls /var/log/packages > $CACHE/package_list
	items=0
	word=installed
	cat /dev/null > $CACHE/DroplineFilesUpgrade
	while [ -n "$word" ]
	do
		read word
		required="`echo $word | cut -f 2 -d ':'`"
		os="`echo $word | cut -f 7 -d ':'`"
		word="`echo $word | cut -f 1 -d ':'`"
		package=${word/".tgz"/""}
		found=`grep "^$package" $CACHE/package_list`
		found_os=`echo $os | grep $LOCAL_OS_ID`
		
		package_is_obsolete "$word"
		obsolete=$?
		if [ "$obsolete" == "1" ]; then
			DroplineFiles[$index]="installed"
		elif [[ "$choice" == "Minimal" && "$required" != "" && "$required" != "required" ]]; then
			DroplineFiles[$index]="installed"
		elif [ "$found" == "$package" ]; then
			DroplineFiles[$index]="installed"
		elif [ "$found_os" == "" ]; then
			DroplineFiles[$index]="installed"
		else
			if [ "$word" != "" ]; then
				get_pkgname `echo "$word"`
				blacklisted=`grep "^$PKGNAME$" /etc/$CONFIGDIR/blacklist`
				if [ "$blacklisted" == "" ]; then
					DroplineFiles[$index]="$word"
					echo "$word" >> $CACHE/DroplineFilesUpgrade
					let "items += 1"
				else
					DroplineFiles[$index]="installed"
				fi
			fi
		fi
		let "index += 1"
	done < "$CACHE/$DroplineFilesVersion"
	
	if [ "$items" == "0" ]; then
		show_dialog_uptodate
	else
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Available Packages" \
			--exit-label $"Back" \
			--textbox $CACHE/DroplineFilesUpgrade 19 50
	fi

	if [ "$VIEW" == "true" ]; then
		$DIALOG --clear
		umask -S $OLD_UMASK  &> /dev/null
		rm $CACHE/package_list
		reset
		exit 0
	else
		return;
	fi
}

set_source ()
{
	if [ "$SOURCE" == $"CD-ROM" ]; then
		net="off"
		cd="on"
		local="off"
	elif [ "$SOURCE" == $"Hard Disk" ]; then
		net="off"
		cd="off"
		local="on"
	else
		net="on"
		cd="off"
		local="off"
	fi

	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Select Source Media" \
			--radiolist $"Where are your Dropline GNOME packages located?" 10 70 3 \
			$"Internet" $"Download the latest files from the Internet" "$net" \
			$"CD-ROM" $"Use the files on an official Dropline CD-ROM" "$cd" \
			$"Hard Disk" $"Use pre-downloaded files on your hard drive" "$local" \
			2> $tempfile
	
	retval=$?
	choice=`cat $tempfile`
	if [ "$retval" == "0" ]; then
		echo $choice | sed -e 's/\"//g' > /etc/$CONFIGDIR/source_media
	fi
	SOURCE=`cat /etc/$CONFIGDIR/source_media`
}

set_proxy ()
{
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--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 /etc/.wgetrc ]; then
				grep http_proxy /etc/.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 $TITLE - $LOCAL_OS $LOCAL_VERSION" \
					--title $"HTTP Proxy Server" --aspect 12 --trim --cr-wrap \
					--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.

Example: 192.168.1.1:1080
" 0 0 $proxy 2> $tempfile
			
			retval=$?
			proxy=`cat $tempfile`
			if [ "$retval" == "0" ]; then
				if [ -e /etc/.wgetrc ]; then
					sed /http_proxy/d /etc/.wgetrc &> $CACHE/proxy
				else
					cat /dev/null > $CACHE/proxy
				fi
				echo "http_proxy=$proxy" >> $CACHE/proxy
				cp $CACHE/proxy /etc/.wgetrc
			fi
		elif [ "$choice" == $"Set Username" ]; then
			if [ -e /etc/$CONFIGDIR/proxy_username ]; then
				read proxy < /etc/$CONFIGDIR/proxy_username
			else
				proxy=""
			fi
			
			$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
					--title $"HTTP Proxy Username" --aspect 12 --trim --cr-wrap \
					--inputbox $"Enter your username for your proxy server.  \
If you do not have a username, leave this line blank.

Example: molly
" \
0 0 $proxy 2> $tempfile
			
			retval=$?
			proxy=`cat $tempfile`
			if [ "$retval" == "0" ]; then
				echo "$proxy" > /etc/$CONFIGDIR/proxy_username
			fi
		elif [ "$choice" == $"Set Password" ]; then
			if [ -e /etc/$CONFIGDIR/proxy_password ]; then
				read proxy < /etc/$CONFIGDIR/proxy_password
			else
				proxy=""
			fi
			
			$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
					--title $"HTTP Proxy Password" --aspect 12 --trim --cr-wrap \
					--inputbox $"Enter your password for your proxy server.  \
If you do not have a password, leave this line blank.

Example: shuriken
" \
0 0 $proxy 2> $tempfile
			
			retval=$?
			proxy=`cat $tempfile`
			if [ "$retval" == "0" ]; then
				echo "$proxy" > /etc/$CONFIGDIR/proxy_password
			fi
		elif [ "$choice" == $"Back" ]; then
			return
		fi
		retval=0
	else
		return
	fi
	
	set_proxy
}

set_directory ()
{
	if [ -e /etc/$CONFIGDIR/cache_dir ]; then
		read dir < /etc/$CONFIGDIR/cache_dir
	else
		dir="/var/cache/$NAME"
	fi
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Download Directory" --aspect 15 --trim \
			--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." 0 0 $dir 2> $tempfile
	
	retval=$?
	dir=`cat $tempfile`
	if [ "$retval" == "0" ]; then
		echo "$dir" > /etc/$CONFIGDIR/cache_dir
		mkdir -p "$dir" &> /dev/null
	fi
}

set_keep_obsolete_apps ()
{
	if [ -e /etc/$CONFIGDIR/keep_obsolete_apps ]; then
		KEEP_OBSOLETE_APPS=`cat /etc/$CONFIGDIR/keep_obsolete_apps`
	else
		KEEP_OBSOLETE_APPS="false"
	fi
	
	if [ "$KEEP_OBSOLETE_APPS" == "true" ]; then
		yes="on"
		no="off"
	else
		yes="off"
		no="on"
	fi
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Keep Old Programs" \
			--radiolist $"Do you want the Dropline Installer to keep old, obsolete \
applications installed when newer programs are available?  Please note that \
obsolete applications are unsupported and may not work." 11 70 2 \
			$"Yes, keep them anyway" "" "$yes" \
			$"No, remove obsolete programs" "" "$no" \
			2> $tempfile
	
	retval=$?
	choice=`cat $tempfile | sed -e 's/\"//g'`
	if [ "$retval" == "0" ]; then
		if [ "$choice" == $"Yes, keep them anyway" ]; then
			echo "true" > /etc/$CONFIGDIR/keep_obsolete_apps
		else
		 	echo "false" > /etc/$CONFIGDIR/keep_obsolete_apps
		fi
		KEEP_OBSOLETE_APPS=`cat /etc/$CONFIGDIR/keep_obsolete_apps`
	fi
}

set_keep_install_files ()
{
	if [ -e /etc/$CONFIGDIR/keep_install_files ]; then
		KEEP_INSTALL_FILES=`cat /etc/$CONFIGDIR/keep_install_files`
	else
		KEEP_INSTALL_FILES="true"
	fi
	
	if [ "$KEEP_INSTALL_FILES" == "true" ]; then
		yes="on"
		no="off"
	else
		yes="off"
		no="on"
	fi
	
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Keep Install Files" \
			--radiolist $"Do you want the Dropline Installer to save the most recent \
files used to install Dropline GNOME?  This will make reinstallations very easy, since \
the files will not need to be downloaded again." 11 70 2 \
			$"Yes, keep them" "" "$yes" \
			$"No, remove them" "" "$no" \
			2> $tempfile
	
	retval=$?
	choice=`cat $tempfile | sed -e 's/\"//g'`
	if [ "$retval" == "0" ]; then
		if [ "$choice" == $"Yes, keep them" ]; then
			echo "true" > /etc/$CONFIGDIR/keep_install_files
		else
		 	echo "false" > /etc/$CONFIGDIR/keep_install_files
		fi
		KEEP_INSTALL_FILES=`cat /etc/$CONFIGDIR/keep_install_files`
	fi
}

edit_preferences ()
{
	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--clear --cr-wrap --title $"Edit Preferences" \
			--cancel-label $"Back" --default-item "$default" \
    		--menu $"" 12 78 6 \
		$"Set Source" $"Select source media (Net, CD, hard disk)" \
		$"Set Proxy" $"Specify an HTTP proxy server" \
		$"Set Directory" $"Specify a directory to hold downloaded files" \
		$"Keep Old Programs" $"Choose whether to remove obsolete applications" \
		$"Keep Install Files" $"Choose whether to save the installation files" \
		$"Back" $"Go back to the main menu" 2> $tempfile
		
	retval=$?
	choice=`cat $tempfile`
		
	case $retval in
	  0)
	  	if [ "$choice" == $"Set Source" ]; then
			set_source;
		elif [ "$choice" == $"Set Proxy" ]; then
			set_proxy;
		elif [ "$choice" == $"Set Directory" ]; then
			set_directory;
		elif [ "$choice" == $"Keep Old Programs" ]; then
			set_keep_obsolete_apps;
		elif [ "$choice" == $"Keep Install Files" ]; then
			set_keep_install_files;
		fi
		
		# set retval back to 0, unless we want to go 'back'
		if [ "$choice" == $"Back" ]; then
			retval=1
		else
			retval=0
		fi
		;;
	esac
		
	if (($retval == 0)); then
		CACHE=`cat /etc/$CONFIGDIR/cache_dir`
		edit_preferences
	else
		main_menu
	fi
}

main_menu ()
{
	if [ -e /etc/$CONFIGDIR/dropline_installed ]; then
		default=$"Upgrade"
	else
		default=$"Install"
	fi
	
	cd $CACHE
	
	if [ "$UPGRADE" == "true" ]; then
		UPGRADE="false"
		install_select_files "0"
		retval=0
	else
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--clear --cr-wrap --title $"The $TITLE $VERSION" \
			--cancel-label $"Exit" --default-item "$default" \
    		--menu $"Welcome to the $TITLE!

Please select an action to perform:" 16 78 7 \
		$"Install" $"Install a fresh copy of Dropline GNOME" \
		$"Upgrade" $"Upgrade an existing installation" \
		$"View Updates" $"Show a list of available updates" \
		$"Download Only" $"Download packages without installing them" \
		$"Remove" $"Uninstall Dropline GNOME" \
		$"Preferences" $"Edit user preferences" \
		$"Exit" $"Exit the $TITLE" 2> $tempfile

		retval=$?

		choice=`cat $tempfile`

		case $retval in
		  0)
			if [ "$choice" == $"Install" ]; then
				install_select_files "1";
			elif [ "$choice" == $"Upgrade" ]; then
				install_select_files "0";
			elif [ "$choice" == $"View Updates" ]; then
				view_updates;
			elif [ "$choice" == $"Download Only" ]; then
				download_only;
			elif [ "$choice" == $"Remove" ]; then
				remove_files;
			elif [ "$choice" == $"Preferences" ]; then
				edit_preferences;
			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 /etc/$CONFIGDIR/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 < /etc/$CONFIGDIR/proxy_username
	read proxy_pass < /etc/$CONFIGDIR/proxy_password
			
	wget --progress=dot:binary -t 0 -O $CACHE/$DroplineFilesVersion --proxy-user=$proxy_user --proxy-passwd=$proxy_pass \
		$DroplineFilesSite/$DroplineFilesVersion
	# check for ctl-c
	if [ "$?" == "130" ]; then
		exit_installer $?
	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
				exit_installer $?
			fi
		fi			
	done
	cd "$dir"
	
	echo $"Creating package list..."
			
	ls /var/log/packages > $CACHE/package_list
	let "items = 0"
	let "updates = 0"
	word=installed
	while [ -n "$word" ]
	do
		read word
		required="`echo $word | cut -f 2 -d ':'`"
		os="`echo $word | cut -f 7 -d ':'`"
		word="`echo $word | cut -f 1 -d ':'`"
		package=${word/".tgz"/""}
		found=`grep "^$package" $CACHE/package_list`
		found_os=`echo $os | grep $LOCAL_OS_ID`
		
		package_is_obsolete "$word"
		obsolete=$?
		if [ "$obsolete" == "1" ]; then
			DroplineFiles[$index]="installed"
		elif [ "$found" == "$package" ]; then
			DroplineFiles[$index]="installed"
		elif [ "$found_os" == "" ]; then
			DroplineFiles[$index]="installed"
		else
			if [ "$word" != "" ]; then
				get_pkgname `echo "$word"`
				blacklisted=`grep "^$PKGNAME$" /etc/$CONFIGDIR/blacklist`
				if [ "$blacklisted" == "" ]; then
					DroplineFiles[$index]="$word"
					let "updates += 1"
				else
					DroplineFiles[$index]="installed"
				fi
			fi
		fi
		let "index += 1"
	done < "$CACHE/$DroplineFilesVersion"
	
	new_installer="`grep $NAME $CACHE/$DroplineFilesVersion | cut -f 1 -d ':' | sed -e 's/\.tgz//g'`"
	if [ "`grep $new_installer $CACHE/package_list`" == "" ]; then
		upgrade_installer=1
		index=1
		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 < /etc/$CONFIGDIR/proxy_username
	read proxy_pass < /etc/$CONFIGDIR/proxy_password
	
	attempt=0
	while [ "$index" -le "$items" ]
	do
		if [ "${DroplineFiles[$index]}" != "installed" ]; then
			wget --progress=dot:binary -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
				exit_installer $?
			fi
			
			# verify that the file was downloaded successfully; if not, try another mirror
			if [[ ! -e "${DroplineFiles[$index]}" && "${attempt}" -lt "${MIRROR_N}" ]]; then
				next_mirror
				let "attempt += 1"
				continue
			else
				attempt=0
			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
				exit_installer $?
			fi
		fi
		let "index += 1"
	done
	
	echo ""
	if [ "$upgrade_installer" == "1" ]; then
		echo $"The $TITLE has been updated to the most recent \
version and will now exit.  Please run the installer again to detect and \
install further updates."
	else
		echo $"Your system is now up to date."
	fi
	echo ""

	exit 0
}

#
# Figure out what type of system we are
#

if [ -e /etc/collegelinux-version ]; then
	temp=`cat /etc/collegelinux-version`
	LOCAL_OS=`echo $temp | cut -d ' ' -f 1`
	LOCAL_VERSION=`echo $temp | cut -d ' ' -f 2`
	LOCAL_OS_ID="c"
	case $LOCAL_VERSION in
	"2.1" )
		LOCAL_SUPPORTED="true"
		DroplineFilesVersion=DroplineFiles2b
		;;
	*)	LOCAL_SUPPORTED="false"
		;;
	esac
elif [ -e /etc/slackware-version ]; then
	temp=`cat /etc/slackware-version`
	LOCAL_OS=`echo $temp | cut -d ' ' -f 1`
	LOCAL_VERSION=`echo $temp | cut -d ' ' -f 2`
	LOCAL_OS_ID="s"
	
	case $LOCAL_VERSION in
	"8.1" )
		LOCAL_SUPPORTED="true"
		DroplineFilesVersion=DroplineFiles2b
		;;
	"9.0.0" | "9.1.0" | "10.0.0" )
		LOCAL_SUPPORTED="true"
		;;		
	*)	LOCAL_SUPPORTED="false"
		;;
	esac
else
	# We need this to make the category list build correctly
	LOCAL_OS="Unknown System"
	LOCAL_VERSION="Unknown Version"
	LOCAL_OS_ID="s"
	LOCAL_SUPPORTED="false"
fi

if [[ "$LOCAL_SUPPORTED" == "false" && "$FORCE" != "true" ]]; then
	echo ""
	echo "You appear to be running $LOCAL_OS $LOCAL_VERSION.  This is not \
a supported platform for Dropline GNOME.  If you would like to try installing \
anyway, pass the --force parameter to the $TITLE."
	echo ""
	echo "Please press 'Enter' to exit..."
	read
	exit 1
fi

#
# Get the machine architecture
#
MACHINE_ARCH=`uname -m`
if [[ "$MACHINE_ARCH" != "i686" && "$FORCE" != "true" ]]; then
	echo ""
	echo "You appear to be running an $MACHINE_ARCH processor.  Dropline \
GNOME will not work on this architecture.  If you believe this to be a \
detection error, pass the --force parameter to the $TITLE."
	echo ""
	echo "Please press 'Enter' to exit..."
	read
	exit 1
fi

#
# Get the kernel version
#
KERNEL_VERSION=`uname -r`
echo "${KERNEL_VERSION}" > /tmp/kernel_version
KERNEL_VALID=`grep "^2.6\|^2.4" /tmp/kernel_version`
if [[ "${KERNEL_VALID}" == "" && "${FORCE}" != "true" ]]; then
	echo ""
	echo "You appear to be running Linux ${KERNEL_VERSION}.  Dropline \
GNOME is only tested on Linux 2.6 and Linux 2.4, and may exhibit problems when run on older \
kernels.  If you believe this to be a detection error, pass the --force \
parameter to the ${TITLE}."
	echo ""
	echo "Please press 'Enter' to exit..."
	read
	exit 1
fi

#
# Create all of the required files and directories
#

if [ ! -d /etc/$CONFIGDIR ]; then
	mkdir -p /etc/$CONFIGDIR
	if [ -d /root/.dropline ]; then
		cp /root/.dropline/* /etc/${CONFIGDIR}/
	fi
fi
if [ ! -e /etc/$CONFIGDIR/cache_dir ]; then
	echo "/var/cache/$NAME" > /etc/$CONFIGDIR/cache_dir
fi
if [ ! -e /etc/$CONFIGDIR/proxy_username ]; then
	cat /dev/null > /etc/$CONFIGDIR/proxy_username
fi
if [ ! -e /etc/$CONFIGDIR/proxy_password ]; then
	cat /dev/null > /etc/$CONFIGDIR/proxy_password
fi
if [ ! -e /etc/$CONFIGDIR/download_mirror ]; then
	echo "$DEFAULT_MIRROR" > /etc/$CONFIGDIR/download_mirror
fi
if [ ! -e /etc/$CONFIGDIR/source_media ]; then
	echo $"Internet" > /etc/$CONFIGDIR/source_media
fi
if [ ! -e /etc/$CONFIGDIR/install_type ]; then
	echo $"Full" > /etc/$CONFIGDIR/install_type
fi
if [ ! -e /etc/$CONFIGDIR/keep_obsolete_apps ]; then
	echo "false" > /etc/$CONFIGDIR/keep_obsolete_apps
fi
if [ ! -e /etc/$CONFIGDIR/keep_install_files ]; then
	echo "true" > /etc/$CONFIGDIR/keep_install_files
fi
if [ ! -e /etc/$CONFIGDIR/blacklist ]; then
	touch /etc/$CONFIGDIR/blacklist
fi

MIRROR=`cat /etc/$CONFIGDIR/download_mirror`
SOURCE=`cat /etc/$CONFIGDIR/source_media`
INSTALLTYPE=`cat /etc/$CONFIGDIR/install_type`
CACHE=`cat /etc/$CONFIGDIR/cache_dir`
KEEP_OBSOLETE_APPS=`cat /etc/$CONFIGDIR/keep_obsolete_apps`
KEEP_INSTALL_FILES=`cat /etc/$CONFIGDIR/keep_install_files`

if [ "$AUTO" == "true" ]; then
	auto_update
elif [ "$VIEW" == "true" ]; then
       view_updates
else
	main_menu
fi
