#!/bin/bash
#
# The Dropline Installer
#
# A package distribution and update system originaly designed for
# Slackware Linux.
#
# Copyright 2002-2004 Todd Kulesza
# Copyright 2005-2006 The Dropline GNOME Team
#
# 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.14.3
CACHE=/var/cache/$NAME
FORCE="false"
NODEPS="false"
UPGRADE="false"
AUTO="false"
ERRORLOG="/var/log/dropline-installer"

declare MIRROR_CURRENT="0"
declare -a MIRROR_LIST
MIRROR_LIST[0]='http://internap.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[1]='http://voxel.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[2]='http://belnet.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[3]='http://mesh.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[4]='http://heanet.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[5]='http://optusnet.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[6]='http://ovh.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[7]='http://puzzle.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[8]='http://kent.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[9]='http://jaist.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[10]='http://umn.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[11]='http://unc.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[12]='http://aleron.dl.sourceforge.net/sourceforge/dropline-gnome/'
MIRROR_LIST[13]='http://osdn.dl.sourceforge.net/sourceforge/dropline-gnome/'

DroplineFilesSite=http://droplinegnome.org
DroplineFilesVersion=DroplineFiles2.14


# 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 LOCAL_OS_ID="s"
declare SOMETHING_IS_BROKEN=0
declare -a DroplineFiles
declare -a FileLengths
declare tempfile

DEP_APPS="dialog
grep
md5sum
perl
sed
sort
mktemp
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=`mktemp 2>/dev/null` || exit 1
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
# ...and no, there is no way to store the previous setting.

# This really, REALLY needs to be bash!!!
if [ "$BASH" = "" ] ; then
	echo ""
	echo $"Error: This installer will likely malfunction with shells"
	echo $"other than the GNU Bourne Again SHell (bash)."
	echo ""
	exit 1
fi

# We don't need to preserve the "old" value for when we exit because
# the new value only applies to _this_ process.
umask 0022

if [ $EUID != 0 ]; then
	echo ""
	echo $"Error: You must be ROOT to run the $TITLE.  Exiting..."
	echo ""
	exit 1
fi

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

exit_installer ()
{
	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 ""
	exit 0
}

print_version ()
{
	echo ""
	echo $"The $TITLE"
	echo $"Copyright 2002-2003 Todd Kulesza"
	echo $"Version $VERSION"
	echo ""
	exit 0
}

OPTS=`getopt -o afhnuvw -l autoupdate,force,help,nodeps,upgrade,version,view \
	-n '$NAME' -- "$@"`
