#!/bin/bash
##SBATCH --nodes=2
#SBATCH --ntasks=8              # Nombre total de processus MPI
#SBATCH --ntasks-per-node=16
##SBATCH --cpus-per-task=1
#SBATCH --hint=nomultithread
#SBATCH -t 00:10:00              # Temps elapsed maximum d'exécution
#SBATCH -o output.out     # Nom du fichier de sortie
#SBATCH -e error.err     # Nom du fichier d'erreur
##SBATCH -p cpu_port             # Nom de la partition d'exécution
##SBATCH -A sos@cpu
##SBATCH --distribution=arbitrary
#SBATCH --account=psl@cpu


cd ${SLURM_SUBMIT_DIR}
module unload intel-all hdf5 netcdf netcdf-cxx netcdf-fortran
module load intel-all/19.0.4
module load hdf5/1.10.5/intel-19.0.4-mpi
module load netcdf/4.7.2/intel-19.0.4-mpi
module load netcdf-cxx/4.2/intel-19.0.4-mpi
module load netcdf-fortran/4.5.2/intel-19.0.4-mpi

#module purge
#module load intel-all/19.0.4
#module load netcdf/4.7.0/intel-19.0.4-mpi
#module load netcdf-cxx/4.2/intel-19.0.4-mpi
#module load netcdf-fortran/4.4.5/intel-19.0.4-mpi
#module load gcc/6.5.0/gcc-4.8.5
module load cmake
module load cdo
module load subversion
#module load arm-forge

ulimit -c 0
#export ALLINEA_NO_TIMEOUT=1

#ddt --connect srun --mpi=pmi2 -K1 -m block:block --multi-prog ./mpmd_srun.conf
#srun --mpi=pmi2 -K1 --multi-prog -m cyclic ./mpmd_srun.conf
export machine_name=jeanzay
export xios_dir=/gpfswork/rech/psl/rpsl954/xios/dev_trunk_omp
export build_dir=build_prod

# python user_config.py

rm -f plein_report.txt
echo -n "#revision " >> plein_report.txt
svn info --show-item revision .. >> plein_report.txt
# echo -n "#url " >> plein_report.txt
# svn info --show-item url .. >> plein_report.txt
echo -n "#relurl " >> plein_report.txt
svn info --show-item relative-url .. >> plein_report.txt
echo -n "#machine " >> plein_report.txt
echo $machine_name >> plein_report.txt
echo -n "#build_dir " >> plein_report.txt
echo $build_dir >> plein_report.txt

export arch_dir=$(readlink ../$build_dir/arch.fcm .. 2>&1)
echo $arch_dir
my_arch=$(echo $arch_dir | sed 's|.*/arch-||' | sed 's|\(.*\).fcm.*|\1|' 2>&1)
echo $my_arch


echo -n "#arch " >> plein_report.txt
echo $my_arch >> plein_report.txt

export xios_revision=$(svn info --show-item revision .. 2>&1)
echo $xios_revision

rm -f setup.sh
touch setup.sh
>setup.sh
for i in $(ls -d test_*/)
do
  cp setup.py ${i%%/}      
  cp run_sub_test.sh ${i%%/}      
  echo "bash -c \"cd " ${i%%/} " && python setup.py \" ">> setup.sh
  echo "echo \"setup.py called from " ${i%%/} "\"">> setup.sh
done

export output=$(python user_config.py 2>&1 >/dev/null)

if [ "$output" -ne 0 ]
then
	echo "user_config.py failed"
	exit
else
	echo "user_config.py OK"
fi


cmake .
ctest -V 
#ctest --output-on-failure
make report

#echo "Generic testcase report" | mailx -s "report" -a report.html yushan.wang@lsce.ipsl.fr

rm -f test_*/setup.py
rm -f test_*/run_sub_test.sh
rm -f test_*/run_test_*.py
rm -f test_*/CMakeLists.txt
rm -f test_*/context_grid_dynamico.xml
rm -f test_*/dynamico_grid.nc
rm -f test_*/default_param.pyc
rm -f test_*/user_param.pyc
rm -f test_*/user_param.py.*


#srun --mpi=pmi2 -K1 --multi-prog -m cyclic ./srun.conf
#srun --mpi=pmi2 ../bin/test_client.exe

#cp plein_report.txt ../Web/plein_report_$xios_revision.txt
