#!/usr/bin/perl
#
# debdeps.pl - finds reverse dependencies for all packages.
# jason plumb - http://noisybox.net
#
# It's slow and inefficient -- but should work.
# You'll need perl (obviously) and grep and sed.
#
# This is probably only really useful for Debian.
# No ations are taken -- just a list is obtained.
#
# One of the dpkg commands complains if you're not root, 
# so expect problems running this as a mortal.
#

$ENV{'COLUMNS'} = 256;
@lines = `dpkg-query -l`;
my %inf;
foreach (@lines){
	chomp;
	next if $_ =~ /(^\|)|(^Desired)|(^\+\+\+)/;	#header crap

	$_ =~ s/^..\s+//;
	$_ =~ s/\s\s+/\t/g;
	($name, $ver, $desc) = split /\t/, $_;

	$inf{ $name }->{'version'} = $ver;
	$inf{ $name }->{'desc'} = $desc;

	print "Checking reverse deps for $name...";
	@rdeps = `dpkg --purge --no-act $name 2>&1 | grep "depends on" | sed -e "s/ depends on .*//"`;
	print $#rdeps+1 . "\n";
	push @nordeps, $name if ($#rdeps == -1);

}

print "----------------------------------------------------\n";
print "The following packages have no reverse dependencies:\n";
foreach $name (@nordeps){
	print $name . "\n";
}
