#!/usr/bin/perl

use strict;

$|=1;

# Set up some variables
my %AGI;
my $tests = 0;
my $fail = 0;
my $pass = 0;


while (<STDIN>) {
	chomp;
	last unless length($_);
#	if (/^agi_(\w+)\:\s+(.*)$/) {
	if (/^(agi_[^:]+)\:\s+(.*)$/) {
		$AGI{$1} = $2;
	}
}

print STDERR "AGI Environment Dump:\n";
foreach my $i (sort keys %AGI) {
	print STDERR " -- $i = $AGI{$i}\n";
}

sub checkresult {
	my ($res) = @_;
	my $retval;
	$tests++;
	chomp $res;
	if ($res =~ /^200/) {
		$res =~ /result=(-?\d+)/;
		if (!length($1)) {
			print STDERR "FAIL ($res)\n";
			$fail++;
		} else {
			print STDERR "PASS ($res)\n";
			$pass++;
		}
	} else {
		print STDERR "FAIL (unexpected result '$res')\n";
		$fail++;
	}
}

print STDERR "Executing a gosub call\n";
#print "EXEC Gosub agi_gosub,s,1(one,two)\n";
print "EXEC Gosub agi_gosub s 1(one,two)\n";
my $result = <STDIN>;
&checkresult($result);

print STDERR "My AGI script is done\n";
