#!/usr/bin/perl -w
use strict;
use lib 'blib/lib';
use lib 'blib/arch';

# Run Proc::UID test suite and send results to maintainer.
#
# This asks the user for confirmation before sending.
#
# Options:
#
# 	-y	- Yes, send the results (no interaction required).
#
# Paul Fenwick <pjf@cpan.org>
# June 2004.

my $MAIL_TEST_TO = 'proc-uid-test@perltraining.com.au';

eval "use Mail::Send;";
if ($@) {
	die "Sorry, I can't mail the test results without the Mail::Send module\n";
}

$ARGV[0] ||= "";
my $YES_SEND = $ARGV[0] eq "-y" ? 1 : 0 ;

print "Building makefile...\n";

my $makefile = `perl Makefile.PL`;

print "Building module...\n";
my $build = `make`;

print "Loading module information...\n";

eval "use Proc::UID;";
if ($@) {
	print "Proc::UID appeared not to build - $@\n";
	print "$build\n";
	print "\n\nI can't send test info without a successful build.\n";
	exit 1;
}

my $VERSION = $Proc::UID::VERSION or die "Cannot get \$Proc::UID::VERSION\n";
my $perl_info = `perl -V`;

print "Running tests...\n";

my $test_results = `make test`;

my $as_root = ($> == 0) ? "as root" : "as user";

my $SUBJECT = "[Proc::UID Test] Proc::UID $VERSION ($^O, perl $]) $as_root";

unless ($YES_SEND) {

	# Let's try to start a pager for the user.

	open(PAGER,"|$ENV{PAGER}") or
	open(PAGER,"|pager")       or
	open(PAGER,"|less")        or
	open(PAGER,"|more")        or
	open(PAGER,"|cat")         or die "Can't find a pager, not even cat!\n";

	print PAGER "The following will be sent to $MAIL_TEST_TO :\n\n\n\n\n";

	print PAGER	"Subject: $SUBJECT\n\n",
			"$perl_info\n\n",
			"$test_results\n\n";

	close(PAGER);

	print "Shall I send this now? [y]";

	chomp( my $answer = <STDIN> );

	$answer ||= "y";

	die "Not sending mail at user request\n" unless (lc($answer) eq "y");

}

print "Sending mail...\n";

my $msg = Mail::Send->new;
$msg->to($MAIL_TEST_TO);
$msg->subject($SUBJECT);
my $fh = $msg->open;

print $fh "$perl_info\n\n$test_results\n\n";

$fh->close;

print "Mail sent.  Thank-you for testing Proc::UID $VERSION\n";
