#!/bin/csh
# ************************************************************************
# 
#        AztecOO: An Object-Oriented Aztec Linear Solver Package 
#                 Copyright (2002) 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) 
# 
# ************************************************************************

## NOTE: Those wishing to cusomize this script to run test exe's
## that have already been autotool'ed should read lines beginning with '##'

# $1 - Used only for automated testing.  No action required by script owner.
#      This parameter names the subdirectory from which the script is being
#      run.  This assists a developer in figuring out which tests failed.
# $2 - Indicates if the test is an automated nightly test.  No action required
#      by script owner.

set error = 0
set AnError = False
### maybe should pass $homedir and package name to script
#set printexitvalue
if( "$2" == "True" ) then # $2 is an optional parameter indicating if 
			  # this is an automated test or not
    # file2 is the log that is created and put into a results email if 
    # errors occur.
    set file2 = ../../../../logErrors.txt
    rm -f $file2
    # Echo some important information into the log file to help developers
    # figure out which tests failed.
## List the Trilinos package being tested on the following line
    echo "Package being tested: AztecOO  " > $file2
    echo "Name of subdirectory: " $1 >>& $file2
    echo "Date: " `eval date` >>& $file2
    echo `uname -a` >>& $file2
    #'file' is a shorter log that is retained even if all tests pass.
    set file = ../../../../log`eval uname`.txt
    rm -f $file
## IMPORTANT: Specify the script owner(s) on the following line
## For one owner type "owner@abc.com", for multiple owners
## "owner1@abc.com; owner2@def.com"
    echo "mheroux@cs.sandia.gov, Aztecoo-regression@software.sandia.gov" >>& $file
    echo "Script owner(s) is listed on the previous line." >>& $file
## List the Trilinos package being tested on the following line
    echo "Package being tested: AztecOO  " >>& $file
    echo "Name of subdirectory: " $1 >>& $file
else
    cd ../../../
    set file = log.`eval date +%d%b%Y_%H%M%S`
    rm -f $file
    echo $file
    echo "Date: " `eval date` > $file
endif
echo `uname -a` >>& $file
## Different directory structures will require different setups.
## This file assumes a structure like that of epetra - exe's live in 
## a direct subdirectory of 'epetra/test' 

## Keep in mind that file and file2-4 live in 'package_name/test'
## Also, 'package_name/test' is the current directory
## It is recommended that all directory changing be done relative to
## the current directory because scripts live in the source directory,
## but are invoked from various build directories

## List the subdirectories of 'test' containing test exe's in the foreach loop
## if directory structure is like that of epetra.
foreach f (AztecOO AztecOO_UserOpUserMat AztecOO_SetParameters)
	cd $f
	foreach g(*.exe)
		echo "############" $g "##############" >>& ../$file
		if( "$2" == "True" ) then
		    ./$g > ../log.txt
		    cat ../log.txt >>& ../$file
		    grep -i "passed" ../log.txt >& /dev/null
		    if( "$status" != "0" ) then
		    # A test failed.
			set AnError = True
			echo "############" $g "##############" >>& ../$file2
			./$g -v >>& ../$file2
		    endif
		    rm -f ../log.txt
		else
		# This is not an automated test.
		    ./$g -v >>& ../$file
		endif
	end
	cd ..
end

## At this point, it is assumed that the current directory is
## 'package_name/test'
if( "$AnError" != "True" && "$2" == "True" ) then
    rm -f $file2
endif

if ( "$AnError" == "True" ) then
    exit 1
else
    exit 0
endif
