#! /usr/bin/env perl

=head1 pkgfind

This tool finds l4re packages in the provided source dir by looking for a
`Control` file and sorting out those that are marked 'obsolete' or 'broken'
by a respective file.

Args:


=over

=item 1: source dir to locate packages in

=item 2: max search depth

=back

=cut

use strict;
use File::Spec::Functions;

my ($p, $max) = @ARGV;

my %hits;

sub find_dir
{
  my $m = shift;
  my $dx = catdir(@_);
  my $d = catdir($p, @_);
  if (-f "$d/Control" and ! -f "$d/obsolete" and ! -f "$d/broken") {
    $hits{$dx} = 1;
    return;
  }
  return if $m <= 1;

  opendir (my $dh, $d) || "can't open $d: $!";
  foreach my $sd (readdir($dh)) {
    next if $sd eq '.' or $sd eq '..';
    next if $sd eq '.git' or $sd eq '.svn';
    my $fd = catdir($d, $sd);
    next unless -d $fd;
    find_dir($m - 1, @_, $sd);
  }
  closedir $dh;
}

find_dir($max);

print join("\n", sort(keys(%hits))). "\n";
