#!/usr/bin/perl -w
use strict;
use strict 'refs';
# ************************************************************************
# 
#                    Teuchos: Common Tools Package
#                 Copyright (2004) Sandia Corporation
# 
# Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
# license for use of this work by or on behalf of the U.S. Government.
# 
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# License, or (at your option) any later version.
#  
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#  
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
# Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
# 
# ************************************************************************
#
# RAB: 2003/11/14: According the the documentation that I can find
# it looks like this script should be invoked from:
#
#    BUILD_DIR/packages/teuchos/test
#
# and therefore I will write all of my relative paths from that
#
printf
  "\n*****************************************************".
  "\n*** Running Teuchos tests (no news is good news) ****".
  "\n*****************************************************\n";

my $success = 1;  # Boolean (false=0,true=nonzero)
my $mpirun;       # stratus="prun -n", any other platform="mpirun -np"
my $result;       # success=0, failure=nonzero
#
# Check for the mpirun command on this platform, which will
# be set by the test harness.  If it isn't specified, use 'mpirun'.
#
$result = $ENV{"TRILINOS_TEST_HARNESS_MPIGO_COMMAND"};
if (!$result) {
  $result = "mpirun -np";
}
$mpirun = $result . " 3 ";

#************************************
# RCP test
#************************************

$result = system ($mpirun . "./RCP/RefCountPtr_test.exe --quiet");
if ($result != 0) {
  # If we failed run it again in verbose mode.
  $success = 0;
  printf
    "\n\n*****************************************".
    "\n*** Running Teuchos RCP test ****".
    "\n*****************************************\n\n";
  system ($mpirun . "./RCP/RefCountPtr_test.exe");
} else {
  printf 
    "\n ****** RCP MPI test PASSED ****** \n";
}
#************************************
# ParameterList test
#************************************

$result = system ($mpirun . "./ParameterList/ParameterList_test.exe");
if ($result != 0) {
  $success = 0;  
  # If we failed run it again in verbose mode.
  printf
    "\n\n*******************************************".
    "\n*** Running Teuchos ParameterList test ****".
    "\n*******************************************\n\n";
  system ($mpirun . "./ParameterList/ParameterList_test.exe --v");
} else {
  printf
    "\n ***** ParameterList MPI test PASSED ***** \n";
}
#************************************
# Timer test
#************************************

$result = system ($mpirun . "./Time/Time_test.exe -v");
if ($result != 0) {
  $success = 0;  
  # If we failed run it again in verbose mode.
  printf
    "\n\n*******************************************".
    "\n******* Running Teuchos Timer test ********".
    "\n*******************************************\n\n";
  system ($mpirun . "./Time/Time_test.exe --v");
} else {
  printf
    "\n ********* Timer MPI test PASSED ********* \n";
}

#************************************
# Polling test
#************************************

$result = system ($mpirun . "./Polling/Polling_test.exe");
if ($result != 0) {
  $success = 0;  
  # If we failed run it again in verbose mode.
  printf
    "\n\n*******************************************".
    "\n******* Running Teuchos Polling test ********".
    "\n*******************************************\n\n";
  system ($mpirun . "./Polling/Polling_test.exe --v");
} else {
  printf
    "\n ********* Polling MPI test PASSED ********* \n";
}
# Return 0 if all the tests were successful, else return -1.
exit ($success ? 0 : -1 );
