#!/usr/bin/perl
#
# upgrade_check
# 
# This program runs on any system that uses
# apt-get (Debian, for example) for package
# management.  If run out of cron, it will
# update the APT database and see if there
# are any upgrades that should be performed.
#
# It sends the resulting package names to root
# via email.  (Make sure root's email goes to
# a human, of course.)
#
# Copyright 2002, Bri Hatch
#
# Released under the GPL.  See COPYING file
# for more information.

use Sys::Hostname;
use strict;

# run apt-get update first
system "/usr/bin/apt-get update >/dev/null 2>&1";

# now run apt-get upgrade
open UPGRADE, "/usr/bin/apt-get --simulate upgrade |";

my @output = <UPGRADE>;

# Yes, this is a sad way to check, but it
# seems to work for all versions of apt-get.
# I wish there were a way to get a 'upgrade
# needed' exit code to check for instead of
# hard coding this pattern.
#
my $NO_UPGRADES_NEEDED =
    '^0 packages upgraded, 0 newly installed, 0 to remove and 0 +not upgraded.';

unless ( grep /$NO_UPGRADES_NEEDED/, @output ) {
	print "Host ", hostname(), " requires the following package upgrades:\n\n";

	# strip uninteresting output
	@output = grep ! /^Reading Package|^Building Depe/, @output;

	print @output, "\n\n";
	print "Log in and run 'apt-get upgrade' at your earliest convienience.\n";
}
