#!/bin/sh
#BHEADER**********************************************************************
# Copyright (c) 2006   The Regents of the University of California.
# Produced at the Lawrence Livermore National Laboratory.
# Written by the HYPRE team. UCRL-CODE-222953.
# All rights reserved.
#
# This file is part of HYPRE (see http://www.llnl.gov/CASC/hypre/).
# Please see the COPYRIGHT_and_LICENSE file for the copyright notice, 
# disclaimer, contact information and the GNU Lesser General Public License.
#
# HYPRE is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License (as published by the Free Software 
# Foundation) version 2.1 dated February 1999.
#
# HYPRE 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 terms and conditions of the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Revision: 1.126 $
#EHEADER**********************************************************************



perform_test()
{
  echo ""
  echo "======================================================================"
  echo "Running tests $HYPRE_MACHINE_COMMANDS in directory $HYPRE_REMOTE_DIR"
  echo "======================================================================"
  echo ""

#=============================================================================
#   Running tests on same machine as that executing this script
#   Destroy old directory; copy newly checked out version; cd to
#   HYPRE_REMOTE_DIR, create a temporary file indicating tests have started,
#   execute tests and create another temporary file to indicate tests completed.
#=============================================================================
  if [ "${HYPRE_COMPILE_MACHINE}" = "`hostname | sed 's/\..*$//'`" ]
  then
     if [ "${HYPRE_REMOTE_DIR}" != "${HYPRE_AUTOTEST_EXECUTE_DIR}" ]
     then
        rm -fR ${HYPRE_REMOTE_DIR}/linear_solvers
        cp -fpr ${HYPRE_AUTOTEST_EXECUTE_DIR}/linear_solvers ${HYPRE_REMOTE_DIR}
     fi
     cd ${HYPRE_REMOTE_DIR}
     echo "Autotest Started" > "autotest_start"
     [ -f linear_solvers/test/AUTOTEST/env.${HYPRE_ARCH} ] && \
        . linear_solvers/test/AUTOTEST/env.${HYPRE_ARCH} ${COMPILERSELECT}
     rm -fR ${HYPRE_REMOTE_DIR}/linear_solvers/autotest.*
     rm -fR ${HYPRE_REMOTE_DIR}/linear_solvers/test/autotest.*
     export HYPRE_RUNTEST_OPTS
     linear_solvers/test/AUTOTEST/autotest_test \
         ${HYPRE_MACHINE_COMMANDS} -v ${HYPRE_MESSAGE}
     echo "Autotest Done" > "autotest_done"

#=============================================================================
#   Running tests on remote machine
#     ssh to compile machine and remove existing linear_solvers directory
#     copy source code from the current directory to defined remote directory
#     ssh to remote machine, create a temporary file indicating tests have 
#     started, execute tests and create another temporary file to indicate tests
#     completed.
#=============================================================================
  else
    echo "deleting old AUTOTEST directory...."
    ssh ${HYPRE_COMPILE_MACHINE} "\
    (/bin/sh -c \"\
        rm -fR ${HYPRE_REMOTE_DIR}/linear_solvers;\"\
    )"

    echo "copying new source...."
    cp -fpr linear_solvers ${HYPRE_REMOTE_DIR}

    echo "Begin running remote tests...."
    ssh -f ${HYPRE_COMPILE_MACHINE} "\
    (/bin/sh -c \"\
      cd ${HYPRE_REMOTE_DIR};\
      echo 'Autotest Started' > 'autotest_start';\
      [ -f linear_solvers/test/AUTOTEST/env.${HYPRE_ARCH} ] && \
        . linear_solvers/test/AUTOTEST/env.${HYPRE_ARCH} ${COMPILERSELECT};\
      rm -fR ${HYPRE_REMOTE_DIR}/linear_solvers/autotest.*;\
      rm -fR ${HYPRE_REMOTE_DIR}/linear_solvers/test/autotest.*;\
      export HYPRE_RUNTEST_OPTS=\\\"${HYPRE_RUNTEST_OPTS}\\\";\
      linear_solvers/test/AUTOTEST/autotest_test \
            ${HYPRE_MACHINE_COMMANDS} -v ${HYPRE_MESSAGE};\
      echo 'Autotest Done' > 'autotest_done';\
      chmod -fR a+rX,ug+w,o-w linear_solvers;\
      chgrp -fR hypre linear_solvers\"\
    )" 
  fi
  echo "returning to autotest...."
}


