#!/usr/bin/env perl

BEGIN {
    if (exists $ENV{GLOBUS_LOCATION}) {
        push(@INC, $ENV{GLOBUS_LOCATION}. '/lib/perl');
    }
}
use POSIX qw(getcwd);
use Globus::Testing::Container;
use strict;
use Getopt::Long;
use File::Path;

my $scheduler = 'Fork';
my $tmpdir = make_tmpdir();
my $help = 0;

GetOptions('scheduler|s=s' => \$scheduler,
           'help' => \$help);

if ($help) {
    print STDERR "Usage: $0 [-scheduler SCHEDULER] [-help]\n";
    exit 1;
}

my $proxy_valid = `grid-proxy-info -timeleft`;
chomp($proxy_valid);
if ($? >> 8 != 0 || $proxy_valid < 30*60) {
    print STDERR "Unable to run tests because your proxy is not valid or\n"
               . "has too little time left.\n";
   exit(1);
}
my $id = `grid-proxy-info -identity`;
chomp($id);

if ($? >> 8 != 0 || $id eq '') {
    print STDERR "Unable to determine identity from proxy\n";
    exit(1);
}

# Start a local service container and submit non-staging jobs to it
my $gridmap = "$tmpdir/grid-mapfile";
my $descriptor = "$tmpdir/global_security_descriptor.xml";

&write_gridmap($gridmap, $id);
&write_security_descriptor($descriptor, $gridmap);

my $wd = getcwd();
my $java_container = new Globus::Testing::Container({
    type => 'java',
    security => 1,
    descriptor => $descriptor
});

my $java_contact = $java_container->contact();
$java_contact =~ m|https?://[^:]*:(\d+)|;
my $port = $1;

my $dn = `grid-proxy-info -issuer`;
chomp $dn;

# Call The Top Level Script
my $rc =
system(
    "./make-test-script.pl -scheduler $scheduler -port $port -dir submit_test submit_test/submit_test_script");
$rc |= system(
    "./make-test-script.pl -scheduler $scheduler -port $port -dir failure_test failure_test/failure_test_script");
$rc |= system(
    "./run-test-script.pl submit_test submit_test_script -subject \"$dn\"");
$rc |= system(
    "./run-test-script.pl failure_test failure_test_script -subject \"$dn\" ");

exit($rc ne 0);

sub write_gridmap
{
    my $gridmap = shift;
    my $id = shift;

    local(*GRIDMAP);

    open(GRIDMAP, ">$gridmap");
    print GRIDMAP "\"$id\" " . (getpwuid($<))[0] . "\n";
    close(GRIDMAP);
}

sub write_security_descriptor
{
    my $descriptor = shift;
    my $gridmap = shift;

    open(DESC_OUT, ">$descriptor");

    print DESC_OUT <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<securityConfig xmlns="http://www.globus.org">
    <gridmap value="$gridmap"/>
</securityConfig>
EOF
    close(DESC_OUT);
}


# make_tmpdir
sub make_tmpdir
{
    my $root;
    my $suffix = 'gram_scheduler_test_';
    my $created = 0;
    my $tmpname;
    my $relativepath;
    my @acceptable = split(//, "abcdefghijklmnopqrstuvwxyz".
                      "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
                      "0123456789");
    if(exists($ENV{TMPDIR}))
    {
        $root = $ENV{TMPDIR};
    }
    else
    {
        $root = "$ENV{GLOBUS_LOCATION}/tmp";
    }
    while($created == 0)
    {
    $relativepath = $suffix .
                $acceptable[rand() * $#acceptable] .
                $acceptable[rand() * $#acceptable] .
                $acceptable[rand() * $#acceptable] .
                $acceptable[rand() * $#acceptable] .
                $acceptable[rand() * $#acceptable] .
                $acceptable[rand() * $#acceptable];
     $tmpname = $root . "/" . $relativepath;
     $relativepath = "./$relativepath";
     $created = mkdir($tmpname, 0700);
     if($created)
     {
         if(-l $tmpname or ! -d $tmpname or ! -o $tmpname)
         {
          $created = 0;
         }
     }
    }
    return $tmpname;
}

END {
    if (-d $tmpdir && -o $tmpdir) {
        File::Path::rmtree($tmpdir);
    }
}
