#!/bin/bash

# Author: Travis B. Hartwell <nafai@travishartwell.net>
# 		  http://www.travishartwell.net/blog/

# TODO:
# - show dialog indicating searching
# - filter out those packages that are already installed
# - offer to show list of those packages

# Returns a temporary filename and adds to the the list of files to
# be deleted
get_temp_file()
{
	filename="$1"
	temp_file=$(mktemp -p /tmp -t $filename.XXXXXX) || exit 1
	
	next_index=${#temp_files[@]}
	temp_files[$next_index]=$temp_file
	
	retval="$temp_file"
}

# Removes all of the registered temp files, if $DEBUG is not set
cleanup()
{
	if [ -z "$DEBUG" ]; then
		for file in ${temp_files[@]}; do
			rm -f "$file"
		done 
	fi
}

# Returns in $reval the terms inputed
get_search_terms()
{
	search_terms="$(zenity --entry \
			--title='Apt Search' \
			--text='Enter your search terms:')"
	
	retval="$search_terms"
}

# Returns in $retval the name of the temporary file containing the results
# Parameters passed in are the search terms
get_search_results()
{
	get_temp_file search_results; temp_file="$retval"
	
	apt-cache search "$@" > $temp_file
	
	retval="$temp_file"
}

# Formats the search results in file $1 for passing to zenity
format_results()
{
	search_results_file="$1"
	
	get_temp_file format_results; temp_file="$retval"

	# Prepend each line with FALSE (default to un-checked)
	# Put quotes around names and descriptions
	# Make the field separator a single space
	cat $search_results_file | while read line; do
		name=$(echo -n $line | cut -d" " -f1)
		description=$(echo -n $line | cut -d" " -f3-)
		echo "FALSE" >> $temp_file
		echo "$name" >> $temp_file
		echo "$description" >> $temp_file
	done

	retval="$temp_file"
}

# Gives the user a list box where they can select which packages to install
# $1 is the list containing the formated output of the search results
get_packages_to_install()
{
	format_results_file="$1"; shift
	title="Results from search on: $*"

	packages=$(cat $format_results_file | zenity --list \
			--text="Choose packages to install" \
			--title="$title" --height=500 --width=875 \
			--checklist \
			--column="Install" --column="Name" --column="Description")
		
	retval="$packages"			
}


generate_installer_file()
{
	packages="$*"
	
	get_temp_file installer_file; temp_file="$retval"

	old_IFS=$IFS; IFS="|"
		
	for package in $packages_to_install; do
		echo -e "$package\tinstall" >> $temp_file
	done
			
	IFS=$old_IFS
	
	retval="$temp_file"
}

install_packages()
{
	packages="$*"
	
	generate_installer_file $packages; installer_file="$retval"

	GKSU_MSG="<b><big>Administration rights are required to install and remove applications</big></b>

Enter your password to grant administration rights."
	
	# Run synaptic under gksu
	# If you have sudo set up, and you can sudo to root, and want to use sudo
	# make sure the gconf key is set:
	# 	$ gconftool-2 --get --ignore-schema-defaults /apps/gksu/sudo-mode
	# If it isn't, set it like this:
	# 	$ gconftool-2 --set /apps/gksu/sudo-mode --type boolean true
	# If you prefer to use su, and it isn't working, check to see if that 
	# gconf key is set, and set it to false or unset it like this:
	#	$ gconftool-2 --unset /apps/gksu/sudo-mode
	# Source (https://launchpad.net/distros/ubuntu/+source/gksu/+bug/28866)
	/usr/bin/gksu -m "$GKSU_MSG" "/usr/sbin/synaptic --hide-main-window --non-interactive --set-selections-file $installer_file"
}

declare -a temp_files
		
if [ -z "$*" ]; then
	get_search_terms; terms="$retval"
else
	terms="$*"
fi

if [ "$terms" ]; then
	get_search_results "$terms"; search_results_file="$retval"
	
	# If there were results
	if [ -s "$search_results_file" ]; then
		format_results "$search_results_file"; format_results_file="$retval"
			
		get_packages_to_install "$format_results_file" "$terms"; packages_to_install="$retval"
				
		if [ "$packages_to_install" ]; then
			install_packages "$packages_to_install"
		fi
	fi
fi
		
cleanup
		
exit 0
