#!/bin/sh
# coding: utf-8
#
## UPGRADE-SYSTEM -- Debian system upgrading and sanitizing CLI tool.
#
## HOMEPAGE
#  http://q-funk.iki.fi
#
## COPYRIGHT
#  (C) 2003-2004, Martin Zdrahal <martin.zdrahal@konflux.at>
#  (C) 2004, Christoph Schindler <hop@30hopsmax.at>
#  (C) 2004-2010, Martin-Éric Racine <q-funk@iki.fi>
#
## LICENSE
#  This package 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.
#
## DEPENDS
# apt (>= 0.6.45) reason: autoclean
# deborphan (>= 1.7) reason: --guess-doc --libdevel
#
## CHANGES
#  2009-08-08	Add obsolete config purge.	v1.2 [MER]
#  2009-06-21   Add uninstalled packages purge.	v1.1 [MER]
#  2005-12-04   Use APT instead of DPKG purge.	v1.0 [MER]
#  2005-05-29   Add non-interactive detection.	v0.9 [MER]
#  2004-09-04   Make orphan purge recursive.	v0.8 [CS]
#  2004-08-19   Add APT exit code check.	v0.7 [MER]
#  2004-06-07   Add CLEANOPTS to config.	v0.6 [MER]
#  2004-03-31   Create config file.		v0.5 [MER]
#  2004-03-24   Add -y to dist-upgrade.		v0.4 [MER]
#  2004-03-15   Add --guess-doc --libdevel.	v0.3 [MER]
#  2004-03-09   Rename upgrade-system.		v0.2 [MER]
#  2004-02-16   Initial release.		v0.1 [MZ]
###
LANGUAGE=C
export LANGUAGE
TEXTDOMAIN=upgrade-system
export TEXTDOMAIN
####################
### SHOW VERSION ###
####################
case $1 in
	"")
		break
		;;
	*)
		echo "upgrade-system $( dpkg -l | grep upgrade-system | awk '{print $3}' )"
		exit
		;;
esac

##########################
### SOURCE PREFERENCES ###
##########################
. /etc/upgrade-system.conf

############################
### SET DEBCONF FRONTEND ###
############################
tty -s
if [ $? != 0 ]
then
	echo "N: Non-Interactive mode selected."
	NOTTY="-q -y -o DPkg::Options::=--force-confdef"
	DEBIAN_FRONTEND="noninteractive"
	export DEBIAN_FRONTEND
	#DEBCONF_ADMIN_EMAIL=""
	#export DEBCONF_ADMIN_EMAIL
fi

############################
### UPDATE PACKAGE LISTS ###
############################
echo "1) Updating package lists."
apt-get -q=2 update
if [ $? != 0 ]
then
	echo "E: Some package lists could not be updated."
	exit 1
fi

########################
### UPGRADE PACKAGES ###
########################
echo "2) Upgrading packages:"
apt-get $NOTTY -u $UPGRADEOPTS
if [ $? != 0 ]
then
	echo "E: Some packages could not be upgraded."
	exit 2
fi

#######################
### CLEAN APT CACHE ###
#######################
echo "3) Cleaning APT cache."
apt-get $CLEANOPTS

#############################
### PURGE ORPHAN PACKAGES ###
#############################
echo "4) Checking for orphan packages:"
while true
do
	ORPHANS=$( deborphan $ORPHANOPTS )
	case $ORPHANS in
		"")
			echo "I: No orphan package to purge."
			break
			;;
		*)
			echo "I: Purging orphan packages..."
			apt-get $NOTTY --purge autoremove $ORPHANS
			# escape dangerous purges automatically
			if [ $? != 0 ]
			then
			        break
			fi
			;;
	esac
done

####################################################################
### FLAUSCH'S SUPER CRUFT LIQUIDATOR -- USE WITH EXTREME CAUTION ###
####################################################################
# Check whether the FLAUSCH option is enabled in upgrade-system.conf
case $FLAUSCH in
	"")
		break
		;;
	*)
		echo "W: FLAUSCH OPTION ENABLED; USE WITH EXTREME CAUTION!"
		##################################
		### PURGE UNINSTALLED PACKAGES ###
		##################################
		echo "5) Checking for uninstalled packages:"
		while true
		do
			ORPHANS=$( dpkg -l | grep '^rc' | awk '{print $2}' )
			case $ORPHANS in
				"")
					echo "I: No uninstalled package to purge."
					break
					;;
				*)
					echo "I: Purging uninstalled packages..."
					dpkg -P $ORPHANS
					# escape dangerous purges automatically
					if [ $? != 0 ]
					then
			        		break
					fi
					;;
			esac
		done
		############################
		### PURGE ORPHAN CONFIGS ###
		############################
		echo "6) Checking for orphan configurations:"
		while true
		do
			ORPHANS=$( deborphan --find-config )
			case $ORPHANS in
				"")
					echo "I: No orphan configuration to purge."
					break
					;;
				*)
					echo "I: Purging orphan configurations..."
					dpkg -P $ORPHANS
					# escape dangerous purges automatically
					if [ $? != 0 ]
					then
					        break
					fi
					;;
			esac
		done
		#################################
		### REMOVING OBSOLETE CONFIGS ###
		#################################
		echo "7) Checking for obsolete configurations:"
		ORPHANS=$( dpkg-query -W -f='${Conffiles}\n' | grep obsolete | awk {'print $1'} )
		case $ORPHANS in
			"")
				echo "I: No obsolete configuration found."
				break
				;;
			*)
				dpkg -S $ORPHANS
				NUMBER=$( dpkg -S $ORPHANS | wc -l )
				echo "N: Number of packages affected: $NUMBER"
				echo "I: Removing obsolete configurations..."
				echo "W: BEWARE OF FALSE POSITIVES! DELETE WITH EXTREME CAUTION!"
				rm -i $ORPHANS
				apt-get $NOTTY --reinstall install $( dpkg -S $ORPHANS | cut -d ':' -f 1 )
				# escape dangerous purges automatically
				if [ $? != 0 ]
				then
				        break
				fi
				;;
		esac
		;;
esac

############
### DONE ###
############
echo "N: System upgrade completed."

#EOF
