#!/usr/bin/perl -w

=pod

=head1 NAME

pler - The DWIM Perl Debugger

=head1 DESCRIPTION

B<pler> is a small script which provides a sanity layer for debugging
test scripts in Perl distributions.

While L<prove> has proven itself to be a highly useful program for
manually running one or more groups of scripts in a distribution,
what we also need is something that provides a similar level of
intelligence in a debugging context.

B<pler> checks that the environment is sound, runs some cleanup tasks
if needed, makes sure you are in the right directory, and then hands off
to the perl debugger as normal.

=cut

use 5.005;
use strict;
use Config;
use File::Which ();
use File::Spec::Functions ':ALL';
use File::Find::Rule ();
use Devel::Pler;

# Convenience constants
use constant FFR  => 'File::Find::Rule';

use vars qw{$VERSION};
BEGIN {
	$VERSION = '0.14';
}





#####################################################################
# Main Script

my $script = shift @ARGV;
unless ( defined $script ) {
	print "# No file name pattern provided, using 't'...\n";
	$script = 't';
}
unless ( $script =~ /\.t$/ ) {
	# Only works in the dist-root for now
	unless ( in_distroot ) {
		error "Implied test names can only be used in the dist root";
	}

	# Get the list of possible tests
	my @possible = FFR->file->name('*.t')->in( 't' );

	# If a number, look for a numeric match
	my $pattern = quotemeta $script;
	my @matches = grep { /$pattern/ } @possible;
	unless ( @matches ) {
		error "No tests match '$script'";
	}
	if ( @matches > 1 ) {
		error(
			"More than one possible test",
			map { "  $_" } sort @matches,
		);
	}
	$script = $matches[0];
}
unless ( -f $script ) {
	error "Test script '$script' does not exist";
}

# Rerun make if needed
if ( in_distroot and has_makefile ) {
	run( make );
}

# Build the command to execute
my @flags = ();
if ( has_blib ) {
	push @flags, '-Mblib';
} elsif ( has_lib ) {
	push @flags, '-Ilib';
}

# Hand off to the perl debugger
unless ( Devel::Pler->is_verbose ) {
	message( "# Debugging $script" );
}
my @cmd = ( perl, @flags, '-d', $script );
handoff( @cmd );

=pod

=head1 TO DO

- Allow execution from the base or F<t> directory.

- Automatically run the F<Makefile.PL> if it needs to be

- Write a heuristic to determine if it needs to be

=head1 SUPPORT

All bugs should be filed via the bug tracker at

L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel-Pler>

For other issues, or commercial enhancement and support, contact the author

=head1 AUTHOR

Adam Kennedy E<lt>adamk@cpan.orgE<gt>

=head1 SEE ALSO

L<prove>, L<http://ali.as/>

=head1 COPYRIGHT

Copyright 2006 Adam Kennedy. All rights reserved.

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.

=cut
