#!/bin/bash
########################################################################
####  Script Name: sm-lib-package-removal
####  version: 1.4.2
####  Date: February 10 2011

####  Copyright (C) Harald Hope 2005-2011
####  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 General Public License for more details.

####  Get the full text of the GPL here:
####  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

####  Script Author: Harald Hope
####  This is a library file for smxi and cannot be run independently

####  Script URL: http://smxi.org/sm/sm-lib-package-removal
####  Script SVN: http://code.google.com/p/smxi
####  Script Home page: http://techpatterns.com/forums/about736.html
########################################################################

########################################################################
####  FUNCTIONS
########################################################################

###---------------------------------------------------------------------
### package removal stuff
###---------------------------------------------------------------------

package_removal()
{
	eval $LOGME
	local repeat='' response='' completed='' opt='' options='' kickThem='' kickItems=''
	local options="bluetooth captive dmraid-mdadm-lvm german-components-de gnomemeetings isdn klaptopdaemon koffice libreoffice-all openoffice.org-all skype splashy squid vdr wifi wvdial xawtv remove-chosen back-to-main-menu"
	local removeCount=$( echo $options | awk '{print NF-1}' )

	print_lib_info $LIB_PACKAGE_REMOVAL
	echo $MLINE
	echo "${S}Please enter each item/group you want to remove completely."
	echo "Simply enter the number, hit enter, then enter the next number,"
	echo "for as many as you want. Once you are done, select ${C}remove-chosen${S}"
	echo "and the selected items will all be removed one by one. You will be asked to"
	echo "confirm each item's removal when it comes up, so you can change your mind."
	echo $LINE
	echo "${S}Note: german-components-de will look for and remove all -de type packages on the system."
	echo "Note: wifi can remove ALL wifi firmware, and associated wifi components."
	echo "${M}This is best for machines that will never use wifi, or for laptops where you"
	echo "want to remove unneeded wifi components. Please read the items carefully before answering ${C}y${M}."
	echo $LINE

	select opt in $options
	do
		case $opt in
			bluetooth|captive|dmraid-mdadm-lvm|german-components-de|gnomemeetings|isdn|klaptopdaemon|koffice|libreoffice-all|openoffice.org-all|pppoe|skype|splashy|squid|vdr|wifi|wvdial|xawtv)
				kickItems="$kickItems $opt"
				echo "${S}Selected items to remove:${C}$kickItems"
				echo "${S}Enter another item's number to remove more packages, or enter ${C}$removeCount${S} to remove items.${N}"
				kickThem='false'
				;;
			remove-chosen)
				completed='true'
				kickThem='true'
				;;
			back-to-main-menu)
				completed='true'
				kickThem='false'
				repeat='false'
				;;
			*)
				completed='true'
				repeat='true'
				print_error opt
				;;
		esac
		if [ "$completed" == 'true' ];then
			break
		fi
	done

	# this is just in case user hits remove-chosen without having entered an item
	if [ "$kickThem" == 'true' ];then
		if [ -n "$kickItems" ];then
			confirm_kick_items "$kickItems"
		else
			echo "${E}No items were selected! Let's try that again!${N}"
			package_removal
		fi
	fi
	eval $LOGME
	if [ "$repeat" == 'true' ];then
		package_removal
	fi
}
# args: $1 - kickItems
confirm_kick_items()
{
	eval $LOGUS

	local opt='' options='' kickItems="$1"

	echo $LINE
	echo "${S}These are your current selections. You will be asked one by one to"
	echo "remove the packages/groups that are installed on your system, and you will"
	echo "be alerted which are not installed as well as the remover runs."
	echo "${C}$kickItems"
	echo -e "${Q}Are you sure you want to completely remove these?"
	echo $LINE
	echo -e $SLE
	echo $LINE
	options="yes-remove-all no-start-over"

	select opt in $options
	do
		log_function_data "opt selected: $opt"
		case $opt in
			yes-remove-all)
				remove_kick_packages "$kickItems"
				;;
			no-start-over)
				echo "${S}Ok, let's start over again.${N}"
				package_removal
				;;
			*)
				print_error opt
				repeat='true'
				;;
		esac
		break
	done

	eval $LOGUE
	if [ "$repeat" == 'true' ];then
		confirm_kick_items "$kickItems"
	fi
}

# args: $1 - which group type to remove
remove_kick_packages()
{
	eval $LOGUS
	local kickIt='' kickItems="$1" packages='' packageCount='' plural=''

	for kickIt in $kickItems
	do
		# I don't know why these exceptions are here, leaving them for now
		# but I don't think they are necessary any longer
		case $kickIt in
			bluetooth)
				packages="$( package_tester '(bluetooth|bluez)' 'wild-full' )"
				;;
			captive)
				packages="$( package_tester '(captive|captive-lufs|captive-install|gnome-vfs-httpcaptive|ntfsprogs-gnomevfs)' )"
				;;
			dmraid-mdadm-lvm)
				packages="$( package_tester '(dmraid|mdadm|lvm|lvm2|lvm-common)' )"
				;;
			german-components-de)
				packages="$( package_tester '([.a-z0-9-]*-de(-[.a-z0-9-]*)*)' )"
				;;
			libreoffice-all)
				packages="$( package_tester '(libreoffice|python-uno)' 'wild-full' )"
				;;
			openoffice.org-all)
				packages="$( package_tester '(openoffice.org|python-uno)' 'wild-full' )"
				;;
			vdr)
				packages="$( package_tester '(*vdr[:space:]|vdr-|xvdr)' 'wild' ) $( package_tester 'vdr' )"
				;;
			wifi)
				#note: pulling bcm, too loose
				packages="$( package_tester '(b43|ipw[1-9]|madwifi|ndiswrapper|wifi|rt25|rt61|wlan-|at76|at76|zd12)' 'wild-full' )"
				;;
			*)
				packages=$( package_tester "$kickIt" 'wild-full' )
				;;
		esac
		# trim spaces from ends and null content
		packages=$( sed -r 's/(^[[:space:]]|[[:space:]]$)//g' <<< "$packages" )
		packageCount=$( wc -w <<< $packages )
		if [ "$packageCount" -gt 1 ];then
			plural='s'
		else
			plural=''
		fi
		echo $LINE
		if [ -n "$packages" ];then
			echo "${S}Removing ${C}$kickIt${S} package$plural now...${N}"
			package_remover "$packages" 'purge' 'group'
		else
			echo "${M}No ${C}$kickIt${M} package installed on your system...${N}"
			sleep 1.3
		fi
	done
	echo $LINE
	echo "${S}Finished removing components.${N}"
	print_hec
	eval $LOGUE
}

# package_removal

###**EOF**###


syntax highlighted by Code2HTML, v. 0.9.1