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

VERSION=2.0.1
CACHE=/var/cache/dropline-installer
NODEPS="False"
EXIT="0"

declare MIRROR
declare DROPLINE_FILES
declare -a DroplineFiles

DEPENDANCIES="cdparanoia
libgr
libjpeg
libpng
libungif
guile
glib
gtk+
perl
xfree86
pkgtools"

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

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

if [ "`whoami`" != "root" ]; then
	echo ""
	echo "Error: You must be ROOT to run the Dropline Installer.  Exiting..."
	echo ""
	
	exit 1
fi

if [[ -n $1 && $1 != "" ]]; then
	if [ "$1" == "--nodeps" ]; then
		NODEPS="True"
	elif [[ "$1" == "--version" || "$1" == "-v" ]]; then
		echo ""
		echo "The Dropline Installer"
		echo "Copyright 2002-2003 Todd Kulesza"
		echo "Version $VERSION"
		echo ""
		exit 0
	elif [[ "$1" == "--help" || "$1" == "-h" ]]; then
		echo ""
		echo "Usage: dropline-installer [options]"
		echo ""
		echo "Options:"
		echo "  --nodeps   Disable dependancy checking."
		echo ""
		exit 0
	else
		echo ""
		echo "Error: Arguement '$1' unknown.  Try $0 --help."
		echo ""
		exit 1
	fi
fi

package_name()
{
  STRING=`basename $1 .tgz`
  # Check for old style package name with one segment:
    # Count number of segments:
    INDEX=1
    while [ ! "`echo $STRING | cut -f $INDEX -d -`" = "" ]; do
      INDEX=`expr $INDEX + 1`
    done
    INDEX=`expr $INDEX - 1` # don't include the null value
    # If we don't have four segments, return the old-style (or out of spec) package name:
    if [ "$INDEX" = "2" -o "$INDEX" = "3" ]; then
      echo $STRING
    else # we have four or more segments, so we'll consider this a new-style name:
      NAME=`expr $INDEX - 3`
      NAME="`echo $STRING | cut -f 1-$NAME -d -`"
      # cruft for later ;)
      VER=`expr $INDEX - 2`
      VER="`echo $STRING | cut -f $VER -d -`"
      ARCH=`expr $INDEX - 1`
      ARCH="`echo $STRING | cut -f $ARCH -d -`"
      BUILD="`echo $STRING | cut -f $INDEX -d -`"
#	  echo $NAME-$VER-$ARCH-$BUILD
    fi
}

show_dialog_success_install ()
{
	$DIALOG --backtitle "The Dropline Installer" \
			--title "Installation Complete" \
			--msgbox "Congratulations!  Your computer is now running the \
Dropline GNOME Desktop.\n\nIf you use the X Window System, 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:\".\n\nIf you have any \
questions or comments, please feel free to visit the Dropline Forums at \
http://www.dropline.net/forums.\n\nThanks for using Dropline GNOME!" 17 66
}

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

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

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

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

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

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

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

	# Remove the older -dropline- packages first	
	current=`pwd`
	cd /var/log/packages
	percent=0
	install_count=0
	install_index=1
	for file in *-dropline-*
	do
		let "install_count += 1"
	done
	
	(
	for file in *-dropline-*
	do
		# check for ctl-c
		if [ "$?" == "130" ]; then
			break;
		fi
		echo "XXX"
		let "percent = install_index * 100 / install_count"
		echo $percent
		echo "Before installing, the Dropline Installer will remove obsolete Dropline GNOME packages.  \
This may take several minutes.\n\nRemoving $file..."
		echo "XXX"
		if [ "open-office-dropline-1.0.1-i386-1" != "$file" ]; then
			removepkg $file &> /dev/null
		fi
		let "install_index += 1"				
	done
	) |
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Removing Obsolete Packages" \
		--gauge "" 12 50
		
