#!/bin/sh

# In order to use this script, you must be logged to anonymous cvs:
# $ cvs login
# (password: cvsanon)

# choose a directory ROOT  in which to perform the test
# the code is in     PWROOT=$ROOT/espresso
# temporary files in TMPDIR=$ROOT/tmp

ROOT=/scratch/daily_test/
MAILTO=q-e-commits@qe-forge.org

# choose setting for the compiler you want to use
export CONFIG_OPTS="--disable-parallel"
# for g95 + MKL:
# export CONFIG_OPTS="--disable-parallel F90=g95"
# for ifort:
. /opt/intel/fce/10.1.008/bin/ifortvars.sh
. /opt/intel/mkl/10.0.1.014/tools/environment/mklvarsem64t.sh
export OMP_NUM_THREADS=1
# in order to use fortran lapack instead of mkl
#export CONFIG_OPTS="--disable-parallel LAPACK_LIBS=../flib/lapack.a"
#
# choose if you want to run only pw.x tests (fast=1) or all examples (fast=0)
fast=1
#
# choose if you want to update an existing cvs (update=1)
#                 or to download a new one     (update=0)
update=1
######################################################################
export CVSROOT=:pserver:anonymous@scm.qe-forge.org:/cvsroot/q-e
PWROOT=$ROOT/espresso
TMPDIR=$ROOT/tmp
OUT=$PWROOT/daily.out
/bin/rm $OUT
touch $OUT

if ! test -d $PWROOT; then
    mkdir -p $PWROOT
    update=0
elif ! test -d $PWROOT/CVS; then
    update=0
fi
if ! test -d $TMPDIR; then
    mkdir -p $TMPDIR
fi

if test  "$update" = "0" ; then
   cd $ROOT
   /bin/rm -r espresso
   cvs co espresso >& cvs.log
   cd $PWROOT
   mv ../cvs.log .
else
   cd $PWROOT
   cvs update -d >& cvs.log
fi
# conflicts=`grep -c '^C ' cvs.log`
# do not consider conflicts in examples: some of them may be bogus
conflicts=`grep -v  '^C examples/' cvs.log | grep -c '^C'`
if test "$conflicts" != "0" ; then
   echo "#" >> $OUT
   echo "# CONFLICTS IN CVS CHECKOUT " >> $OUT
   echo "#" >> $OUT
   grep '^C ' cvs.log >> $OUT
   mail -s "RESULTS OF DAILY QE TEST" $MAILTO < $OUT
   exit 1
fi
# do not re-run everything if only examples have changed
changes=` grep -e '^[UP] ' cvs.log | grep -c -v -e '^[UP] examples' -e '^[UP] Doc' -e'^[UP] doc-def' `

if test  "$changes" = "0" ; then
   #
   # no changes to cvs, do nothing
   #
   exit 0
else
   echo "#" >> $OUT
   echo "# CHECKOUT ESPRESSO " >> $OUT
   echo "#" >> $OUT
   grep -e '^U ' -e'^P ' -e '^M ' cvs.log >> $OUT
fi
echo "#" >> $OUT
echo "# CONFIGURE " >> $OUT
echo "#" >> $OUT
./configure $CONFIG_OPTS >& configure.log
tail -1 config.log >> $OUT

echo "#" >> $OUT
echo "# MAKE ALL " >> $OUT
echo "#" >> $OUT
make all >& make.log
errors=`grep -c -e Error make.log`
if test  "$errors" = "0" ; then
   echo "make all: success" >> $OUT
else
   echo "make all: failure! Last 50 lines from 'make all':" >> $OUT
   echo "-------------------------------------------------" >> $OUT
   tail -50 make.log >>  $OUT
   mail -s "RESULTS OF DAILY QE TEST" $MAILTO < $OUT
   exit $errors
fi
#
if test "$fast" = "0" ; then
#
   cd examples
   ./make_clean
   echo "#" >> $OUT
   echo "# RUNNING ALL EXAMPLES " >> $OUT
   echo "#" >> $OUT
   mv environment_variables environment_variables.orig
   cat > environment_variables << EOF
BIN_DIR=$PWROOT/bin
PSEUDO_DIR=$PWROOT/pseudo
TMP_DIR=$TMPDIR
PARA_PREFIX=
PARA_POSTFIX=
EOF
   #
   echo > run.log
   ###./run_all_examples >& run.log
   #
   # TESTING ALL EXAMPLES
   #
   example_list=`/bin/ls -d example??`
   for example in $example_list
   do

     cd $PWROOT/examples/$example

     if test -x ./run_example ; then
       if test  "$example" = "example21" ; then
         # the argument "64" to run_example is used in example 21
         ./run_example 64 >& run.log
       else
         # all other cases
         ./run_example >& run.log
       fi
       # get the exit status of the script run_example
       exit_status=$?
       N=`echo $example | sed 's/example//'`
       if test $exit_status -eq 0; then
         echo "example $N finished OK" >> $OUT
       else
         echo "example $N ERROR EXIT (exit status = $exit_status)" >> $OUT
       fi

       cat run.log >> ../run.log
       /bin/rm run.log
     fi

   done

   for example in $example_list
   do

     cd $PWROOT/examples/$example

     N=`echo $example | sed 's/example//'`

     if /bin/grep "HIGH VERBOSITY EXAMPLE" < run_example > /dev/null ; then
       # high verbosity example: do not check
       echo "#" >> $OUT
       echo "# CHECK OF EXAMPLE # " $N " SKIPPED">> $OUT
       echo "#" >> $OUT
     else
       # low-verbosity example: do a full check
       echo "#" >> $OUT
       echo "# CHECKING EXAMPLE # " $N >> $OUT
       echo "#" >> $OUT
       ../check_example ./ >> $OUT
     fi
   done

   cd $PWROOT/examples
i
   mv environment_variables.orig environment_variables
#
else
#
   cd tests
   ./check-pw.x.j >> $OUT
   cd ../cptests
   ./check-cp.x.j >> $OUT
#
fi

cd $PWROOT/TDDFPT/Examples
make clean
make small_test >> $OUT 2>&1
#
mail -s "RESULTS OF DAILY QE TEST" $MAILTO  -- -f giannozz@democritos.it < $OUT
