#!/bin/sh
#-----------------------------------------------------------------------------
# testsuite: Run tests on the latest AIPS++ update
#-----------------------------------------------------------------------------
#
#   Copyright (C) 1992-1997,1998
#   Associated Universities, Inc. Washington DC, USA.
#
#   This program 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; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program 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 General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   Correspondence concerning AIPS++ should be addressed as follows:
#          Internet email: aips2-request@nrao.edu.
#          Postal address: AIPS++ Project Office
#                          National Radio Astronomy Observatory
#                          520 Edgemont Road
#                          Charlottesville, VA 22903-2475 USA
#
#-----------------------------------------------------------------------------
# Usage: testsuite [-i] [-m module ] [-p package]
#-----------------------------------------------------------------------------
# testsuite may optionally run an inhale and then do a gmake runtests.
# It is invoked regularly as a cron job, on aips2.nrao.edu.
#
# Options:
#   -i     do a gmake runtests after doing an inhale
#   -m     do a gmake runtests for a specfic module
#   -p     do a gmake runtests for a specfic package
#          the -m and -p options maybe combined
#
# Status return values correspond to the various phases of the operation
#    standard testsuite status
#
# Notes:
#=============================================================================
# Initialize
#-----------------------------------------------------------------------------
# Guard against being overwritten.
  case $0 in
  *-)
     ARGS=$*
     ;;
  *)
     cp $0 $0-
     exec $0- $*
     ;;
  esac

# Identify ourselves.
  HOST=`uname -n | sed -e 's/\..*//'`
  LOGNAME=${LOGNAME-$USER}

  echo ""
  echo "testsuite: Execution on $HOST by $LOGNAME commenced at"
  echo "testsuite: `adate`"
  echo "testsuite: `adate -l`"

  echo ""
  echo "testsuite: Command line arguments \"$*\""

# Check that CASAPATH is defined.
  if [ "$CASAPATH" = "" ] ; then
     exec 1>&2
     echo ""
     echo "testsuite: CASAPATH is undefined, abort!"
     exit 1
  fi

# Get the site name.
  SITE=`echo $CASAPATH | awk '{print $3}'`

# Produce a status report.
  echo ""
  echo "testsuite: Current status of the $SITE installation"
  avers -l

# Determine the AIPS++ root directory.
  AIPSROOT=`echo $CASAPATH | awk '{print $1}'`

# Check the accessibility of the AIPSROOT area.
  if [ ! -d "$AIPSROOT" ]
  then
     exec 1>&2
     echo ""
     echo "testsuite: The AIPS++ root directory does not exist or is"
     echo "           not accessible, $AIPSROOT"
     exit 1
  fi

# Get account information.
  AIPS2MGR=`getrc -i account.manager    2> /dev/null`
  AIPS2PRG=`getrc -i account.programmer 2> /dev/null`
  MGRACCNT=`echo $AIPS2MGR | sed -e 's/\..*//'`
  MGRGROUP=`echo $AIPS2MGR | sed -e 's/.*\.//'`
  PRGGROUP=`echo $AIPS2PRG | sed -e 's/.*\.//'`

# Check that this script is being run from the right account.
#  if [ "$MGRACCNT" != "$LOGNAME" -a "$MGRACCNT" != "" ]
#  then
#     exec 1>&2
#     echo ""
#     echo "testsuite: This script can only be run by $MGRACCNT, not" \
#          "$LOGNAME, abort!"
#     exit 1
#  fi

# Set default file creation mode.
  umask 002

# Parse command input.
  MODE=incremental
  HYBRID=
  DINKUM=
  DOCSYS=docsys
  TARGETS=runtests
  LATCHKEY=
  INHALE=0
  DEFMODULES=0
  DEFPACKAGES=0
  PACKAGES=""
  MODULES=""
  HASMODULES=0
  HASPACKAGES=0
  INHALE_FLAG="-m"

  while [ "$#" -gt 0 ]
  do
     case $1 in
     -c)
        INHALE_FLAG="-c"
        INHALE=1
        ;;
     -i)
        INHALE=1
        ;;
     -m*)
        DEFMODULES=1
        DEFPACKAGES=0
        HASMODULES=1;
        case $1 in 
        -m)
           shift
           MODULES="$1"
           ;;
        *)
           MODULES=`echo $1 | sed -e 's/^-m//'`
           ;;
        esac
        ;;
     -p*)
        DEFMODULES=0
        DEFPACKAGES=1
        HASPACKAGES=1;
        case $1 in 
        -p)
           shift
           PACKAGES="$1"
           ;;
        *)
           PACKAGES=`echo $1 | sed -e 's/^-p//'`
           ;;
        esac
        ;;
     *)
        if [ $DEFPACKAGES -gt 0 ] ; then
           PACKAGES="$PACKAGES $1"
        elif [ $DEFMODULES -gt 0 ] ; then
           MODULES="$MODULES $1"
        else
           exec 1>&2
           echo ""
           echo "Usage: testsuite [-i] [-m module names] [-p package names]"
           exit 1
        fi
        ;;
     esac

     [ "$2" = "" ] && break

     shift
  done


# Report the amount of free space available for the update.
  echo ""
  echo "testsuite: Available space in the /tmp and $AIPSROOT partitions"
  df /tmp $AIPSROOT

# Define major AIPS++ directories.
  AIPSLAVE="$AIPSROOT/slave"
  AIPSCODE="$AIPSROOT/code"


# Get the user name.
  USERNAME=$LOGNAME@

  echo "modules=$MODULES  packages=$PACKAGES "

  if [ $INHALE -gt 0 ] ; then
    echo ""
    echo "testsuite: inhale started"
    echo ""
    inhale $INHALE_FLAG > $AIPSROOT/inhale.log 2>&1
    echo ""
    echo "testsuite: runtests started"
    echo ""
    runtests
  else
     if [ $HASPACKAGES -gt 0 -o $HASMODULES -gt 0 ] ; then
       if [ $HASPACKAGES -eq 0 ] ; then
          PACKAGES="aips"
       fi
       for PACK in $PACKAGES
       do
         if [ $HASMODULES -gt 0 ] ; then
            for MODULE in $MODULES
            do
               if [ -d $AIPSCODE/$PACK/implement/$MODULE ] ; then
                  cd $AIPSCODE/$PACK/implement/$MODULE
                  gmake runtests
               else
                  echo "testsuite: package $PACK has no module name $MODULE."
               fi
            done
         else 
           if [ -d $AIPSCODE/$PACK ] ; then
               cd $AIPSCODE/$PACK
               gmake runtests
           else
               echo "testsuite: no package named $PACK"
           fi
         fi
       done
     else
        runtests
     fi
  fi

  echo ""
  echo "testsuite: Execution terminated at"
  echo "testsuite: `adate`"
  echo "testsuite: `adate -l`"
  exit 0
