#!/bin/bash

set -eu  # exit on most (not all!) error codes, and error on unset variables
set -xv

######################################################################
# Author : Frédéric Hourdin (LMDZ team)
# Setting up the LMDZ SCM for HighTune explorer
# 2019/02/21
######################################################################

ROOT=$(pwd)
echo "ROOT: $ROOT"
install_dir=$(realpath $ROOT/..)

rad="rrtm"
rad="oldrad" # Much faster compilation. Good for tests

echo '#####################################################################'
echo  "Choosing the LMDZ version"
echo  "List of available versions on "
echo  "http://www.lmd.jussieu.fr/~lmdz/pub/src/Readme"
echo  'version can be "trunk" or something like "20210320.trunk"'
echo 'A particular svn release could be chosen as well (experts only)'
echo '#####################################################################'

# Definition of the LMDZ version. It is the option passed to install_lmdz.sh

version='-v 20221201.trunk'
version='-unstable -v 20230210.trunk'
version='-v 20230412.trunk'
version='-v 20230626.trunk'
version='-unstable -v 20230920.trunk -r 4706' # Solving a bug on arm_cu
version='-v 20231022.trunk'
version='-v 20240508.trunk'
version='-unstable -v 20240722.trunk' # Solving a bug on arm_cu
version='-unstable -v 20240923.trunk' # Pre testing 2024/09
version='-unstable -v 20241121.trunk' # Pre testing 2024/11
version='-v 20250327.trunk'           # Testing 2025/04/04

LMDZ=LMDZ$(echo "$version" | sed -e 's/-v//g' -e 's/-unstable//' -e 's/-r/r/' -e 's/ //g')

echo '#####################################################################'
echo ' Checking if model already exists'
echo '#####################################################################'

LMDZroot=""
if [[ -d $ROOT/../../$LMDZ ]] ; then
   LMDZroot=$ROOT/../..
else
   # By default, LMDZ is installed next to HighTune
   LMDZroot=$ROOT/..
fi
LMDZroot=$(readlink -f "$LMDZroot")
LMDZdir=$LMDZroot/$LMDZ
echo "LMDZdir $LMDZdir"

echo "export LMDZdir=$LMDZdir" > $install_dir/env_LMDZ.profile
echo "export rad=$rad" >> $install_dir/env_LMDZ.profile

hostname=$(hostname) 
case ${hostname:0:6} in
  "beleno") echo "\
# this config is designed for belenos
# module purge tries to unalias condam but the alias does not exist so it raises an error
# so I create a false condam alias so that it can be unaliased...
alias condam="ls"
module purge
module load gcc/9.2.0
module load cmake/3.24.1
module load openmpi/gnu/ilp64/4.0.5.1 # this is a prereq for hdf5
module load hdf5/1.8.18
export PATH=/opt/softs/libraries/GCC_5.3.0/nco-4.5.5/bin/:\$PATH
export PATH=$install_dir/bin:\$PATH   # we still need sed_i command
alias ncap2="ncap"
" >> $install_dir/env_LMDZ.profile
    ;;

  *) # in the general case, we copy env.sh to env_LMDZ.profile
    cat $install_dir/env.sh >> $install_dir/env_LMDZ.profile
    ;;
esac

source $install_dir/base_env.profile
source $install_dir/env_LMDZ.profile

# Could be written with "sed -i" but this option is not safe on MacOSX
# a POSIX version of sed -i has been written in $installdir/bin/sed_i 
#sed_i -e 's:LMDZdir=.*.$:LMDZdir='"$LMDZdir"':' -e 's/^rad=.*$/rad='$rad'/' "$ROOT/models/LMDZ/serie_LMDZ.sh"
# this is no longer useful because LMDZdir is written in env_LMDZ.profile, 
# which is sourced by serie_LMDZ.sh

echo '#####################################################################'
echo ' Installing LMDZ if needed'
echo ' To force reinstalling, you should run'
echo ' rm -rf LMDZtrunk'
echo '#####################################################################'

if [[ ! -d $LMDZdir/1D ]] ; then
   echo "LMDZ directory called $LMDZdir does not exists"
   echo "LMDZ installation will start within 3 seconds"
   sleep 3
   cd "$LMDZroot"
   wget http://lmdz.lmd.jussieu.fr/pub/install_lmdz.sh -O install_lmdz.sh
   echo "Installing LMDZ, output in $(pwd)/install_lmdz$$.out; Might take minutes"

   # Gestion de la compilation netcdf
   gccnum=$( gcc --version | head -1  | awk ' { print $NF } ' | cut -d. -f1 )
   netcdf=$LMDZroot/netcdf$gccnum
   sed '/default_env_path=$env_path/aecho "# Nothing to do" > $( dirname $env_path )/arch-local-gfortran.env' install_lmdz.sh > install_lmdz.sh.sedtmp
   mv install_lmdz.sh.sedtmp install_lmdz.sh 
   chmod +x install_lmdz.sh
   ncopt="-netcdf $netcdf"
   ncopt="-netcdf 0"
   # Si on est sûr ou sur un cent ncopt="" ou ncopt="-netcdf 0"


   install_cmd="./install_lmdz.sh $version -bench 0 $ncopt -name $LMDZ -verbose" # -noclean"
   echo "$install_cmd"
   bash $install_cmd &> install_lmdz$$.out
   cd "$LMDZ"
   wget http://lmdz.lmd.jussieu.fr/pub/1D/1D.tar.gz ; tar xvf 1D.tar.gz
   cd -
   # Changing the default debug option for compilation
   sed_i -e '/opt_comp=.*.debug/s/-debug//' -e 's/^rad=.*$/rad='$rad'/' "$LMDZ/1D/bin/compile"
   sed_i -e '/opt_compile=.*.debug/s/-debug//' -e 's/^rad=.*$/rad='$rad'/' "$LMDZ/1D/run.sh"
   cd -
else
   echo "$LMDZdir Already exists"
   echo "No need to reinstall"
fi

echo '#####################################################################'
echo ' End of setup_LMDZ.sh'
echo '#####################################################################'
