#!./perl -w

print "1..7\n";

use strict;
use ExtUtils::MakeMaker;
use Config;
use File::Spec::Functions qw(:DEFAULT rel2abs);

# Because were are going to be changing directory before running Makefile.PL
my $perl = rel2abs( $^X );

print "# perl=$perl\n";
my $runperl = "$perl -x \"-I../blib/lib\"";

$| = 1;

my $dir = "ext-$$";
my @files;

print "# $dir being created...\n";
mkdir $dir, 0777 or die "mkdir: $!\n";


END {
    use File::Path;
    print "# $dir being removed...\n";
    rmtree($dir);
}

my $package = "ExtTest";


################ XS
my $xs = catfile($dir, "$package.xs");
push @files, "$package.xs";
open FH, ">$xs" or die "open >$xs: $!\n";

print FH <<'EOT';
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

char *
archname()
{
  return ARCHNAME;
}

EOT
print FH "MODULE = $package		PACKAGE = $package\n";
print FH <<'EOT';

PROTOTYPES: ENABLE

char *
archname()

EOT

close FH or die "close $xs: $!\n";

################ PM
my $pm = catfile($dir, "$package.pm");
push @files, "$package.pm";
open FH, ">$pm" or die "open >$pm: $!\n";

print FH "package $package;\n";
print FH "use $];\n";

print FH <<'EOT';

use strict;
use Carp;

require Exporter;
require DynaLoader;
use vars qw ($VERSION @ISA @EXPORT_OK);

$VERSION = '0.01';
@ISA = qw(Exporter DynaLoader);
@EXPORT_OK = qw(archname);
EOT
print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
close FH or die "close $pm: $!\n";
################ test.pl
my $testpl = catfile($dir, "test.pl");
push @files, "test.pl";
open FH, ">$testpl" or die "open >$testpl: $!\n";

print FH "use strict;\n";
print FH "use $package qw(archname);\n";
print FH <<'EOT';
use Config;
print "1..1\n";

my $xs_archname = archname;
if ($xs_archname eq $Config{archname}) {
  print "ok 1\n";
} else {
  print "not ok 1 # &archname() eq '$xs_archname', \$Config{archname} eq '$Config{archname}'\n";
}
EOT
################ Makefile.PL
# We really need a Makefile.PL because make test for a no dynamic linking perl
# will run Makefile.PL again as part of the "make perl" target.
my $makefilePL = catfile($dir, "Makefile.PL");
push @files, "Makefile.PL";
open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
print FH <<"EOT";
#!$perl -w
use ExtUtils::MakeMaker;
WriteMakefile(
              'NAME'		=> "$package",
              'VERSION_FROM'	=> "$package.pm", # finds \$VERSION
              (\$] >= 5.005 ?
               (#ABSTRACT_FROM => "$package.pm", # XXX add this
                AUTHOR     => "$0") : ())
             );
EOT

close FH or die "close $makefilePL: $!\n";

chdir $dir or die $!; push @INC,  '../../lib';
END {chdir ".." or warn $!};

my @perlout = `$runperl Makefile.PL PERL_CORE=1`;
if ($?) {
  print "not ok 1 # $runperl Makefile.PL failed: $?\n";
  print "# $_" foreach @perlout;
  exit($?);
} else {
  print "ok 1\n";
}


my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
if (-f "$makefile$makefile_ext") {
  print "ok 2\n";
} else {
  print "not ok 2\n";
}
my $makefile_rename = ($^O eq 'VMS' ? '.mms' : '.old');
push @files, "$makefile$makefile_rename"; # Renamed by make clean

my $make = $Config{make};

$make = $ENV{MAKE} if exists $ENV{MAKE};

if ($^O eq 'MSWin32' && $make eq 'nmake') { $make .= " -nologo"; }

my @makeout;

print "# make = '$make'\n";
@makeout = `$make`;
if ($?) {
  print "not ok 3 # $make failed: $?\n";
  print "# $_" foreach @makeout;
  exit($?);
} else {
  print "ok 3\n";
}

if ($Config{usedl}) {
  print "ok 4\n";
} else {
  my $makeperl = "$make perl";
  print "# make = '$makeperl'\n";
  @makeout = `$makeperl`;
  if ($?) {
    print "not ok 4 # $makeperl failed: $?\n";
  print "# $_" foreach @makeout;
    exit($?);
  } else {
    print "ok 4\n";
  }
}

my $maketest = "$make test";
print "# make = '$maketest'\n";

@makeout = `$maketest`;

my $test = 5;

if ($?) {
  print "not ok $test # $maketest failed: $?\n";
  print "# $_" foreach @makeout;
} else {
  print "ok $test\n";
}
$test++;

my $makeclean = "$make clean";
print "# make = '$makeclean'\n";
@makeout = `$makeclean`;
if ($?) {
  print "not ok $test # $make failed: $?\n";
  print "# $_" foreach @makeout;
} else {
  print "ok $test\n";
}
$test++;

foreach (@files) {
  unlink $_ or warn "unlink $_: $!";
}

my $fail;
opendir DIR, "." or die "opendir '.': $!";
while (defined (my $entry = readdir DIR)) {
  next if $entry =~ /^\.\.?$/;
  print "# Extra file '$entry'\n";
  $fail = 1;
}
closedir DIR or warn "closedir '.': $!";
if ($fail) {
  print "not ok $test\n";
} else {
  print "ok $test\n";
}
