# this is just a helper module for the installation phase, so it can
# be re-used during the standalone and mod_perl 2.0 install

# it has this unusual filename: install-pl, to prevent MakeMaker from
# installing it system-wide
package Apache::Test::install;

use strict;
use warnings;

use File::Spec::Functions qw(catfile catdir);

# on Case-Insensitive systems Apache/Test.pm can't coexist with
# Apache/test.pm, since Apache::test is now deprecated (was renamed to
# Apache/testold.pm in mod_perl 1.28, we need to find and remove any
# occurrences of this file. CPAN authors should
# s/Apache::test/Apache::testold/ and can either require mod_perl 1.28
# which already carries it or simply bundle it. The best option is to
# port the test suite to use Apache::Test which works with both
# mod_perl generations.
#
# we could have done this cleanup only for case-insensitive systems,
# but I feel that doing it for all systems, will speedup the
# transitions from Apache::test to either Apache::Test or
# Apache::testold.
#
sub nuke_Apache__test_target {

    my $cleanup_packlist = ".mypacklist";

    my @convicts = ();
    foreach (@INC) {
        my $dir = catdir $_, "Apache";
        next unless -d $dir;
        opendir DIR, $dir or die "Cannot opendir $dir: $!\n";
        my @matches = grep /^test.pm$/, readdir DIR;
        closedir DIR;
        push @convicts, map { catfile $dir, $_ } @matches if @matches;
    }

    if (@convicts) {
        print <<EOI;
!!! Makefile.PL has found old copies of Apache/test.pm which will
be removed during 'make install' to prevent collisions with Apache::Test:

@{[join "\n", @convicts]}

CPAN authors are advised to either use Apache::testold or port their
test suite to Apache::Test which works with both mod_perl generations.
EOI
    }

    open PACKLIST, ">$cleanup_packlist"
        or die "Can't open $cleanup_packlist: $!";
    print PACKLIST join "", map "$_\n", @convicts;
    close PACKLIST;

    return <<EOF;

nuke_Apache__test:
\t\$(FULLPERL) -MExtUtils::Install -e \\
\t"-e qq{$cleanup_packlist} && uninstall(qq{$cleanup_packlist}, 1, 0)"
EOF

}

1;
