package Dist::Zilla::Plugin::DualLife;
BEGIN {
  $Dist::Zilla::Plugin::DualLife::AUTHORITY = 'cpan:FLORA';
}
{
  $Dist::Zilla::Plugin::DualLife::VERSION = '0.03';
}
# ABSTRACT: Distribute dual-life modules with Dist::Zilla

use Moose;
use List::AllUtils 'first';
use namespace::autoclean;

with 'Dist::Zilla::Role::InstallTool';


has entered_core => (
    is => 'ro',
    isa => 'Str',
    default => "5.009005",
);


has eumm_bundled => (
    is => 'ro',
    isa => 'Bool',
    default => "0",
);

sub setup_installer {
    my ($self) = @_;

    my $makefile = first { $_->name eq 'Makefile.PL' } @{ $self->zilla->files };
    $self->log_fatal('No Makefile.PL. It needs to be provided by another plugin')
        unless $makefile;

    my $content = $makefile->content;
    my $entered = $self->entered_core;

    my $dual_life_args = q[$WriteMakefileArgs{INSTALLDIRS} = 'perl'];

    if ( $self->eumm_bundled ) {
        $dual_life_args .= "\n    if \$] <= 5.011000;\n\n";
    }
    else {
        $dual_life_args .= "\n    if \$] >= $entered && \$] <= 5.011000;\n\n"
    }

    $content =~ s/(?=WriteMakefile\s*\()/$dual_life_args/
        or $self->log_fatal('Failed to insert INSTALLDIRS magic');

    $makefile->content($content);
}

__PACKAGE__->meta->make_immutable;

1;

__END__

=pod

=encoding utf-8

=head1 NAME

Dist::Zilla::Plugin::DualLife - Distribute dual-life modules with Dist::Zilla

=head1 SYNOPSIS

In your dist.ini:

  [DualLife]

=head1 DESCRIPTION

Dual-life modules, which are modules distributed both as part of the perl core
and on CPAN, sometimes need a little special treatment. This module tries
provide that for modules built with C<Dist::Zilla>.

Currently the only thing this module does is providing an C<INSTALLDIRS> option
to C<ExtUtils::MakeMaker>'s C<WriteMakefile> function, so dual-life modules will
be installed in the right section of C<@INC> depending on different versions of
perl.

As more things that need special handling for dual-life modules show up, this
module will try to address them as well.

The options added to your C<Makefile.PL> by this module are roughly equivalent
to:

    'INSTALLDIRS' => ($] >= 5.009005 && $] <= 5.011000 ? 'perl' : 'site'),

If the module didn't enter core in 5.009005, set the C<entered_core>
attribute appropriately:

    [DualLife]
    entered_core=5.006001

=head1 ATTRIBUTES

=head2 entered_core

Indicates when the distribution joined core.  Defaults to 5.009005 for
all the things that came in for 5.10.

=head2 eumm_bundled

Boolean for distributions bundled with ExtUtils::MakeMaker.  Prior to v5.12,
bundled modules might get installed into the core library directory, so
even if they didn't come into core until later, they need to be forced into
core prior to v5.12 so they take precedence.

=head1 ACHTUNG!

=over 4

=item *

This module is a really B<gross> hack

To do the things it does properly, C<Dist::Zilla>'s C<MakeMaker> plugin would
first have to be reworked a lot. If and whenever that happens, this module will
be adapted accordingly and maybe stop being so horribly wrong.

=item *

Only C<ExtUtils::MakeMaker> is supported

If you decide to use an install tool other than C<ExtUtils::MakeMaker>, this
module will C<NOT> work. However, that's only because I happen to use
C<ExtUtils::MakeMaker> only. If you prefer using C<Module::Build> or something
else instead and want to use this plugin with it, I'd be very to apply patches
to make it work with those other install tools.

=item *

Only works with C<Makefile.PL>s generated by C<Dist::Zilla::Plugin::MakeMaker>

This module rewrites the contents of an existing C<Makefile.PL> to insert
additional options. The heuristics it currently uses for that depend on the
conventions currently used in C<Makefile.PL>s generated with
C<Dist::Zilla::Plugin::MakeMaker>. If those change, this module will
break. However, I'll do my best to update this plugin if that ever happens.

Hopefully we'll have a more extensible C<MakeMaker> plugin at some point, so all
these issues will go away.

=back

=for Pod::Coverage setup_installer

=head1 AUTHOR

Florian Ragwitz <rafl@debian.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Florian Ragwitz.

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

=cut