#=============================================================================
# Function for display of help/usage
#=============================================================================
help () {
  printf "Usage: $0 [-nocvs|-rev Rev#] [-h|-help] [-d|-debug] HostOptions [HostOptions ... ]\n"
  printf "\n"
  printf "HYPRE autotest script. This is the high level wrapper for the\n"
  printf "autotest_test script, which is run nightly from cron. This script\n"
  printf "determines which systems autotest_test is to be executed on, and the\n"
  printf "options to use.\n"
  printf "\n"
  printf "%s\n" "-nocvs    Do not checkout the latest source code from CVS."
  printf "                 Use the existing ./linear_solvers directory for\n"
  printf "                 testing.\n"
  printf "%s\n" "-rev Rev# Check out the specified revision or tagged source" 
  printf "%s\n" "          from CVS." 
  printf "%s\n" "-help     Print this information and exit."
  printf "%s\n" "-debug    Turn on debug mode."
  printf "\n"
  printf "HostOptions      Host ID or configuration on which autotest_test is to be run.\n"
  printf "                 Valid host IDs are:\n"
  printf "%s\n" "     -alc       LC - CHAOS."
  printf "%s\n" "     -mcr       LC - CHAOS."
  printf "%s\n" "     -pengra    LC - CHAOS."
  printf "%s\n" "     -thunder   LC - CHAOS."
  printf "%s\n" "     -ubgl      LC - SLES."
  printf "%s\n" "     -up        LC - AIX."
  printf "%s\n" "     -vertex    LC - Opteron."
  printf "%s\n" "     -tux       CASC - Linux."
  printf "%s\n" "     -insure    CASC - Linux, executing insure++."
  printf "%s\n" "     -tuxgcc    CASC - gcc compilers"
  printf "%s\n" "     -tuxpgi    CASC - PGI compilers"
  printf "%s\n" "     -tuxabsoft CASC - Absoft-Pro8.0 Fortran 90"
  printf "%s\n" "     -tuxlahey  CASC - Lahey Fortran compiler"
  printf "%s\n" "     -tuxintel  CASC - Intel compilers"
  printf "%s\n" "     -tuxkai    CASC - KAI KCC compiler"
  printf "\n"
}


usage ()
{
  printf "Usage: $0 [-nocvs|-rev Rev#] [-h|-help] [-d|-debug] HostOptions [HostOptions ... ]\n"
}


#==============================================================================
#    Sets options for machines on which regression tests are being run
#==============================================================================
hypre_set_machine_options()
{
  case $1 in
     -tux)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="tux136"
        HYPRE_RUN_MACHINE="tux136"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/i686-pc-linux-gnu/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -b 1 -nsde 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='PRIMARY[default]'
        COMPILERSELECT="default"
        ;;
     -insure)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="tux136"
        HYPRE_RUN_MACHINE="tux136"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/i686-pc-linux-gnu-insure/AUTOTEST"
	HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -i 2"
        HYPRE_RUNTEST_OPTS=""
        HYPRE_MESSAGE='PRIMARY[insure++]'
        ;;
     -alc)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="alc"
        HYPRE_RUN_MACHINE="alc"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/chaos_3_x86_elan3/AUTOTEST"
	HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -ne 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[ALC]'
        ;;
     -mcr)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="mcr"
        HYPRE_RUN_MACHINE="mcr"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/chaos_3_x86_elan3/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -nse 1 -a 2 -Bcf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[MCR]'
        ;;
     -pengra)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="pengra"
        HYPRE_RUN_MACHINE="pengra"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/chaos_3_x86_elan3/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -nse 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[Pengra]'
        ;;
     -thunder)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="thunder"
        HYPRE_RUN_MACHINE="thunder"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/chaos_3_ia64_elan4/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -nse 1 -a 2 -cf"
        HYPRE_MESSAGE='SECONDARY[Thunder]'
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        ;;
     -ubgl)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="ubgl"
        HYPRE_RUN_MACHINE="ubgl"
        HYPRE_REMOTE_DIR="/usr/gapps/hypre/sles_9_ppc64/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -nse 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[uBGL]'
        ;;
     -up)
        HYPRE_ARCH="aix"
        HYPRE_COMPILE_MACHINE="up"
        HYPRE_RUN_MACHINE="up"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/aix_5_64_fed/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -nse 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[uP]'
        ;;
     -vertex)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="vertex"
        HYPRE_RUN_MACHINE="vertex"
        HYPRE_REMOTE_DIR="/usr/gapps/hypre/chaos_3_x86_64_ib/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -nse 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[vertex]'
        ;;
     -tuxpgi)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="tux136"
        HYPRE_RUN_MACHINE="tux136"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/i686-pc-linux-gnu-pgi5.x/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -ndse 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[pgi]'
        COMPILERSELECT="pgi"
        ;;
     -tuxabsoft)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="tux136"
        HYPRE_RUN_MACHINE="tux136"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/i686-pc-linux-gnu-gcc3.x/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -b 1 -nde 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[Absoft-Pro8.0]'
        COMPILERSELECT="absoft"
        ;;
     -tuxlahey)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="tux136"
        HYPRE_RUN_MACHINE="tux136"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/i686-pc-linux-gnu-gcc3.x/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -b 1 -nde 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[lahey]'
        COMPILERSELECT="lahey"
        ;;
     -tuxintel)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="tux136"
        HYPRE_RUN_MACHINE="tux136"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/i686-pc-linux-gnu-gcc3.x/AUTOTEST"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -b 1 -nde 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[Intel-8.0]'
        COMPILERSELECT="intel"
        ;;
     -tuxkai)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="tux136"
        HYPRE_RUN_MACHINE="tux136"
        HYPRE_REMOTE_DIR="/usr/casc/hypre/i686-pc-linux-gnu-kai/AUTOTEST"
	HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -b 1 -nde 1 -a 2 -cf"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='SECONDARY[KCC]'
        COMPILERSELECT="kai"
        ;;
     -tuxlocal)
        HYPRE_ARCH="linux"
        HYPRE_COMPILE_MACHINE="tux136"
        HYPRE_RUN_MACHINE="tux136"
        HYPRE_REMOTE_DIR="`pwd`"
        HYPRE_MACHINE_COMMANDS="-r $HYPRE_RUN_MACHINE -l 1 -a 2"
        HYPRE_RUNTEST_OPTS="-D HYPRE_NO_SAVED"
        HYPRE_MESSAGE='LOCAL-TEST[default]'
        COMPILERSELECT="default"
        ;;
  esac
}

