#!/usr/bin/env perl

BEGIN { push(@INC, $ENV{GLOBUS_LOCATION} . "/lib/perl"); }

# ------------------------------------------------------------------------
# test-toolkit - A script to test an installation of the Globus Toolkit.
# ------------------------------------------------------------------------

use strict;
use Getopt::Long;
use Cwd;
use Config;
use File::Find;
use Globus::Testing::Utilities;

# ------------------------------------------------------------------------
# $Revision: 1.5.78.1 $
# ------------------------------------------------------------------------
my $VERSION = "test-toolkit version:  ".`ident $0 | grep Revision | awk {'print \$2;'}`;

# ------------------------------------------------------------------------
# Get options
# ------------------------------------------------------------------------
my ($help, $no_credentials_setup, $version, $debug, $color, $remote, $directory);

GetOptions (
    'help'              => \$help,
    'no-credentials-setup' => \$no_credentials_setup,
    'color'             => \$color,
    'debug'             => \$debug,
    'version'           => \$version,
    'remote=s'          => \$remote,
    'directory=s'       => \$directory
);

if (defined $help)        { help(); exit; }
if (defined $version)     { print "$VERSION"; exit; }

if (! -d "$ENV{GLOBUS_LOCATION}") 
{
    die "You need to set GLOBUS_LOCATION in your environment\n";
}

if (defined $remote) 
{
    $ENV{TEST_REMOTE} = $remote;
}

# once we set these environment variables, we can create a 'Utility'.
$ENV{TEST_DEBUG} = $debug;
$ENV{TEST_COLOR} = $color;

my $u = new Utilities();

$u->debug("color -> $color");
$u->debug("debug -> $debug");
$u->debug("globus -> $ENV{GLOBUS_LOCATION}");
$u->debug("remote -> $remote");

if (! defined $directory)   {
    $directory = "$ENV{GLOBUS_LOCATION}/test";
}
$u->debug("directory -> $directory");

if (! defined $no_credentials_setup)  { setup_credentials(); }

# ========================================================================
# Main
# ========================================================================

start();
find(\&Wanted, $directory);
done();

exit;


# ========================================================================
# Subroutines
# ========================================================================

# ------------------------------------------------------------------
# Help
# ------------------------------------------------------------------
sub help {
    print "Usage: test-toolkit [options]

    Options:

    [--help]                 Print this message
    [--no-credentials-setup] Don't setup credentials for tests
    [--version]              Print out version of test-toolkit
    [--debug]                Turn on debugging
    [--color]                Turn on color
    [--remote=<FQDN>]        Run remote tests against <FQDN>
                             Default is to only run local tests
    [--directory=<dir>]      Only run tests behind <dir>     
                             Default is GLOBUS_LOCATION/test
\n";
}

# ------------------------------------------------------------------
# run_TESTS
# ------------------------------------------------------------------
sub Wanted {
    $u->debug("file -> $_");
    $u->debug("directory -> $File::Find::dir");

    /^TESTS\.pl\z/s && $u->command("./TESTS.pl") ||
        /^(vendor|g)cc(32|64).*\z/s && ($File::Find::prune = 1);
}

# ------------------------------------------------------------------
# start
# ------------------------------------------------------------------
sub start {
    $u->debug("Starting test-toolkit");

    my @date = split /\s+/, localtime;
    $u->announce("test-toolkit: started $date[1].$date[2].$date[4]  $date[3]");
}

# ------------------------------------------------------------------
# setup credentials info
# ------------------------------------------------------------------
sub setup_credentials {
    $u->debug("Setting up credentials...");

    $ENV{X509_CERT_DIR}   = "$ENV{GLOBUS_LOCATION}/test/globus_test";
    $ENV{X509_USER_CERT}  = "$ENV{X509_CERT_DIR}/testcred.pem";
    $ENV{X509_USER_KEY}   = "$ENV{X509_CERT_DIR}/testcred.pem";
    $ENV{X509_USER_PROXY} = "$ENV{X509_CERT_DIR}/testcred.pem";
    $ENV{GRIDMAP}         = "$ENV{X509_CERT_DIR}/grid-mapfile";

    $u->debug("Environment variables set up");

    chmod 0600, $ENV{X509_USER_PROXY};

    my $subject = system("grid-proxy-info", "-identity");

    if ($? != 0) {
        print "Unable to determine identity from proxy file $ENV{X509_USER_PROXY}";
	exit 1;
    }

    system("rm -f $ENV{GRIDMAP} >/dev/null 2>&1");

    system("grid-mapfile-add-entry -dn \"$subject\" -ln `whoami` -f $ENV{GRIDMAP} >/dev/null 2>&1");

    if ($? != 0) {
        print "Unable to add identity \"$subject\" to gridmap $ENV{GRIDMAP}";
	exit 2
    }
    $u->debug("Credentials setup done.");

}

# ------------------------------------------------------------------
# done
# ------------------------------------------------------------------
sub done {
    $u->announce("Done");
}
