package Cross::Inspector::Author;

use Moose;
extends 'Cross::Inspector';

use warnings;
use strict;

use Data::Dumper;
use File::Slurp qw/slurp/;
use YAML::Syck;
use Pod::POM;

=head1 NAME

Cross::Inspector::Author - The great new Cross::Inspector::Author

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

Quick summary of what the module does.

Perhaps a little code snippet.

    use Cross::Inspector;

    my $foo = Cross::Inspector->new();
    ...

=head1 EXPORT

A list of functions that can be exported.  You can delete this section
if you don't export anything, such as for a purely object-oriented module.

=head1 SUBROUTINES/METHODS

=head2 function1

=cut

sub process {
	my $self = shift;

	return '' unless -f $self->path;
	print "Inspecting: ",$self->path,"\n";

	my $data;

	if ($self->path =~ m/meta\.ya?ml/i) {
		my $tmp = LoadFile($self->path);
		if ($tmp->{'author'}) {
			#print Dumper $tmp->{'author'};
			foreach (@{$tmp->{'author'}}) {
				if ($_ =~ m/(.*?)\s+<(.*?)>/) {
					my ($name,$end) = ($1,$2);
					push @$data, {name=>$name, email=>$end};
				}
			}
		}
	}

	if ($self->path =~ m/(Makefile\.PL|Build\.PL)/i) {
		my $content = slurp($self->path);
		if ($content =~ m/AUTHOR\s+=>\s+('|")(.*?)('|")/) {
			my $found = $2;
			if ($found =~ m/(.*?)\s+<(.*?)>/) {
				my ($name,$end) = ($1,$2);
				push @$data, {name=>$name, email=>$end};
			}
		}
	}

	if ($self->path =~ m/\.(pm|pod)$/) {
   	# get description and synopsis
   	my $parser = Pod::POM->new;
   	my $pom = $parser->parse_file($self->path);

   	foreach my $head1 ($pom->head1()) {
      	if ($head1->title() =~ m/authors?/i) {
				my $text = $head1->content->present;
				$text =~ s/^[\n\s]*//g;
				$text =~ s/[\n\s]*$//g;
				while ($text =~ m/(\w.*?),?\s+(C<)?<?(\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\b)>?>?/gi) {
					my ($name,$end) = ($1,$3);
					$name =~ s/^\s*//;
					$name =~ s/\s*$//;
					#$data->{'authors'} = {name=>$name, email=>$end};
					push @$data, {name=>$name, email=>$end};
				}
				unless ($data) {
					my @tmp = split /,?\s*\n/, $text;
					foreach (@tmp) {
						$_ and push @$data, {name=>$_};
					}
				}
      	}
   	}
	}

	my @tmp;
	foreach (@$data) {
		push @tmp, $_->{name}.' <'.$_->{email}.'>';
	}
	my $value = join ', ', @tmp;

	{'author'=>{'value'=>$value, 'attrs'=>$data}};
}

sub join {
	my ($self, @v) = @_;

	my $new;
	foreach my $i (@v) {
		foreach my $j (@{$i->{attrs}}) {
			push @$new, $j unless (grep {$j->{email} eq $_} @{[map {$_->{email}} @$new]});
		}
	}

	my @tmp;
	foreach (@$new) {
		push @tmp, $_->{name}.' <'.$_->{email}.'>';
	}
	my $value = join ', ', @tmp;

	{'value'=>$value, 'attrs'=>$new};
}

=head1 AUTHOR

Nuno Carvalho, C<< <smash at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-cross-inspector at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Cross-Inspector>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Cross::Inspector


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Cross-Inspector>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Cross-Inspector>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Cross-Inspector>

=item * Search CPAN

L<http://search.cpan.org/dist/Cross-Inspector/>

=back


=head1 ACKNOWLEDGEMENTS


=head1 LICENSE AND COPYRIGHT

Copyright 2010 Nuno Carvalho.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


=cut

1; # End of Cross::Inspector
