#!perl -w
#############################################################################
#
# If your platform/installation does not support make test, you can try this:
#
#    perl maketest
#
#############################################################################

use strict;
use Test::Harness qw(&runtests $verbose);

$verbose = 0;


#############################################################################
# Are we in the right directory?  If not, the pathname component of $0
# must be pointing to it or we wouldn't be running this script.
#

unless(-f 'MANIFEST') {
  my($script_name, $script_dir) = File::Basename::fileparse($0);
  if($script_dir) {
    chdir($script_dir) || die "chdir($script_dir): $!";
  }
}


#############################################################################
# Confirm distribution is complete (read MANIFEST file without assuming it
# has been converted to platform's native text format).
# Build up a list of test files as we go.
#

my @tests = ();
{
  open(MNFST, 'MANIFEST') || die "open(MANIFEST): $!";
  local($/) = undef;
  foreach(split(/[\r\n]+/, <MNFST>)) {
    next unless(/\S/);
    my $src_file = File::Spec->catfile(split('/'));
    (-f $src_file  ) || die "Could not find expected file: $src_file";
    push @tests, $src_file if($src_file =~ /^t\b.*\.t$/);
  }
  close(MNFST);
}

print "XML::Distribution appears complete\n";


#############################################################################
# Build and populate what we need of blib (the build library)
#

my $path = 'blib';
(-d $path) || mkdir($path, 0777) || die "mkdir($path): $!";

$path = File::Spec->catdir($path, 'lib');
(-d $path) || mkdir($path, 0777) || die "mkdir($path): $!";

$path = File::Spec->catdir($path, 'XML');
(-d $path) || mkdir($path, 0777) || die "mkdir($path): $!";

$path = File::Spec->catfile($path, 'Simple.pm');
unless(-f $path) {
  open(MOD, 'Simple.pm') || die "open(Simple.pm): $!";
  {
    local($/) = undef;
    my $module = <MOD>;
    close(MOD);
    open(MOD, ">$path") || die "open($path): $!";
    print MOD $module;
    close(MOD);
  }
  print "Created $path\n";
}


#############################################################################
# Run the tests
#

print "Running tests...\n";

unshift @INC, 'blib/lib';

@tests = @ARGV if(@ARGV);

runtests @tests;


