#!/bin/sh 
#   !/bin/bash
#
deflist="qa-suite Timings semi_emp"
deflist="qa-suite Timings semi_emp MMFF94 MMFF94_dative MMFF94_ext props"
#deflist=MMFF94_testsuite
#deflist=Timings
#
verPreFix=QA
verPostFix=""
#
###########
# make sure envirionment is initialized
#  ... needed if called from inside a crontab
#
if [ "$MYSRC" = "" ] 
then
  . $HOME/.bashrc 
  cd $HOME/QA_DIR             # default -b option
fi
srcdir=$MYSRC/Benchmarks    # default -s option
#
#
###########
# handle arguments
# 
origArgs="$@"
skipBuild="NO"
testOptions="YES"
newDefList=""
while [ "$1" != "" -a "$testOptions" = "YES" ]
do
 case "$1" in 
  "-P") 
    shift; 
    verPreFix="${1}"
    shift;
    if [ "$verPreFix" = "" ]
    then
      echo missing prefix argument for -P 
    fi
    ;;
  "-p") 
    shift; 
    verPostFix="_"$1
    shift;
    if [ "$verPostFix" = "_" ]
    then
      echo missing postfix argument for -p 
    fi
    ;;
  nobuild|noBuild|skipBuild|skipbuild|-n)
    skipBuild="YES"
    shift
    ;;
  sparloc|-b)
    shift;
    BINDIRPATH="${1}"/; shift
    ;;
  -a)
    shift;
    ANDYBINS="${1}"/; shift
    ;;
  -s)
    shift; srcdir="$1" ; shift
    ;;
  -w) 
    shift;  
    if [ ! -d $1 ] ; then
      echo $1 is not a directory
      exit 5
    fi
    cd $1 ; shift
    ;;
  -x)
    shift;
    newDefList="$newDefList $1"
    shift
    ;;

  -c)
    shift
    SUB_DIR=$1;  export SUB_DIR
    SPAR_CPUDIR=$1; export SPAR_CPUDIR
    SPAR_CPUD_tmp=$SPAR_CPUDIR  # this is the the above two should be deleted
    shift
    ;;

  -e)
    shift
    if [ "$1" != "." -a "$1" != "-" ] ; then
	sparScript="${1}"
    fi
    shift
    ;;

  -h|-help|--help)
    echo "run-test [test-options] [cl-options]"
    echo "   test-options: "
    echo "      -p <postfix>: append <postifix> to files/directories"
    echo "      -P <prefix> : prepend <prefix> to main result directory."
    echo "      -n          : Skip build process"
    echo "      -b <dir>    : binary location.  (SPAR_x xfor backends)"
    echo "      -a <dir>    : location of submission libraries (SPAR_SUBMIT)"
    echo '      -w <dir>    : work/results dir [ $HOME/QA_DIR ]'
    echo '      -s <dir>    : source directory [ $MYSRC/Benchmarks ]'
    echo "      -x <dir>    : templates [ $deflist ]"
    echo "      -c <sdir>   : processor sub-dir, i.e. G5 or P4"
    echo "      -e <spartan>: spartan script, '-e .' same as default   [ /var/spartan/spartan ]"
    echo "      -h | --help : this help"
    echo ""
    echo "    Common calls"
    echo " > setenv SP /release_ia32/bin/spartan04.122_22"
    echo ' > $SP/sp_run_test -n -b $SP -w /scratch/ -x Timings -p tryTimes'
    echo ' > $SP/sp_run_test -n -e spartan06 -b $SP -c P4 -w /Bench'
    exit 0
    ;;
  *)
    testOptions="NO" 
    ;;
  esac
done
while [ "$1" != "" ]
do
    newDefList="$newDefList $1"
    shift
done
if [ "${newDefList}" != "" ] ; then
  deflist="${newDefList}"
