The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!perl -w

use strict;

$/ = undef;

use File::Copy;
use File::Spec;

my @files = grep { -f $_ } qw(Makefile.PL Build.PL);
push @files, 'Makefile.PL' unless(@files);

my @oses = @ARGV;

foreach my $os (@oses) {
    eval "use Devel::AssertOS qw($os)";
    die("Couldn't find a module for $os\n") if($@ && $@ !~ /OS unsupported/);
}

my %modules = map { $_ => $INC{$_} } grep { /Devel/i && /(Check|Assert)OS/i } keys %INC;

mkdir 'inc';
mkdir 'inc/Devel';
mkdir 'inc/AssertOS';
foreach my $module (keys %modules) {
    my $dir = join('::', split(/\W+/, $module));
    $dir =~ s/::pm//;
    my @dircomponents = ('inc', (split(/::/, $dir)));
    pop @dircomponents;
    my $file = join('::', split(/\W+/, $module));
    $file =~ s/::pm/.pm/;
    $file =~ s/.*:://;
    mkdir File::Spec->catdir(@dircomponents);
    copy($modules{$module},
         File::Spec->catfile(@dircomponents, $file));
    open(MANIFEST, '>>MANIFEST') || warn("Can't update MANIFEST\n");
    print MANIFEST "\n".join('/', @dircomponents, $file);
    close(MANIFEST);
}

foreach my $file (@files) {
    open(FILE, $file) || next;
    my $contents = <FILE>;
    close(FILE);
    open(FILE, ">$file") || die("Can't write $file\n");
    print FILE 'use lib inc; use Devel::AssertOS qw('.
        join(' ', @oses).
        ");\n\n";
    print FILE $contents;
    close(FILE);
}

=head1 NAME

use-devel-assertos - a script to package Devel::AssertOS modules
with your code.

=head1 DESCRIPTION

This script, when run in the directory in which your shiny new module
lives, will bundle whichever Devel::AssertOS modules you ask it to
in the C<inc> directory, and update your Makefile.PL (or Build.PL)
appropriately.  If neither exists, it will create a Makefile.PL.

=head1 SYNOPSIS

    ./use-devel-assertos NetBSD OpenBSD FreeBSD

=head1 USAGE

In the example above, this will insert code to make your module
depend on one of the specified OSes.

=head1 WARNINGS, BUGS and FEEDBACK

This script has not been thoroughly tested.  You should check by
hand that it has done what you expected after running it.

If you use Module::Build::Compat to write a Makefile.PL, then you
will need to re-run this script whenever you have generated a new
Makefile.PL.

I welcome feedback about my code, including constructive criticism.
Bug reports should be made using L<http://rt.cpan.org/> or by email.

=head1 SEE ALSO

L<Devel::AssertOS>

L<Devel::CheckOS>

L<Devel::AssertLib>

=head1 AUTHOR

David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>

=head1 COPYRIGHT and LICENCE

Copyright 2007 David Cantrell

This software is free-as-in-speech software, and may be used,
distributed, and modified under the same conditions as perl itself.

=head1 CONSPIRACY

This software is also free-as-in-mason.

=cut