#!/bin/bash
# $Id: check_version.sh 6173 2026-04-17 14:51:07Z fairhead $

#set -vx 

# AFAIRE
# On pourrait gerer le justcheck en creant un repertoire
# avec
#  if recheck
#   resubench=./RESUBENCH$$
#  else
#   resubench=$RESU_D/$datelmdz$rel_svn
#On pourrait ainsi utiliser une fonction independante qui genere les messages
# comme check_version_message



#########################################################################
# Verification de a convergence du modele par rapport aux versions
# precedentes
# + 1+1=2
#########################################################################

version_ref=latest
resolution=48x36x39
compilo=gfortran
parallel=mpi_omp
# Option pour le debug du script check_version lui meme qui consiste a ne
# pars reexecuter l'installation et les simulations mais a simplement
# refaire les diff sur des simulations existantes.
justcheck=0 # just compare results not writing of results in RESUBENCH
tmpdir=/tmp/`whoami` ; mkdir -p $tmpdir
tmpdir=/home/lmdz/tmp
rel_svn=""
local_d=`pwd`

install=1
rad=rrtm
check_1D=1
check_SP=1
check_1p1=1
check_Debug=1
check_isotopes=1
check_parallel=1
check_compile=1
check_ini=1
mail_address=lmdz-commit@listes.lmd.ipsl.fr
MODEL=""
branche=trunk

#########################################################################
# Pour aller chercher des fichiers par wget si pas sur le cpte lmdz
#########################################################################

if [ `whoami` = lmdz ] ; then
   get="ln -sf /u/lmdz/WWW"
else
   get="wget https://www.lmd.jussieu.fr/~lmdz"
fi

#########################################################################
# Options du script
#########################################################################

while (($# > 0))
  do
  case $1 in
      "-h") cat <<fin
check_version.sh [-h] version [-latest version_ref]
version is the name of the version of LMDZ to be checked modipsl.version.tar.gz
version_ref is the version to be compared with.
Default "latest".
options:
  -c compiler : sets the compiler to use
  -justcheck  : just check results, do not overwrite anything
  -r svn : revision 
  -latest latest : latest is the date of the last version tested
  -model_dir : directory where tun run the bench if LMDZ is already installed 
fin
          exit;;

      "-c") compilo="$2" ; shift ; shift ;;
      "-justcheck") justcheck=1 ; shift ;;
      "-r") rel_svn="$2" ; shift ; shift ;;
      "-latest") version_ref="$2" ; shift ; shift ;;
      "-model_dir") MODEL="$2" ; shift ; shift ;;
      *) version="$1" ; shift ;;
   esac
done

################################################################################
# Definition des noms des repertoires à comparer
################################################################################

# branche=`echo $version | cut -d. -f2`
if [ "$MODEL" = "" ] ; then
   MODEL=$tmpdir/LMDZ$version$rel_svn
   branche=`echo $version |cut -c 10-`
   datelmdz=`echo $version | cut -d. -f1`
else
   #if [ $install = 1 ] ; then echo Use model_dir only if the model is already installed ; exit ; fi
   install=0
   if [ "`echo $MODEL | cut -c1`" != "/" ] ; then MODEL=`pwd`/$MODEL ; fi # MODEL transformed in absolute path if not
   datelmdz=`date +%Y%m%d`$$
fi

if [ -d $MODEL ] ; then
   mv $MODEL ${MODEL}_$$
fi

echo version $version $branche $datelmdz
RESU_D=~/WWW/RESUBENCH/$branche/$compilo
if [ $justcheck = 0 ] ; then
   resubench=$RESU_D/$datelmdz$rel_svn
   if [ -d $resubench ] ; then mv $resubench $resubench$$ ; fi
   mkdir -p $resubench
fi
latest=$RESU_D/$version_ref
echo $latest 

# recuperation de la version a laquelle on compare pour le message final :
comparea=`ls -ld $RESU_D/$version_ref | awk -F/ ' { print $NF } '`
echo comparea $comparea

################################################################################
echo 0. Installation du modele
################################################################################

# Edition de install.sh, install.sh avec bench 48x36x19
if [ $install = 1 ] ; then
   cd $tmpdir
   if [ "$rel_svn" = "" ] ; then opt_svn="" ; else opt_svn="-r $rel_svn" ; fi
