#!/usr/bin/perl
#
# rcbugs - Find packages installed on a Debian system affected by RC bugs
# 
# (c) Emanuele Rocca <ema@debian.org> 2003
# Distributed under the GNU GPL License (see http://www.gnu.org/gpl)

use LWP::Simple;

use strict;

my $debug = 1;
my $btsurl = "http://bugs.debian.org/release-critical/debian/all.html";
my $statusfile = "/var/lib/dpkg/status";
my @buglines = split("\n",get($btsurl))
	or die("\nImpossible to get the bts page");
my @bugs;
my $i = 0;
my $ibugs = 0;

foreach (@buglines) {
  if ($buglines[$i]=~/<a name=\".*\"><strong>Package:<\/strong>/) {
      if ( $buglines[$i]=~/"(.*?)"/ ) {
      	$bugs[$ibugs]= $1;
      }
      $ibugs++;
  }
  $i++;
}

my @packagelines;

if (open(FILE, "<$statusfile")) {
        @packagelines = <FILE>;
	close(FILE);
}

$i = 0;
my $package;
my $packages = 0;
my $systembugs = 0;

foreach (@packagelines) {
	if ($packagelines[$i]=~/Status: install ok installed/) {
		$packages++;
		if ($packagelines[$i-1]=~/: (.*)/) {
			$package = $1;
			for (my $k=0; $k < $ibugs; $k++) {
				if ($package eq $bugs[$k]) {
					print "$package\n";
					$systembugs++;
				}
			}
		}
	}
	$i++;
}

print "\n\nInstalled packages: $packages" if ($debug);
print "\nRealase-critical bugs currently in Debian unstable: $ibugs" if $debug;

if ($systembugs eq 0) {
	print "\nYour system is rc-bug free!";
}
else {
	print "\nInstalled packages affected by rc-bugs: $systembugs" if ($debug);
}
