#!/bin/sh

# apt-remove: interactive apt-get remove tool
#
# Dobrica Pavlinusic <dpavlin@rot13.org>
#
# based on well-known apt-iselect available at:
# http://www.rot13.org/~dpavlin/apt-iselect-remove.html
#
# 2006-01-04 first version
#
# It will automatically use sudo if installed or require user to enter root
# password when needed. 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
	use_sudo=1
else
	use_sudo=0
fi

do_sudo() {
	msg=$1 ; shift
	if [ "$use_sudo" = 1 ] ; then
		sudo -p "$msg, please enter your password: " $*
	else
		if [ "`id -u`" != 0 ] ; then
			echo "$msg, please enter root password..."
		fi
		su -c "$*"
	fi
}

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

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

res=`mktemp /tmp/tmp-res.XXXXXX` || ( echo "Can't create temp file!" ; exit 1 )
res2=`mktemp /tmp/tmp-res2.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )
sel=`mktemp /tmp/tmp-sel.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )
pkg=`mktemp /tmp/tmp-pkg.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )

rmtemp() {
	rm -f $res
	rm -f $res2
	test -f $sel && rm -f $sel
	rm -f $pkg
}

trap 'rmtemp; exit 1' INT QUIT TERM SEGV

dpkg_l_search() {
	search_words="$*"

	echo "Searching dpkg -l for \"$search_words\"..."

	COLUMNS=500 dpkg -l "$@" | grep ^ii | awk '{print $2}' | 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 "$nr results for \"$search_words\", enter new search {s:search=%[Search for]S}" > $res2
		echo >> $res2
		cat $res | sed 's/^/{s}/' >> $res2
	fi
	if [ $nr -gt 0 ] ; then
		echo >> $res2
		echo "{s:_quick_install_}Remove all selected" >> $res2
	fi
	echo >> $res2
	echo "Enter new dpkg -l search {s:search=%[Search for]S}" >> $res2
}

dpkg_l_search "$@"

pkg_nr=3
loop=1

while [ $loop = 1 ] ; do

	loop=0;
	iselect -m -d '{,}' -P -p $pkg_nr -n "apt-iselect: $nr results for \"$search_words\"" < $res2 > $sel

	if [ ! -s $sel ] ; then
		exit 0;
	elif tmp=`grep search= <$sel 2>/dev/null` ; then
		dpkg_l_search `echo $tmp | grep search= | cut -d= -f2`
		loop=1
	elif tmp=`grep -i ':_quick_install_' $sel` ; then
		debs=`grep -v ':_quick_install_' $sel | cut -d: -f2 | cut -d" " -f1 | xargs echo`
		do_sudo "Removing '$debs'" apt-get remove $debs
	else
		# not search, find packages info

		echo '{s}Back to search results' > $pkg
		echo >> $pkg
		hl_regex=`echo $search_words | sed -e 's/ /|/' -e 's,^,s#(,' -e 's,$,)#{b}\\\1{/b}#g,'`

		cat "$sel" | while read tmp ; do

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

			apt-cache show "$pkg_name" | cut -c-128 | sed 's/^\(Package: \)/{s}\1/' | sed -r "$hl_regex" | sed '1,3 s,{/*b},,g' >> $pkg

		done
		echo '{s}Back to search results' >> $pkg

		tmp=`iselect -d '{,}' -n "Packages: $pkg_list" -m < $pkg`
		if echo $tmp | grep -i back >/dev/null ; then
			loop=1
		elif echo $tmp | grep '^Package: ' >/dev/null ; then
			deb=`echo $tmp | sed -e 's,{b/*},,g' -e 's,Package: *,,g'`
			do_sudo "Removing '$deb'" apt-get remove $deb
		elif [ -z "$tmp" ] ; then
			loop=1
		else
			echo -n "Can't parse output '$tmp' from iselect! Press enter to continue..."
			read tmp
			loop=1
		fi
	fi
done

rmtemp