fi
#
###########
# first compile latest version
# 
MDIR=`pwd`
if [ "$skipBuild" = "NO" ] ; then
  if [ ! -d "$MYSRC"/CL  ] ; then
    echo MYSRC is not correctly defined: $MYSRC
    exit 101
  fi
  cd $MYSRC

  ./updateAll.sh release
  ret=$?
  if [ "$ret" -ne "0" ] ; then
    echo "  "
    echo "!!! Error compiling Spartan"
    exit 99
  fi
fi
#################
# determine BINDIRPATH using cl
#
#if [ "$BINDIRPATH" = "" ] ; then
#  cd $MYSRC
#  cd mechanics
#  BINDIRPATH=`$CL_ROOT/cl release $@ showvars | grep FinalBinPath | awk '{print $3}'`
#fi

#
###########
# Copy/create the test suite
#
cd $MDIR  # return back to root directory
#
ds=`date +"%Y-%m-%d+%H"`
curlist=${verPreFix}"_"${ds}${verPostFix}
if [ -d ${curlist}_dir ]
then
  i=1
  while [ $i -le 100 -a -d ${curlist}_${i}_dir ]
  do
    i=`expr $i + 1`
  done
  curlist=${curlist}_${i}
fi

mkdir ${curlist}_dir
stat=$?
if [ "$stat" != "0" ] ; then
  echo ; echo problem creating directory "["`pwd`"]" ${curlist}
  exit $stat
fi
touch ${curlist}_dir/_spartandir
#
###########
# start standard output 
# 
echo -e "\n\n"                                                    | tee -a ${curlist}_dir/LOG 
echo    "#######################################################" | tee -a ${curlist}_dir/LOG 
echo    "# Regression Tester"                                     | tee -a ${curlist}_dir/LOG 
echo    "#  "`date`                                               | tee -a ${curlist}_dir/LOG 
echo    "#  "`uname -a`                                           | tee -a ${curlist}_dir/LOG 
echo    "# destination : ${curlist}_dir  "                        | tee -a ${curlist}_dir/LOG 
echo    "# executables : $BINDIRPATH     "                        | tee -a ${curlist}_dir/LOG 
echo    "# standards   : $deflist        "                        | tee -a ${curlist}_dir/LOG 
echo    "#######################################################" | tee -a ${curlist}_dir/LOG 
echo    "# "                                                      | tee -a ${curlist}_dir/LOG 
echo    "# original Command:"                                     | tee -a ${curlist}_dir/LOG 
echo    "# " $0                                                   | tee -a ${curlist}_dir/LOG 
echo    "#     ${origArgs}"                                       | tee -a ${curlist}_dir/LOG 
echo -e "#\n\n"                                                   | tee -a ${curlist}_dir/LOG 

#
###########
# Copy/create the test suite
#
echo "Creating " `pwd`"/"${curlist}_dir  | tee -a ${curlist}_dir/LOG

