#! /usr/bin/env perl

use Globus::GRAM::Error;
use Globus::Core::Paths;

use IO::File;
use Test;
use File::Path;
use File::Compare;

my (@tests, @todo) = ();
my $contact = $ENV{CONTACT_STRING};
my $verbose = 0;

sub expire_test
{
    my $extra_rsl = shift;
    my $info = Globus::Core::Paths::bindir . "/grid-proxy-info";
    my $timeout;
    my $rsl = "&(executable=/bin/sleep)(arguments=90)";
    my $rc;

    chomp($timeout = `grid-proxy-info -timeleft`);

    $rsl .= "${extra_rsl}(proxy_timeout=${timeout})";

    if($verbose)
    {
	print "# Submitting job\n";

	foreach(split(/\n/, $rsl))
	{
	    print "#    $_\n";
	}
    }

    system("globusrun -s -r \"$contact\" \"$rsl\" >/dev/null 2>&1");
    $rc = $? >> 8;

    ok($rc, Globus::GRAM::Error::USER_PROXY_EXPIRED()->value);
}

push(@tests, "expire_test(\"\")");
push(@tests, "expire_test(\"(save_state = yes)\")");

if(@ARGV)
{
    my @doit;

    $verbose = 1;

    foreach(@ARGV)
    {
	if(/^(\d+)-(\d+)$/)
	{
	    foreach($1 .. $2)
	    {
		push(@doit, $_);
	    }
	}
	elsif(/^(\d+)$/)
	{
	    push(@doit, $1);
	}
    }
    plan tests => scalar(@doit);

    foreach (@doit)
    {
	eval "&$tests[$_-1]";
    }
}
else
{
    plan tests => scalar(@tests), todo => \@todo;

    foreach (@tests)
    {
	eval "&$_";
    }
}

