#!/usr/bin/env perl

use Getopt::Long;

$NAME ||= 'AUTHOR_NAME';
$EMAIL ||= 'AUTHOR_EMAIL';
$LICENSE ||= 'perl';
$KWALITEE = 0;

sub usage
{
    print STDERR <<'EOS';
Usage: newmod.pl [options] Module::Name
  --name X            Use author name X.
  --email X           Use author email X.
  --license X         Use license X (e.g. "perl").
  --kwalitee          Add boilerplate to increase kwalitee.
EOS
    exit 1;
}

@orig_argv = @ARGV;

GetOptions('name:s' => \$NAME,
           'email:s' => \$EMAIL,
           'license:s' => \$LICENSE
           'kwalitee' => \$KWALITEE) && @ARGV == 1 or usage;

$mod = shift;
($dir = $mod) =~ s/::/-/g;
mkdir $dir or die $!;
chdir $dir;

sub cat
{
    my $o = shift;
    open OUT, ">$o" or die $!;
    print OUT @_;
    close OUT;
}

($file = $mod) =~ s/.*:://;
(undef, undef, undef, $d, $m, $y) = localtime;
$y += 1900;

$license = {
    perl => <<'EOS',
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
EOS
    gpl => <<'EOS',
This library is free software under the terms of the GNU general public
license.
EOS
}->{lc $LICENSE} || '';

cat 'Makefile.PL', <<EOS;
use ExtUtils::MakeMaker;

WriteMakefile(
    NAME              => '$mod',
    VERSION_FROM      => '$file.pm',
    PREREQ_PM         => { },
    (\$] >= 5.005 ?     ## Add these new keywords supported since 5.005
      (AUTHOR         => q|$NAME <$EMAIL>|) : ()),
    ABSTRACT          => 'Something.',
);
EOS

cat "$file.pm", <<EOS;
package $mod;

=head1 NAME

$mod -- ...

=head1 SYNOPSIS



=cut

\$VERSION = '0.01';

1;
__END__

=head1 DESCRIPTION



=head1 AUTHOR

$NAME, E<lt>${EMAIL}E<gt>

Bug reports welcome, patches even more welcome.

=head1 COPYRIGHT

Copyright (C) $y, $NAME.
$license

=cut
EOS

cat "test.pl", <<EOS;
use Test::Simple tests => 1;
use $mod;
ok(1, 'loaded');
EOS

cat "ChangeLog", <<EOS;
$y-$m-$d  $NAME <$EMAIL>
	* original version; created by $0 @orig_argv
EOS

cat "README", <<EOS;
$mod version 0.01

INSTALLATION

To install this module, run the following commands:

    perl Makefile.PL
    make
    make test
    make install


DEPENDENCIES

COPYRIGHT AND LICENSE

Copyright (C) $y, $NAME
$license
EOS

cat 'MANIFEST', <<EOS;
Makefile.PL
MANIFEST
README
test.pl
$file.pm
EOS