#!/bin/sh
#
## UPDATE-SYSTEM  -  simple Debian system updater.
#  
## HOMEPAGE
#  http://funkyware.konflux.at/
#
## AUTHORS 
#  Christoph Schindler <hop@30hopsmax.at> (2004)
#  Martin-Eric Racine <q-funk@iki.fi> (2004,2005)
#  Martin Zdrahal <martin.zdrahal@konflux.at> (2003)
#
## CHANGES
#  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]
#
. /etc/upgrade-system.conf
tty -s
if [ $? != 0 ]
then
	echo "N: Non-Interactive upgrade selected."
	NOTTY="-q -y -o DPkg::Options::=--force-confdef"
	DEBIAN_FRONTEND="noninteractive"
	export DEBIAN_FRONTEND 
	#DEBCONF_ADMIN_EMAIL=""
	#export DEBCONF_ADMIN_EMAIL
fi
echo "1) Updating available package lists."
apt-get -q=2 update
if [ $? != 0 ]
then
	echo "E: Some package lists could not be updated."
	exit 1
fi
echo "2) Upgrading installed packages:"
apt-get $NOTTY -u $UPGRADEOPTS
if [ $? != 0 ]
then
	echo "E: Some packages could not be upgraded."
	exit 2
fi
echo "3) Cleaning APT cache."
apt-get $CLEANOPTS
echo "4) Checking for orphan packages:"
while /bin/true
do
	# save the orphan list for later use
	ORPHANS=$( deborphan $ORPHANOPTS )
	case $ORPHANS in
		"")
			echo "I: No orphan package to be purged."
			break
			;;
		*)
			echo "I: Purging orphan packages..."
			apt-get -y --purge remove $ORPHANS
			;;
	esac
done
case $FLAUSCH in
	"")
		break
		;;
	*)
		echo "[] Checking for orphan configurations:" 
		while /bin/true
		do
			ORPHANS=$( deborphan --find-config )
			case $ORPHANS in
				"")
					echo "I: No orphan configuration to be purged."
					break
					;;
				*)
					echo "I: Purging orphan configurations..."
					dpkg -P $ORPHANS
					;;
			esac
		done
		;;
esac
if [ -f /usr/bin/update-menus ]
then
	echo "5) Refreshing menus."
	update-menus
fi
echo "N: System upgrade completed."
#EOF