#   \rm -f install_lmdz.sh ; $get/pub/install_lmdz.sh ; chmod +x install_lmdz.sh
# Le temps de confirmer la bascule
#   \rm -f install_lmdz.sh ; $get/pub/script_install/install_lmdz_old.sh ; \mv install_lmdz_old.sh install_lmdz.sh ; chmod +x install_lmdz.sh
   \rm -f install_lmdz.sh ; $get/BOL/script_install/install_lmdz_old.sh ; \mv install_lmdz_old.sh install_lmdz.sh ; chmod +x install_lmdz.sh
   # On install sans les bench pour que les benchs soient faits à la main
   # ./install_lmdz.sh -v $version $opt_svn -d $resolution -SCM -parallel $parallel -veget CMIP6
     ./install_lmdz.sh -unstable -v $version $opt_svn -parallel $parallel -xios -icolmdz -bench 0 -noclean
fi





if [ $justcheck = 0 ] ; then
    latest=$RESU_D/latest
    \rm -f $latest
    ln -sf  $resubench $latest
fi


cd $LMDZdir/..
LMDZ=`\ls -d LMD*` #Nom du modele LMDZ sur modeles/ : LMDZ4, LMDZ5, LMDZ

svn upgrade # Il faut mettre à jour le svn si la version sur 
            # laquelle le checkout a ete fait est plus ancienne
            # ce qu'on souhaite par ailleurs pour pouvoir ensuite
            # commettre depuis une version ancienne de svn (comme celles
            # des SL du réseau local
svnrel=`svn info $LMDZ | grep 'Changed Rev' | head -1 | awk ' { print $4 } '`
#FH 20160822
if [ "$svnrel" = "" ] ; then
  svnrel=`svn info $LMDZ | grep vision | head -1 | awk ' { print $2 } '`
fi

ccc=$converge$convergeNP$convergeD$converge1D$convergeI
if [ "$ccc" = "SNDUI" ] ; then ccc="OK " 
	elif [ "$ccc" = "SNDUx" ] ; then
		ccc="OKx"
fi



########################################################################
# Ectiture du message de bilan
########################################################################

cd $local_d

echo $version'		'$svnrel'	'$bench'	'$ccc '	'$unpun'	'$init'   (ref:' $comparea ')' > tmp.resu
cat > tmp.message <<eod
disponible sur
https://www.lmd.jussieu.fr/~lmdz/Distrib/modipsl.$version.tar.gz

      Test local LMD network, gfortran, 48x36x19
      ==========================================

install version         SVN     Bench   Conv.   1+1=2   Init
                                run     Nnum.      &
                                        /prev.  mpiXomp

eod
cat tmp.resu >> tmp.message
cat >> tmp.message <<eod


                                        ||
                                        \/

S/s/-: 3D standard physics  Converging/runing/not runing
N/n/-: -  new        -                  -
D/d/-: new with debug                   -
U/u/-: unicolonne                       -
I/i/x/-: isotope            Converging/runing/run failed /compiled failed
noc: runs but no convergence
OK <=> SNDUI

Description of LMDZ quality control runs can be found here:

https://lmdz.lmd.jussieu.fr/le-coin-des-developpeurs/controle-qualite

with a link to the LMDZPedia HowTo on how to debug it

https://lmdz-forge.lmd.jussieu.fr/mediawiki/LMDZPedia/index.php/HowTo:_debug_the_quality_control

eod

cat tmp.message

if [ $mail_address != "" ] &&  [ $justcheck = 0 ] ; then
   ssh lmdz@django "mail -s 'Nouvelle version pour install_lmdz.sh' $mail_address < "$local_d"/tmp.message"
#   ssh lmdz@lmdz-forge "mail -s 'Nouvelle version pour install_lmdz.sh' $mail_address < "$local_d"/tmp.message"
fi

#
# clean up of /tmp/lmdz if everything went smoothly
#
grep -q 'OK	OK 	OK2	OK' tmp.resu
RET=$?
if [ ${RET} -eq 0 ] ; then
  echo "Quality control checks out for version $version"
  echo "We cleanup $tmpdir/LMDZ$version"
  echo "\rm -rf $tmpdir/LMDZ$version"
  \rm -rf $tmpdir/LMDZ$version
fi
grep -q 'OK	OKx 	OK2	OK' tmp.resu
RET=$?
if [ ${RET} -eq 0 ] ; then
  echo "Quality control checks out for version $version"
  echo "We cleanup $tmpdir/LMDZ$version"
  echo "\rm -rf $tmpdir/LMDZ$version"
  \rm -rf $tmpdir/LMDZ$version
fi
grep -q 'OK	SNDU- 	OK2	OK' tmp.resu
RET=$?
if [ ${RET} -eq 0 ] ; then
  echo "Quality control checks out for version $version"
  echo "We cleanup $tmpdir/LMDZ$version"
  echo "\rm -rf $tmpdir/LMDZ$version"
  \rm -rf $tmpdir/LMDZ$version
fi