#==============================================================================
# Beginning of 'main program' of script for regression testing hypre.
# Initialize variables
#==============================================================================
DEBUG_MODE=""
HYPRE_MESSAGE=""
COMPILERSELECT=""
HYPRE_RUN_MACHINE=""
HYPRE_AUTOTEST_EXECUTE_DIR="`pwd`"
export HYPRE_RUN_MACHINE
export HYPRE_MESSAGE COMPILERSELECT HYPRE_AUTOTEST_EXECUTE_DIR

CVSROOT=/home/casc/repository
export CVSROOT


#=============================================================================
# Parse command line arguments.  If no args, print usage info and exit.
# the nocvs/rev/help/debug arguments are expected to precede all HostOptions
#=============================================================================
if [ $# -lt 1 ]
then
  usage
  exit 2
fi
case $1 in
    -h|-help) 
        help
        exit
        ;;
    -d|-debug) 
        DEBUG_MODE="yes"
        set -xv
        shift
        ;;
esac


#=============================================================================
# Define permissions
#=============================================================================
OldMask=`umask -S`
umask u=rwx,g=rx,o=rx


#=============================================================================
# Check out the repository into the HYPRE_REMOTE_DIR directory, if requested.
# The repository resides on the CASC cluster; before checking out a new version
# existing versions are removed.
#=============================================================================
if [ "$1" = "-nocvs" ]
then
   shift
else
   if [ ! -d "$CVSROOT" ]
   then
      echo "Error: no CVS repository location found, CVSROOT = $CVSROOT"
      exit
   fi
   rm -fR linear_solvers
   if [ "$1" = "-rev" ]
   then
      REV=$2
      shift
      shift
      cvs -q checkout -r $REV -P linear_solvers 1> $0.checkout.log 2> $0.checkout.err
   else
      cvs -q checkout -P linear_solvers 1> $0.checkout.log 2> $0.checkout.err
   fi
   chmod -f a+rX,ug+w,o-w $0.checkout.*
   chgrp -f hypre $0.checkout.*
fi


#==============================================================================
# set permissions and group for linear_solvers directory
#==============================================================================
chmod -fR a+rX,ug+w,o-w linear_solvers
chgrp -fR hypre linear_solvers


#==============================================================================
# Run test suites on remote machines
# NOTE: the command line should ONLY have the HostOptions left for parsing
#       allowances are made in this loop for other options to be interspersed
#==============================================================================

while [ "$*" != "" ]
do
  HYPRE_RUN_MACHINE=""
  COMPILERSELECT=""
  arg=$1
  cd ${HYPRE_AUTOTEST_EXECUTE_DIR}
  case ${arg} in
    -d|-debug) 
        DEBUG_MODE="yes"
        set -xv
        shift;;
    -h|-help) 
        help
        exit;;
    -nocvs)
        echo "ERROR: -nocvs option MUST be first argument! Quiting..."
        help
        exit;;
    *)
        hypre_set_machine_options ${arg}
        perform_test
        shift;;
  esac

done


#==============================================================================
#    Reset permissions
#==============================================================================
chmod -fR a+rX,ug+w,o-w ${HYPRE_REMOTE_DIR}/linear_solvers
chgrp -fR hypre ${HYPRE_REMOTE_DIR}/linear_solvers


#==============================================================================
# Restore original permissions
#==============================================================================
umask $OldMask
