#!/usr/bin/env perl

use warnings;
use strict;
use File::Find;
use Getopt::Attribute;


our $VERSION = '0.03';


our $prefix : Getopt(prefix=s);
our $suffix : Getopt(suffix=s);
our $join : Getopt(join=s);

# only print projroots, then exit -- no distfind
our $print_roots : Getopt(printroots);

# only print path to one or more given distributions
our @print_path : Getopt(printpath=s);
my %print_path = map { $_ => 1 } @print_path;  # lookup hash

my $projroot = shift || $ENV{PROJROOT};
my @projroot = map { s/^~/$ENV{HOME}/; $_ } split /\s*;\s*/ => $projroot;

if ($print_roots) {
    print join ' ' => @projroot;
    exit;
}

my @distro;

find (sub {
    return unless -d;

    # prune some things first for efficiency reasons - otherwise find() gets
    # quite slow.

    if ($_ eq '.svn' || $_ eq 'blib') {
        $File::Find::prune = 1;
        return;
    }

    if ($_ eq 'projctrl' || -e "$_/Build.PL" || -e "$_/Makefile.PL") {

        # only remember the distro if there was no print_path restriction, or
        # if it is within the print_path specs

        if (@print_path == 0 || exists $print_path{$_}) {
            push @distro => $File::Find::name;
        }

        # but prune anyway - we assume there are no distributions below a
        # directory that contains a Build.PL or a Makefile.PL.

        $File::Find::prune = 1;
    }
}, @projroot);

$join = ' ' unless defined $join;
$prefix = '' unless defined $prefix;
$suffix = '' unless defined $suffix;

print join $join => map { "$prefix$_$suffix" } @distro;


__END__

=head1 NAME

Dist::Joseki - tools for the prolific module author

=head1 SYNOPSIS

None yet (see below).

=head1 DESCRIPTION

None yet. This is an early release; fully functional, but undocumented. The
next release will have more documentation.

=head1 TAGS

If you talk about this module in blogs, on del.icio.us or anywhere else,
please use the C<distjoseki> tag.

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to
C<bug-dist-joseki@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.

=head1 INSTALLATION

See perlmodinstall for information and options on installing Perl modules.

=head1 AVAILABILITY

The latest version of this module is available from the Comprehensive Perl
Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a CPAN
site near you. Or see <http://www.perl.com/CPAN/authors/id/M/MA/MARCEL/>.

=head1 AUTHOR

Marcel GrE<uuml>nauer, C<< <marcel@cpan.org> >>

=head1 COPYRIGHT AND LICENSE

Copyright 2007 by Marcel GrE<uuml>nauer

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

