#!/bin/bash

set -e
#set -vx

########################################################################
#======================================================================#
#----------------------------------------------------------------------#
#|               1. OPTIONS                                           |#
#======================================================================#
########################################################################

# Special tricks to deactivate the git clone and so when the network
# is not available. Can be used only after the setup.sh has been run succesfully
# once
network=on # on/off

# Old (ExeterUQ) or new (ExeterUQ_MOGP) of Exter UQ programs
ExeterUQ=ExeterUQ_MOGP
pip=pip3 # or pip
python=python3 # or python

if [ $# = 0 ] ; then
cat <<eod
Use : setup.sh Model_Name [EXPE_NAME]
Model_Name name among :
eod
ls  models | cat
exit
fi
MODEL=$1

if [ ! -d models/$MODEL ] ; then
   echo models/$MODEL does not exists
   exit
fi

if [ $# = 1 ] ; then
   EXP=$MODEL
else
   EXP=$2
fi



########################################################################
#======================================================================#
#|           2. CHECKING PACKAGES INSTALLATION                        |#
#======================================================================#
########################################################################

############################################
# Testing availability of various programs
############################################
for exe in R Rscript cdo ncks $python $pip ; do
    if [ "`which $exe`" = "" ] ; then echo You need $exe ; exit 1 ; fi
done

#####################################################
# After svn rev 200, python installation is required
# Except if forcing running an old bench with old=0.
#####################################################
rev=`svn info --show-item revision`

#####################################################
# Insure the same version of mogp tools will be used 
# with this version of the HighTune project
#####################################################
# This commit corresponds to the last one on devel 
# Author: Eric Daub <45598892+edaub@users.noreply.github.com>
# Date:   Wed Jun 3 18:18:33 2020 +0100
mogp_commit=8dd1b6f76b93c8b96540f240b0dc0d6739892d00

if [ $network = on ] ; then
if [ $ExeterUQ = ExeterUQ_MOGP ] ; then
    if [ ! -d mogp_emulator ] ; then
      git clone https://github.com/alan-turing-institute/mogp_emulator
    fi 
    cd mogp_emulator
    $pip install --user -r requirements.txt
    $pip install patsy --user
    git checkout $mogp_commit
    $python setup.py install --user
    cd ..

    ############################################################
    # Needed R packages
    # Following R documentations, using ~/.Renviron to specify
    # where the R packages should be installed by a non root user
    ############################################################
    if [ ! -f ~/.Renviron ] ; then
      rversion=`R --version | head -1 | awk ' { print $3 } '`
      R_LIBS_USER=~/.local/lib/r-HighTune/$rversion
      echo 'R_LIBS_USER='$R_LIBS_USER >> ~/.Renviron
      mkdir -p $R_LIBS_USER 
    else
      if [ "`grep R_LIBS_USER ~/.Renviron`" = "" ] ; then
         echo ~/.Renviron exists without R_LIBS_USER, please check ; exit 1 ; fi
    fi
    eval src/CheckInstallPackages.sh reticulate invgamma GenSA far fields lhs maps mco ncdf4 shape tensor withr loo MASS pracma
    if [ "$?" != "0" ] ; then echo Problem encountered when installing R packages ; exit 1 ; fi
else
    # When using the original version of ExeterUQ, should install rstan
    # Touchy ...
    eval src/CheckInstallPackages.sh reticulate invgamma GenSA far fields lhs maps mco ncdf4 shape tensor withr loo MASS rstan
    if [ "$?" != "0" ] ; then echo Problem encountered when installing R packages ; exit 1 ; fi
fi # if [ $network = on ] ; then
fi # if [ $ExeterUQ = ExeterUQ_MOGP ] ; then

#####################################################
# Insure the same version of ExeterUQ tools will be used 
# with this version of the HighTune project
#####################################################
# This commit corresponds to the last one on master
# Merge: 089036e e284f7e
# Author: vicvolodina93 <52710405+vicvolodina93@users.noreply.github.com>
# Date:   Thu Jun 4 07:59:41 2020 +0100
# exeter_commit=9e45cde952d75c10515646f412226f6072606b81
exeter_commit=ef50736999f1879f19759ca84655a326e7a6b74d

if [ ! -d $ExeterUQ ] ; then
   git clone https://github.com/BayesExeter/$ExeterUQ 
fi 
cd $ExeterUQ
if [ $ExeterUQ = ExeterUQ_MOGP ] ; then git checkout $exeter_commit ; fi
cd ..

########################################################################
#======================================================================#
#            3. CONFIGURING THE SPECIFIC EXPERIMENT                    #
#======================================================================#
########################################################################

#########################################################
# Possibility to have a setup_* specific of a given model
#########################################################
if [ -f models/$MODEL/setup_$MODEL.sh ] ; then models/$MODEL/setup_$MODEL.sh ; fi

######################################################
# Download the data relevant to the current experiment
######################################################
if [ "$MODEL" = "ECRAD" ] ; then
  
  if [ ! -d LES1D_ecRad ] ; then
     wget http://simu:visu2018@www.umr-cnrm.fr/visu-high-tune/data_tuningtool/LES1D_ecRad.tar
     tar xvf LES1D_ecRad.tar
     \rm -rf LES1D_ecRad.tar
  fi
  
  if [ ! -d RAD ] ; then
     wget http://simu:visu2018@www.umr-cnrm.fr/visu-high-tune/data_tuningtool/RAD.tar
     tar xvf RAD.tar
     \rm -rf RAD.tar
  fi

else

  if [ ! -d LES ] ; then
     wget http://simu:visu2018@www.umr-cnrm.fr/visu-high-tune/data_tuningtool/les.tar
     tar xvf les.tar
     \rm -rf les.tar
  fi

fi

DIR0=`pwd`

mkdir -p WORK/$EXP

cp src/* WORK/$EXP/
cp -r models/$MODEL/* WORK/$EXP/

cd WORK/$EXP
if [ "$MODEL" = "ECRAD" ] ; then
  ln -s $DIR0/RAD .
else
  ln -s $DIR0/LES .
fi
ln -s $DIR0/$ExeterUQ/BuildEmulator .
ln -s $DIR0/$ExeterUQ/HistoryMatching .
ln -s $DIR0/mogp_emulator .


##############################################################
# Modules. In preparations for computer centers
##############################################################

hostname=`hostname`
echo lmdz_env $hostname 0:3 ${hostname:0:3}

if [ ${hostname:0:5} = jean- ] ; then
                ( cat <<eod
                module purge
                compilo=19.0.4 # available 2013.0, 2017.2
                module load intel-compilers/\$compilo
                module load intel-mpi/\$compilo
                module load hdf5/1.10.5/intel-\$compilo-mpi
                module load netcdf/4.7.0/intel-\$compilo-mpi
                module load netcdf-fortran/4.4.5/intel-\$compilo-mpi
                module load subversion/1.9.7/gcc-4.8.5
                module load intel-mkl/\$compilo
                module load nco
                module load cdo
                module load ferret
                module load r
                module load python/2.7.16
                login=`whoami` ; groupe=`echo \$login | cut -c2-4`
                # Inputation de la consommation sur le groupe \$groupe
                # Peut se changer a la main :
                # groupe=gzi
                submit="sbatch -A \${groupe}@cpu "
                run="srun --label -n "
                SCRATCHD=\$SCRATCH
                STORED=\$STORE
                LMDZD=\$WORK
eod
) >| env.sh

else
          ( cat <<eod
          echo No special environment needed on this computer
eod
) >| env.sh

fi

chmod +x env.sh
