#!/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 $
# ------------------------------------------------------------------------
my $VERSION = "test-toolkit version:  ".`ident $0 | grep Revision | awk {'print \$2;'}`;

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

GetOptions (
    'help'              => \$help,
    '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");

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

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

exit;


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

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

    Options:

    [--help]                 Print this message
    [--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]");
}

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