for dlist1 in $deflist
do
  dlist="$dlist1"
  if [ ! -d "$dlist" ] ; then
    if [ -d ${srcdir}/$dlist ] ; then
      dlist=${srcdir}/$dlist 
    else
      echo MYDIR is not correctly defined: $MYDIR/$dlist or $srcdir/$dlist
      echo srcdir = $srcdir
      echo dlist  = $dlist
      echo deflist= $deflist
      exit 102
    fi
  fi

  echo "Reading " ${dlist}               | tee -a ${curlist}_dir/LOG
  if [ -r ${dlist}/sheet ] ; then  # copy last sheet file
    cp ${dlist}/sheet ${curlist}_dir/sheet
  fi
  for dd_spar in ${dlist}/*/_spartan
  do
    subdir=`dirname  "${dd_spar}"`
    basdir=`basename "${subdir}"`
    cp -rp "${subdir}" "${curlist}_dir/${basdir}"
  done
  
done

echo "Running" | tee -a ${curlist}_dir/LOG
#
###########
# Run the benchmark suite
#
###echo $MYSRC/HOME/spartan_d -d "$BINDIRPATH" -x ${curlist}_dir | tee -a ${curlist}_dir/LOG
###     $MYSRC/HOME/spartan_d -d "$BINDIRPATH" -x ${curlist}_dir
if [ "$BINDIRPATH" != "" ] ; then  ## these are the variables i(pk) control
  #xport SPAR_USER_ROOT="${BINDIRPATH}"
  export SPAR_USER_ABINITIO="${BINDIRPATH}"
  export SPAR_USER_SEMIEMP="${BINDIRPATH}"
  export SPAR_USER_MECHANICS="${BINDIRPATH}"
  export SPAR_USER_PROPERTY="${BINDIRPATH}"
  export SPAR_USER_GRAPHICS="${BINDIRPATH}"
  export SPAR_USER_CONFORMER="${BINDIRPATH}"
  export SPAR_USER_GAUSSIAN="${BINDIRPATH}"
  export SPAR_USER_QCHEM="${BINDIRPATH}"
  export SPAR_USER_QMMM="${BINDIRPATH}"
  export SPAR_USER_STARTBACKEND="${BINDIRPATH}"
  export SPAR_USER_OVERRIDE="YES"
fi
if [ "$SPAR_CPUD_tmp" != "" ] ; then
  export SPAR_USER_CPUDIR=${SPAR_CPUD_tmp}
  export SPAR_USER_OVERRIDE="YES"
fi 
if [ "$ANDYBINS" != "" ] ; then
  export SPAR_USER_BUILDER="${ANDYBINS}"
  export SPAR_USER_INSTALL="${ANDYBINS}"
  export SPAR_USER_SUBMIT="${ANDYBINS}"
  export SPAR_USER_ARCH="${ANDYBINS}"
  export SPAR_USER_OVERRIDE="YES"
fi
# 
# use the -d to override everything!!
#
SPARTAN_SCRIPT=/var/spartan/spartan
SPARTAN_SCRIPT=spartan
SPARTAN_SCRIPT=spartanLoc.sh
if [ "$sparScript" != "" ] ; then
  SPARTAN_SCRIPT="${sparScript}"
fi
#
Q_VARS="--internal-default-queue -v 0"
Q_VARS="-t 0 -v 0 "
pwd
echo  \
$SPARTAN_SCRIPT  -x "${curlist}"_dir ${Q_VARS} | tee -a "${curlist}"_dir/LOG
$SPARTAN_SCRIPT  -x "${curlist}"_dir ${Q_VARS} | tee -a "${curlist}"_dir/LOG
stat=$?
#    just to echo a time stamp
date                                           | tee -a "${curlist}"_dir/LOG
echo "Spartan Return Status = " $stat          | tee -a "${curlist}"_dir/LOG
#
###########
# Analyze for errors
#
if [ -x ./sp_test_list ] ; then
    tt="./sp_test_list" 
elif [ -x "$BINDIRPATH"/sp_test_list ] ; then
    tt="$BINDIRPATH"/sp_test_list
elif [ -x $MYSRC/Benchmarks/sp_test_list ] ; then
    tt=$MYSRC/Benchmarks/sp_test_list
else
    echo ""; 
    echo "!!!! Can't find sp_test_list"
    exit 5
fi
echo -e "\n\n"                                    | tee -a ${curlist}_dir/LOG
$tt    ${curlist}_dir | tee ${curlist}_dir/errlog | tee -a ${curlist}_dir/LOG
echo "..."
$tt -t ${curlist}_dir > ${curlist}_dir/time.LOG
echo
#
###########
# Alert me if there are errors
#
#errcnt=`cat ${curlist}_dir/errlog | wc -l` 
errcnt=`egrep '^=====:' ${curlist}_dir/errlog | awk '{print $4}'`

if [ x"${errcnt}"x = "xx" -o "$errcnt" -ge 1 ] ; then
   echo "${errcnt}" errors
#  mail -s 'Spartan errors' phil@wavefun.com < ${curlist}_errlog
fi
#
###########
# All done
#
echo compressing backup copy of ${curlist}_dir
tar -czf ${curlist}.tgz ${curlist}_dir

if [ "${errcnt}" != "0" ] ; then
  exit 1
fi
exit 0

#
