#!/bin/sh -e

# a sane default
EMAIL=root

# set EMAIL
[ -e /etc/apticron/apticron.conf ] && . /etc/apticron/apticron.conf

# get rid of any archives
apt-get -qq clean

# update the package lists
apt-get -qq update

# get the list of URI's we intend to download
URIS=`apt-get --print-uris -qq dist-upgrade | cut -d\' -f2`


if [ -n "$URIS" ] ; then
	
	# do the upgrade downloads
	apt-get -qq -d dist-upgrade

  (
	echo "Apticron  has  detected  that  some  packages  need  upgrading  on  `hostname` "
	echo
	echo The following packages have been downloaded, and are available for installation :
	echo

	for pkg in $URIS ; do
		echo "  " $pkg
	done

	echo
	echo ======================================================================================
	echo
	echo Package Details:
	echo 

	apt-listchanges -f text /var/cache/apt/archives/*.{rpm,deb} 2> /dev/null

	echo
	echo ======================================================================================
	echo
	echo You can perform the upgrade by issuing the command:
	echo
	echo "  apt-get dist-upgrade"
	echo
	echo as root on `hostname`
	echo
	echo --
	echo Apticron on `hostname`

   ) | mailx -s "New Package Updates on `hostname`" $EMAIL
	
fi
