#!/bin/bash
#
# Lancement d'un install_lmdz.sh immédiatement apres un commit
# pour tester la compilation et l'execution. Retour d'un code erreur adequat
# au serveur svn pour notifier qui de droit

set -vx

REV="$1"

TMP_OUT=/u/lmdz/WWW/Distrib/OUT
TMP_D=/tmp/lmdz/test_rev_${REV}_$$ ; mkdir -p $TMP_D

resolution=48x36x39 
physiq=NPv6.0.14splith
# Changed on 15/03/2023 r4473
physiq=NPv6.3

cd $TMP_D
cp /u/lmdz/WWW/pub/install_lmdz.sh .
#chmod 755 /u/lmdz/WWW/pub/install_lmdz.sh
chmod +x install_lmdz.sh
#./install_lmdz.sh -SCM -unstable -debug -d $resolution -r $REV -physiq $physiq > ${TMP_OUT}/post-commit-lmdz-cq-${REV}_$$ 2>&1
./install_lmdz.sh -SCM -unstable -debug -d $resolution -r $REV -netcdf /u/lmdz/Netcdf/lmdz-cq_gcc_8.3.0/ -rad oldrad -parallel mpi_omp  -physiq $physiq \
		   > ${TMP_OUT}/post-commit-lmdz-cq-${REV}_$$ 2>&1
#
# menage: si ca a marche on efface le repertoire dans /tmp/lmdz
#
grep 'Everything is cool' ${TMP_OUT}/post-commit-lmdz-cq-${REV}_$$
RET=$?
# checking 1D results
cd LMDZ*/1D/EXEC/6AL79/
listecas="ARMCU/REF RICO/SHORT SANDU/REF"
((ret1d=0))
for cas in $listecas ; do
    ((ret1d=$ret1d*10))
    gunzip -dc "$cas"/listing.gz |grep -i cool
    retcas=$?
    ((ret1d=$ret1d+$retcas))
done
# checking isotope compilation
cd $TMP_D
cd LMDZ*/modipsl/modeles/LMDZ/
./makelmdz_fcm -debug -arch local -j 8 -rad rrtm -d $resolution -v false -p lmdiso -isotopes true -isoverif true gcm  > ./compile_iso.out 2>&1
ls bin/*iso*
retiso=$?

if [ ${RET} -eq 0 ] && [ ${ret1d} -eq 0 ] && [ ${retiso} -eq 0 ] ; then
  echo "Everything is cool for revision ${REV}" >> ${TMP_OUT}/post-commit-lmdz-cq-${REV}_$$
  echo "Cleaning up /tmp/lmdz/test_rev_${REV}_$$" >> ${TMP_OUT}/post-commit-lmdz-cq-${REV}_$$
  \rm -rf /tmp/lmdz/test_rev_${REV}_$$
fi
#
# sorties retour nécessaires pour envoyer le message a la liste
#
echo "Sortie dans" ${TMP_OUT}/post-commit-lmdz-cq-${REV}_$$
if [ ${RET} -eq 0 ] ; then
    echo "Bench 3D ok"
fi
if [ ${ret1d} -eq 0 ] ; then
    echo "Bench 1D ok"
fi
if [ ${retiso} -eq 0 ] ; then
    echo "Compilation iso ok"
fi

#grep 'Everything is cool' ${TMP_OUT}/post-commit-lmdz-cq-${REV}_$$
exit
