
$user = (getpwuid($>))[0];

$PT = './pt';

$| = 1;

print <<EOF;
Running tests for pt..

Should anything fail, this make will abort.
if it doesn't look right, just hit ^C.

EOF

&cr;

print "First, I'll run pt on just your processes:\n\n";

&run($PT);
while(<PT>) {
    print;
    &test1;
} 
&ran;
&result1;
&cr;

print <<EOF;
Ok, next test: run pt on just this process, tracing back to the root:

EOF

&run ("$PT $$");

while (<PT>) {
    print;
    &test2;
} 
&ran;

&result2;

&cr;

print "\nNow for all processes:\n\n";

&run("$PT -a");

$init = $sawme = $warned = $sawpt = 0;

while (<PT>) {
    print;
    &test3;
} 
&ran;
&result3;


system("touch ./test") && die "can't touch \"test\" semaphore file: $!";

print "\nLooks good, now go for the install!\n";

sub run {
    $Args = shift;
    print "$ $Args\n";
    open(PT, "$Args |") || die "couldn't fork: $!";
} 

sub ran {
    die "\"$Args\" exited badly: $!" unless close(PT);
} 

sub cr {
    print "\nHit <CR> to continue, ^C to abort: ";
    <STDIN>;
    print "\n";
} 

sub more {
    print "$_[0]; continue? [n] ";
    exit 1 unless <STDIN> =~ /^\s*y/i;
} 

sub test1 {
    $sawpt += /pt/;
    $sawme += /\b$user\b/i;
    if (/^\(\d+\)/ && !$warned++) {
	@pw = getpwuid($1);
	if  ($pw[0]) {
	    &more("Hmmm, uid $1 didn't convert to $pw[0]");
	} else {
	    print "Oops, you don't have a uid $1 here\n";
	    &more("Maybe the ps line is mangled;");
	} 
    } 
}

sub test2 { 
    $init += /^\w+\s+1\b.+\binit\b/; 
}

sub result1 {
    &more("Your uid should have appeared here") unless $sawme;
    &more("The pt program should have appeared here") unless $sawpt;
}

sub result2 {
    &more("Didn't see init (process 1)") unless $init;
    &more("Saw init $init times") if $init > 1;
}

sub test3 {
    &test1;
    &test2;
}

sub result3 {
    &result1;
    &result2;
} 
