#!/bin/sh

# apt-iselect: interactive apt-cache search tool
#
# Dobrica Pavlinusic <dpavlin@rot13.org>
# http://www.rot13.org/~dpavlin/apt-iselect.html
#
# 2003-08-02 first version
# 2003-08-03 using mktemp to create temp files, auto-install iselect
# 2003-08-04 catch signals so temp files are not left behind, added
#		enter new search at end of results, autoconfig
# 2003-08-06 changed delimiter to \s/ to prevent conflict with e-mails
# 2003-08-15 automatically run apt-get update if there is no package list
#		(e.g. changeing apt/sources.list without update), cut lines
#		to 80 chars before sending to iselect (bug workaround)
#
# I know it's ugly, but it's still faster than aptitude :-)
#
# It will automatically use sudo if installed or require user to be root
# because apt-cache needs this. It will also install iselect deb package
# if it's not already there.
#
# WARNING: due to iselect limitation, maximum number of results is
# 1020 and maximum length of all results is 1048576 bytes

if [ ! -z "`which sudo`" ] ; then
	id=`sudo -p "Using sudo, please enter your password: " id -u`
	sudo="sudo"
else
	id=`id -u`
	sudo=""
fi

if [ "$id" != 0 ] ; then
	echo "You really need to be root to use apt-cache and thus, apt-iselect !"
	echo "One way to do this is to install sudo which will be automatically used..."
	exit 1
fi

if [ -z "`which iselect`" ] ; then
	echo "You really need iselect for apt-iselect, installing..."
	$sudo apt-get install iselect
fi

if [ -z "$*" ] ; then
	echo "Usage: $0 [search pattern for apt-cache search]"
	exit 1
fi

res=`mktemp` || ( echo "Can't create temp file!" ; exit 1 )
res2=`mktemp` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )
pkg=`mktemp` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )

function rmtemp() {
	rm -f $res
	rm -f $res2
	rm -f $pkg
}

# need to update apt cache?
sudo apt-cache stats 2>&1 | grep "^W: Couldn't stat source package list" >/dev/null && sudo apt-get update

trap 'rmtemp; exit 1' INT QUIT TERM SEGV

function apt_cache_search() {
	search_words=$*

	echo "Searching apt-cache for \"$search_words\"..."

	apt-cache search "$*" | head -1020 > $res
	nr=`wc -l $res | sed 's/^ *//' | cut -d" " -f1`
	if [ $nr = 0 ] ; then
		echo "No results for \"$search_words\"" > $res2
		nr="no"
	else
		echo -e "$nr results for \"$search_words\", enter new search \\s:search=%[Search for]S/\n" > $res2
		cat $res | sed 's/^/\\s\//' >> $res2
	fi
	echo -e "\nEnter new apt-cache search \\s:search=%[Search for]S/" >> $res2
}

apt_cache_search $*

pkg_nr=3
loop=1

while [ $loop = 1 ] ; do

	loop=0;
	tmp=`iselect -d '\,/' -P -p $pkg_nr -n "apt-iselect: $nr results for \"$search_words\"" < $res2`

	if echo $tmp | grep search= >/dev/null ; then
		apt_cache_search `echo $tmp | cut -d= -f2`
		loop=1
	else
		# not search, find package info

		pkg_nr=`echo $tmp | cut -d: -f1`
		pkg_full=`echo $tmp | cut -d: -f2`
		pkg_name=`echo $pkg_full | cut -d" " -f1`

		if [ ! -z "$pkg_name" ] ; then
			echo -e '\s/Back to search results\n' > $pkg
			apt-cache show $pkg_name | sed 's/^\(Package: \)/\\s\/\1/' | cut -c-80 >> $pkg
			echo '\s/Back to search results' >> $pkg

			tmp=`iselect -d '\,/' -n "Package: $pkg_full" < $pkg`
			if echo $tmp | grep -i back >/dev/null ; then
				loop=1
			elif echo $tmp | grep '^Package: ' >/dev/null ; then
				deb=`echo $tmp | cut -d: -f2`
				$sudo apt-get install $deb
			else
				echo -n "Can't parse output '$tmp' from iselect! Press enter to continue..."
				read tmp
				loop=1
			fi
		fi
	fi
done

rmtemp