#		$DIALOG --backtitle "The Dropline Installer" \
#		--title "Removing Obsolete Packages" \
#		--infobox "Before installing, the Dropline Installer will remove obsolete Dropline GNOME packages.  \
#This may take several minutes.\n\nRemoving $file..." 7 50
#		removepkg $file &> /dev/null
	
	if [ "$?" == "130" ]; then
		reset
		exit 130
	fi
	
	cd $current
	
	cd $dir
	count=${#DroplineFiles[@]}
	index=0
	percent=0
	install_count=0
	install_index=1
	while [ "$index" -lt "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			let "install_count += 1"
		fi
		let "index += 1"
	done
	index=0
	cat /dev/null > $tempfile
	(
	while [ "$index" -le "$count" ]
	do
		# check for ctl-c
		if [ "$?" == "130" ]; then
			break;
		fi
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			echo "XXX"
			let "percent = install_index * 100 / install_count"
			echo $percent
			echo "Installing ${DroplineFiles[$index]}..."
			echo "XXX"
			if [ ! -e "${DroplineFiles[$index]}" ]; then
				echo "Could not open file: ${DroplineFiles[$index]}" > $tempfile
				break;
			fi
			upgradepkg --install-new --reinstall "${DroplineFiles[$index]}" >/dev/null 2>$tempfile
			if [ "`cat $tempfile`" != "" ]; then
				# FAM does this a lot, it's ok
				if [ "`grep \"Configuration changes to rpc\" $tempfile`" == "" ]; then
					break;
				fi
			fi
			let "install_index += 1"
		fi
		let "index += 1"
	done
	) |
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Installing Packages" \
		--gauge "" 8 50
	
	if [ "$?" == "130" ]; then
		reset
		exit 130
	fi
	
	if [ "`cat $tempfile`" != "" ]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Installation Error" \
			--msgbox "`cat $tempfile`" 12 65
	else		
		remove_gnome1_packages
		remove_obsolete_packages $dir
		clean_up
		show_dialog_success_install
	fi
	
	cd $CACHE
}

upgrade_files ()
{
	dir=`dirname "$1"`
	TYPE="$2"
	
	# 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
		let "install_count += 1"
	done
	
	(
	for file in *-dropline-*
	do
		# check for ctl-c
		if [ "$?" == "130" ]; then
			break;
		fi
		echo "XXX"
		let "percent = install_index * 100 / install_count"
		echo $percent
		echo "Before upgrading, the Dropline Installer will remove obsolete Dropline GNOME packages.  \
This may take several minutes.\n\nRemoving $file..."
		echo "XXX"
		if [ "open-office-dropline-1.0.1-i386-1" != "$file" ]; then
			removepkg $file &> /dev/null
		fi
		let "install_index += 1"				
	done
	) |
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Removing Obsolete Packages" \
		--gauge "" 12 50
	if [ "$?" == "130" ]; then
		reset
		exit 130
	fi
	
	cd $dir
	count=${#DroplineFiles[@]}
	index=0
	percent=0
	install_count=0
	install_index=1
	while [ "$index" -lt "$count" ]
	do
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			let "install_count += 1"
		fi
		let "index += 1"
	done
	index=0
	cat /dev/null > $tempfile
	(
	while [ "$index" -le "$count" ]
	do
		# check for ctl-c
		if [ "$?" == "130" ]; then
			break
		fi
		if [[ "${DroplineFiles[$index]}" != "installed" && "${DroplineFiles[$index]}" != "" ]]; then
			echo "XXX"
			let "percent = install_index * 100 / install_count"
			echo $percent
			echo "Upgrading to ${DroplineFiles[$index]}..."
			echo "XXX"
			if [ ! -e "${DroplineFiles[$index]}" ]; then
				echo "Could not open file: ${DroplineFiles[$index]}" > $tempfile
				break;
			fi
			
			upgradepkg --install-new --reinstall "${DroplineFiles[$index]}" >/dev/null 2>$tempfile
			if [ "`cat $tempfile`" != "" ]; then
				# FAM does this a lot, it's ok
				if [ "`grep \"Configuration changes to rpc\" $tempfile`" == "" ]; then
					break;
				fi
			fi
			let "install_index += 1"
		fi
		let "index += 1"
	done
	) |
	$DIALOG --backtitle "The Dropline Installer" \
		--title "Upgrading Packages" \
		--gauge "" 8 50 0
	
	if [ "$?" == "130" ]; then
		reset
		exit 130
	fi
	
	if [ "`cat $tempfile`" != "" ]; then
		$DIALOG --backtitle "The Dropline Installer" \
			--title "Installation Error" \
			--msgbox "`cat $tempfile`" 12 65
	else		
		remove_gnome1_packages
		remove_obsolete_packages $dir
		clean_up
		show_dialog_success_upgrade
	fi
	
	cd $CACHE
}

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

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

	retval=$?

	choice=`cat $tempfile`

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

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

	retval=$?

	choice=`cat $tempfile`

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

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

	retval=$?

	choice=`cat $tempfile`

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

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

	retval=$?

	choice=`cat $tempfile`

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

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

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

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

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

	retval=$?

	choice=`cat $tempfile`

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

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

MIRROR=`cat ~/.dropline/download_mirror`

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

main_menu