if [ "$?" != "0" ]; then
	echo $"Try '$NAME --help'."
	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 ()
{
	local retval 
	local wgetoutput pidfile

	rm -f $CACHE/$DroplineFilesVersion

	wgetoutput=`tempfile -p wget` || exit 1
	pidfile=`tempfile -p dialog` || exit 1

	read proxy_user < /etc/$CONFIGDIR/proxy_username
	read proxy_pass < /etc/$CONFIGDIR/proxy_password
	read limit_rate < /etc/$CONFIGDIR/limit_rate

	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"Retrieving File List" \
		--no-kill \
		--tailboxbg $wgetoutput 15 70 2>$pidfile
	
	# It actually takes a moment for the pidfile to be populated!
  sleep 1

	wget --progress=dot:binary -t 3 -T 30 -w 1 \
	  ${limit_rate:+--limit-rate=$limit_rate} \
		${proxy_user:+--proxy-user=$proxy_user} \
		${proxy_pass:+--proxy-passwd=$proxy_pass} \
		-O $CACHE/$DroplineFilesVersion -o $wgetoutput \
		$DroplineFilesSite/$DroplineFilesVersion
	retval=$?

	if [ "`cat $pidfile`" = "" ]; then
		sleep 1
	fi
	kill -3 `cat $pidfile`
	rm -f $wgetoutput $pidfile

	if [ "$retval" -eq 130 ]; then
		exit_installer $?
	fi

  if [ "$retval" -ne 0 ]; then
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Oops, wget exited with status: $retval" --trim --cr-wrap \
			--msgbox $"For some reason we were unable to download the master \
package index.  There are a number of reasons this could occur, but we \
suggest you check your network connection and make sure the Dropline \
website is actually up, and then try again. \n\n\
Press enter to return to the main menu." 0 0
	fi
	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://forums.droplinegnome.org.

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://forums.droplinegnome.org.

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 -ne $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
                         grep -q "^$word.*dl$" $CACHE/package_list # We only care about ones ending in dl
                         if [ $? == 0 -a "$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 ()
{
	(( MIRROR_CURRENT++ ))
	if [ $MIRROR_CURRENT -ge ${#MIRROR_LIST[@]} ]; then
		MIRROR_CURRENT=0
	fi
	MIRROR=${MIRROR_LIST[$MIRROR_CURRENT]}
}

random_mirror ()
{
	(( MIRROR_CURRENT = RANDOM % ${#MIRROR_LIST[@]} ))
	MIRROR=${MIRROR_LIST[$MIRROR_CURRENT]}
}

merciful_wget () 
{
	local filename=${1:?'Internal error: merciful_wget requires a filename!'}
	local filelength=${2:?'Internal error: merciful_wget requires a file length!'}
	local attempt=0
	local success=0
	local proxy_user proxy_pass limit_rate
	local length retval pidfile wgetoutput

	if [ -e "$CACHE/$filename" ]; then
		length=`ls -al "$CACHE/$filename" | awk '{ print $5 }'`
		if [ $length -eq $filelength ]; then
			return 0 # ...because we already have the file, we hope. 
		fi
	fi

	read proxy_user < /etc/$CONFIGDIR/proxy_username
	read proxy_pass < /etc/$CONFIGDIR/proxy_password
	read limit_rate < /etc/$CONFIGDIR/limit_rate

	if [ "$MIRROR" = "" ]; then
		random_mirror
	fi

  until [ $attempt -gt ${#MIRROR_LIST[@]} ] ; do

		pidfile=`tempfile -p dialog` || exit 1
		wgetoutput=`tempfile -p wget` || exit 1
	
		$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
			--title $"Downloading $filename ($download_index of $download_count)" \
			--no-kill --tailboxbg $wgetoutput 21 78 2>$pidfile

		wget --progress=dot:binary -w 1 -t 1 -T 30 \
			${limit_rate:+--limit-rate=$limit_rate} \
			${proxy_user:+--proxy-user=$proxy_user} \
			${proxy_pass:+--proxy-passwd=$proxy_pass} \
			$MIRROR$filename \
			-o $wgetoutput -O "$CACHE/$filename"
		retval=$?

		# Let's not have any more zombies lurking about...
		if [ "`cat $pidfile`" = "" ]; then
			sleep 1 # Sometimes it takes a moment for dialog to background itself
		fi
		kill -3 `cat $pidfile`
		rm -f $pidfile $wgetoutput

		# Check for ^C
		if [ $retval -eq 130 ]; then
			rm -f "$CACHE/$filename"
			exit_installer $?
		fi

		# Wget's exit status might as well be random for all the good it does.
		# Did we at least get *something*?
		if [ -r "$CACHE/$filename" ]; then
			length=`ls -al "$CACHE/$filename" | awk '{ print $5 }'`
    	if [ $length -eq $filelength ]; then
				# Well, it looks supefficially correct...
				success=1
				break
			fi
		fi

		rm -f "$CACHE/$filename"
		(( attempt++ ))
		next_mirror

	done

	if [ $success -ne 1 ]; then
		return 1
	fi
}


download_files ()
{		
	count=${#DroplineFiles[@]}
	index=0
	download_count=0
	download_index=1
	
	# Seems to be counting the number of packages needing to be downloaed...
	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

			merciful_wget ${DroplineFiles[$index]} ${FileLengths[$index]}
			(( download_index++ ))
			
		fi
		(( index++ ))
	done
	
	return "$retval"
}

fill_file_list ()
{
	local index=0
	unset DroplineFiles
	unset FileLengths

	$DIALOG --backtitle $"The $TITLE - $LOCAL_OS $LOCAL_VERSION" \
		--title $"In Progress" --aspect 50 --trim \
		--infobox $"Building file list..." 0 0

	while read ; do 
		DroplineFiles[$index]="`echo $REPLY | cut -d: -f1`"
		FileLengths[$index]="`echo $REPLY | cut -d: -f6`"
		(( index++ ))
	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
	unset FileLengths
	local index=0
		
	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
	
	while read ; do
		# Skipping blank lines?
		if [ "$REPLY" != "" ]; then
			type="`echo $REPLY | cut -d: -f2`"
			if [[ "$type" = "required" || "$type" = "optional" ]]; then
				DroplineFiles[$index]="`echo $REPLY | cut -d: -f1`"
				FileLengths[$index]="`echo $REPLY | cut -d: -f6`"
				(( index++))
			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
		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
				reset
				exit 0
			fi
			retval=0
			;;
		esac
	fi
	
	if (($retval == 0)); then
		CACHE=`cat /etc/$CONFIGDIR/cache_dir`
		main_menu
	fi
	
	$DIALOG --clear
	reset
	exit 0
}

auto_update ()
{
	local retval
	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
	read limit_rate < /etc/$CONFIGDIR/limit_rate
			
	logdata "wget --progress=dot:binary -t 0 -O $CACHE/$DroplineFilesVersion \
    ${limit_rate:+--limit-rate=$limit_rate} \
		${proxy_user:+--proxy-user=$proxy_user} \
		${proxy_pass:+--proxy-passwd=$proxy_pass} \
		$DroplineFilesSite/$DroplineFilesVersion"
	wget --progress=dot:binary -t 0 -O $CACHE/$DroplineFilesVersion \
    ${limit_rate:+--limit-rate=$limit_rate} \
		${proxy_user:+--proxy-user=$proxy_user} \
		${proxy_pass:+--proxy-passwd=$proxy_pass} \
		$DroplineFilesSite/$DroplineFilesVersion
	retval=$?
	logdata "Exit status was $retval"
	# check for ctl-c
	if [ "$retval" == "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
	read limit_rate < /etc/$CONFIGDIR/limit_rate
	
	attempt=0
	while [ "$index" -le "$items" ]
	do
		if [ "${DroplineFiles[$index]}" != "installed" ]; then
			wget --progress=dot:binary -w 1 -N -t 0 -c -F \
				${limit_rate:+--limit-rate=$limit_rate} \
				${proxy_user:+--proxy-user=$proxy_user} \
				${proxy_pass:+--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_LIST[@]}" ]]; 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
}

press_enter_to_continue ()
{
	echo ""
	echo $"Please press 'Enter' to continue..."
	read
}

error_not_slackware ()
{
	echo ""
	echo $"Error: Dropline GNOME was designed to be used on Slackware and it's"
	echo $"use on other target distributions is *not* recommended.  We have no"
	echo $"way of predicting what may happen if you try.  Sorry!"
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

error_slackware_too_old ()
{
	echo ""
	echo $"Error: This release of Dropline GNOME was designed to be used on"
	echo $"Slackware 10.2 and you appear to be running Slackware $1."
	echo $"Older versions than this are no longer supported because of the"
	echo $"amount of packages that need to be replaced to make things work."
  echo $"Please, PLEASE, upgrade your Slackware installation.  You will"
	echo $"be very glad you did."
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

error_slackware_too_new ()
{
	echo ""
	echo $"Error: This release of Dropline GNOME was designed to be used on"
	echo $"Slackware 10.2 and you appear to be running a newer version, namely"
	echo $"Slackware $1.  Currently DLG *mostly* works on 11.0 but it is"
	echo $"recommended you wait just a little while for us to get the next"
	echo $"release ready to go before attempting an installation.  You will"
	echo $"be much happier with the result."
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

error_slackware_current ()
{
	echo ""
	echo $"Error: This release of Dropline GNOME was designed to be used on"
	echo $"Slackware 10.2 and you appear to be running something somewhere"
	echo $"between that and 11.0, most likely called -current.  Currently DLG"
	echo $"*mostly* works on 11.0, but -current is too much of a moving target"
	echo $"for us to know from one day to the next whether it will work or not."
	echo $"We recommend you completely upgrade to 11.0 and come back for a"
	echo $"future release."
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

error_wrong_slackware ()
{
	local version=$1
	local major=`echo $version | cut -d. -f1`
	local minor=`echo $version | cut -d. -f2`

	if [ "$major" -lt 10 -o "$minor" -lt 2 ]; then
		error_slackware_too_old $version
	fi

	if [ "$minor" -gt 2 -o "$major" -gt 10 ]; then
		error_slackware_too_new $version
	fi
}

check_slackware_version ()
{
	local tmpval

	if [ -e /etc/slackware-version ]; then
		tmpval=`cat /etc/slackware-version | awk '{ print $2 }'`
		if [ "$tmpval" != '10.2.0' ]; then
			error_wrong_slackware $tmpval
		fi
		LOCAL_OS=`cat /etc/slackware-version | cut -d ' ' -f 1`
		LOCAL_OS_ID="s"
		LOCAL_VERSION=$tmpval
	else
                LOCAL_OS="Unknown System"
                LOCAL_VERSION="Unknown Version"
                LOCAL_OS_ID="s"
		error_not_slackware
	fi
}

error_wrong_arch ()
{
	echo ""
	echo $"Error: Dropline GNOME was designed to be run on an i686 or newer"
	echo $"processor.  You appear to be using an $1, which will cause many"
	echo $"of the binaries to simply fail or otherwise malfunction.  It is"
	echo $"possible that in the future we will bring back compatibility with"
	echo $"older CPUs and other architectures, but in the meantime we very"
	echo $"sincerely recommend you *not* continue with the installation"
	echo $"because you will not be happy with the result.  Sorry!"
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

check_machine_arch ()
{
	local arch=`uname -m`
	if [ "$arch" != 'i686' ]; then
		error_wrong_arch $arch
	fi
}

error_entirely_wrong_kernel ()
{
	echo ""
	echo $"You appear to be running Linux $1.  Dropline GNOME was designed"
	echo $"to run on Linux kernel versions 2.4 and 2.6 ONLY and stands almost"
	echo $"no chance of working properly on substantially older or newer"
	echo $"kernels.  Please upgrade to something reasonably recent before you"
	echo $"attempt to install this again.  Sorry!"
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

error_outdated_twofour ()
{
	echo ""
	echo $"You appear to be running Linux $1.  Dropline GNOME was designed"
	echo $"to run on Linux kernel versions 2.4.31 (which comes with Slackware"
	echo $"10.2) and higher.  Older versions of the kernel may contain bugs"
	echo $"and vulnerabilities that you will certainly wish to upgrade away"
	echo $"from before continuing with this installation.  Thanks!"
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

error_devel_kernel ()
{
	echo ""
	echo $"You appear to be running Linux $1.  Dropline GNOME was designed"
	echo $"to run on Linux kernel versions 2.4.x and 2.6.x.  The 2.5.x set"
	echo $"of kernels may work perfectly well for you, but we have no way"
	echo $"of knowing what will go wrong when DLG is used on a developmental"
	echo $"kernel.  For this reason we recommend you either downgrade to the"
	echo $"latest 2.4.x kernel or go ahead and upgrade to the latest 2.6.x"
	echo $"kernel before attempting to continue with this installation.  Your"
	echo $"computer will thank you for it."
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

error_outdated_twosix ()
{
	echo ""
	echo $"You appear to be running Linux $1.  Dropline GNOME was designed"
	echo $"to run on either the 2.4.x kernel or the 2.6.x kernel, however"
	echo $"there are some bugs in 2.6 kernels older than 2.6.7 that virtually"
	echo $"require you go ahead and upgrade to 2.6.7 or newer.  Please, PLEASE"
	echo $"do so before attempting to continue with the installation."
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

check_kernel_version ()
{
	local version=`uname -r`
	local major=`echo $version | cut -d. -f1`
	local minor=`echo $version | cut -d. -f2`
	# For kernels with interesting strings at the end of the version...
	local teeny=`echo $version | cut -d. -f3 | sed 's/^\([0-9]*\).*/\1/'`

  if [ "$major" -ne 2 ]; then
		error_entirely_wrong_kernel $version
		return
	fi

	if [ "$minor" -eq 4 -a "$teeny" -lt 26 ]; then
		error_outdated_twofour $version	
		return
	fi

	if [ "$minor" -eq 5 ]; then
		error_devel_kernel $version
		return
	fi

	if [ "$minor" -eq 6 -a "$teeny" -lt 7 ]; then
		error_outdated_twosix $version
		return
	fi
}

error_no_cmov_flag ()
{
	echo ""
	echo $"You appear to have a CPU which does not support the cmov instruction"
	echo $"which many Dropline GNOME binaries use.  Your CPU will cause the"
	echo $"applications which use this instruction to *crash immediately*."
	echo $"Bear in mind that in the future we may do some recompiles that will"
	echo $"get around this problem, but for now we recommend that you do not"
	echo $"continue with the installation.  Sorry!"
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

error_cpu_too_slow ()
{
	echo ""
	echo $"Your computer appears to only be capable of $1 bogomips of processing"
	echo $"power.  This is simply not fast enough to be able to run Dropline GNOME"
	echo $"in a manner that you would be happy with.  We recommend you try using"
	echo $"a simpler system like XFCE or Blackbox until you can find a reasonable"
	echo $"upgrade for your machine that can provide at least 400 bogomips of"
	echo $"power.  Sorry!"
	SOMETHING_IS_BROKEN=1
	press_enter_to_continue
}

check_cpu_supports_cmov ()
{
	local cpuflags=`cat /proc/cpuinfo | grep "^flags" | cut -d" " -f 3-`
	local item
	local present=0

	for item in $cpuflags; do
		if [ "$item" = 'cmov' ]; then
			present=1
			break
		fi
	done

	if [ $present -ne 1 ]; then
		error_no_cmov_flag
	fi
}

check_cpu_speed ()
{
	local tmpval=`cat /proc/cpuinfo | grep "^bogomips" | cut -d" " -f 2`
	local bogomips=`echo $tmpval | cut -d. -f 1`
	
	if [ $bogomips -lt 400 ]; then 
		error_cpu_too_slow $bogomips
	fi
}

error_general_brokenness ()
{
	echo ""
	echo $"The above messages are considered fatal or near-fatal errors by"
	echo $"this installer, which will now exit.  Please remedy the problems"
	echo $"mentioned before continuing, or you can *try* passing the --force"
	echo $"option to the installer if you think things will work anyway or that"
	echo $"there was an error in the detection process."
}

if [ "$FORCE" != 'true' ]; then
	check_slackware_version
	check_machine_arch
	check_kernel_version
	check_cpu_supports_cmov
	check_cpu_speed

	if [ $SOMETHING_IS_BROKEN -eq 1 ]; then
		error_general_brokenness
		exit 1
	fi
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/limit_rate ]; then
	touch /etc/$CONFIGDIR/limit_rate
fi
if [ ! -e /etc/$CONFIGDIR/download_mirror ]; then
	echo "" > /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
# vim: ts=2
