#!PERL -w
# rpmdepsize - visualize the size of RPM dependencies
# (C) Copyright 2009 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Written by Richard W.M. Jones <rjones@redhat.com>

use strict;

use Getopt::Long;
use Pod::Usage;

my $man = 0;
my $help = 0;

GetOptions ('help|?' => \$help,
	    'man' => \$man)
    or pod2usage (2);
pod2usage (1) if $help || @ARGV == 0;
pod2usage (-exitstatus => 0, -verbose => 2) if $man;

# Recurse through dependencies until all deps have been found.
my %deps;

foreach (@ARGV) {
    $deps{$_} = []
}

my $stable = 0;
while (!$stable) {
    $stable = 1;
    foreach my $name (sort keys %deps) {
	if (@{$deps{$name}} == 0) {
	    $stable = 0;
	    add_deps ($name);
	}
    }
}

sub add_deps
{
    my $name = shift;

    print "resolving deps in $name ...\n";

# repoquery is incredibly slow.  Unfortunately python has a
# privileged position into the yum databases, and a python
# script to access this information runs quickly, so this
# is what the alternate implementation below uses.
#     my $cmd =
# 	"repoquery --recursive --resolve -R $name |
#          sort -u | awk -F- '{print \$1}'";

    my $cmd = "./repodeps $name | grep -v '^Loaded plugins:'";

    open RQ, "$cmd |" or die "$cmd: $!";
    my $n = 0;
    while (<RQ>) {
	chomp;
	push @{$deps{$name}}, $_;
	$n++;
	$deps{$_} = [] unless exists $deps{$_};
    }
    close RQ;
    push @{$deps{$name}}, $name if $n == 0;
}

__END__

=head1 NAME

 rpmdepsize - Visualize the size of RPM dependencies

=head1 SYNOPSIS

 rpmdepsize [--options] package [package ...]

=head1 OPTIONS

=over 4

=item B<--help>

Display short usage message and exit.

=item B<--man>

Display manual page and exit.

=back

=head1 DESCRIPTION





=head1 HOME PAGE

L<http://et.redhat.com/~rjones/rpmdepsize>

=head1 SEE ALSO

L<rpm(1)>, L<repoquery(1)>, L<dot(1)>.

=head1 AUTHORS

Richard W.M. Jones <rjones @ redhat . com>

=head1 COPYRIGHT

(C) Copyright 2009 Red Hat Inc.,
L<http://et.redhat.com/~rjones/febootstrap>.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

=cut
