#! /usr/bin/perl -w
use diagnostics;
use strict;

my $verbose = 0;

while (@ARGV) {
	my $arg = shift;
	if ($arg eq '-v' or $arg eq '--verbose') {
		$verbose = 1;
	} else {
		warn "Unrecognized option: $arg\n";
	}
}

local (*AVAILABLE, *STATUS);
open AVAILABLE, '</var/lib/dpkg/available' or
	die "Can't open available file: $!";
open STATUS, '</var/lib/dpkg/status' or die "Can't open status file: $!";

my (%available, %status);
my $package;

while (<AVAILABLE>)
{
	if (/^Package: (.*)/)
	{
		$package = $1;
	}
	elsif (/^Version: (.*)/)
	{
		$available{$package} = $1;
		undef $package;
	}
}

while (<STATUS>)
{
	if (/^Package: (.*)/)
	{
		$package = $1;
	}
	elsif (/^Version: (.*)/)
	{
		$status{$package} = $1;
		undef $package;
	}
}

close STATUS;
close AVAILABLE;

foreach my $package (sort keys %available)
{
	my ($av, $st) = ($available{$package}, $status{$package});
	if (defined $st)
	{
		if ($av ne $st) {
			print "!!! $package $av $st\n";
		} elsif ($verbose) {
			print "$package $av $st\n";
		}
	}
}

