#!/bin/bash
set -eu  # exit on most (not all!) error codes, and error on unset variables
set -vx

######################################################################
# Author : Najda Villefranque
# Setting up the MESONH SCM for HighTune explorer
# 2025/02/04
######################################################################

network=1 # 0 to set it off and forbid git commands
ROOT=$(pwd)
install_dir=$(realpath $ROOT/..)

VERSION=5-7-1

# local variables end with underscore to avoid 
# resetting them when sourcing mesonh profiles
MNH_ECRAD_=1      # 0 to compile without ECRAD
VER_USER_=MOSAI   # something to compile user sources
VER_USER_=ADRIEN  # something to compile user sources
VER_USER_=        # something to compile user sources

reset_mesonh_env(){
  ### the commented commands below do not work...
  # prof=$1
  # grep "^export" $prof|awk -F= '{ print $1 }'|sed -e "s/export/unset/" > unset_vars
  # source unset_vars
  # rm unset_vars
  # 
  # unset only XYZ to avoid error message (not very clean???)
  unset XYZ
}

get_mesonh(){
  cd $MNH_ROOT
  wget http://mesonh.aero.obs-mip.fr/mesonh/dir_open/dir_MESONH/MNH-V${VERSION}.tar.gz
  tar xvf MNH-V${VERSION}.tar.gz
  rm MNH-V${VERSION}.tar.gz
  cd - > /dev/null
}

compile_mesonh_master(){
  cd $MNH_DIR/src
  export MNH_ECRAD=$MNH_ECRAD_
  set +u
  ./configure
  prof=$(ls -1rt ../conf/profile_mesonh-*|tail -1) # with path
  source $prof
  cp $prof $install_dir
  if [ ! -f $MNH_DIR/exe/PREP_IDEAL_CASE$XYZ ] ; then
    ####### patch ######
    ln -sf LIB/RAD/ecrad-1.4.0/data ecrad_data
    sed_i 's,^CDATADIR.*=.*,CDATADIR="'$MNH_DIR'/src/ecrad_data",g' \
      $MNH_DIR/src/MNH/ini_radiations_ecrad.f90
    ####################
    make -j
    make installmaster
  fi
  cd - > /dev/null
  set -u
}

compile_mesonh_user(){
  cd $MNH_DIR/src
  export VER_USER=$VER_USER_
  export MNH_ECRAD=$MNH_ECRAD_
  if [ ! -d $VER_USER ] ; then 
    if [ $network -eq 1 ] ; then 
      wget https://web.lmd.jussieu.fr/~nvillefranque/pub/data/mnh/$VER_USER.tar
      tar xvf $VER_USER.tar
    fi
    test ! -d $VER_USER && { echo "error: no $VER_USER repo in $pwd" ; exit 1 ; }
  fi

  set +u
  ./configure
  prof=$(ls -1rt ../conf/profile_mesonh-*|tail -1) # with path
  source $prof
  cp $prof $install_dir
  if [ ! -f $MNH_DIR/exe/PREP_IDEAL_CASE$XYZ ] ; then
    make user -j 
    make installuser
  fi
  cd - > /dev/null
  set -u
}

install_dephy-tools(){
  tool=$1 # e.g. dephy-mnh or dephy-scm
  url=$2  # e.g. https://github.com/gdr-dephy/dephy-scm.git
  cd $install_dir
  if [ ! -d $tool ] ; then
    if [ $network -eq 1 ] ; then 
      git clone $url $tool
    else
     echo "error: Failed cloning $tool. Is network=1 in setup_MESONH.sh? Exit." && exit 1
    fi
  else
    if [ $network -eq 1 ] ; then
      cd $tool
      #git pull origin master
      cd ..
    else
      echo "warning: Failed updating $tool. Will use locally available version."
    fi
  fi
  test ! -d $tool && echo "error: Failed installing $tool. Exit." && exit 1
  cd - > /dev/null
}

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

MNH_ROOT=""
MNH=MNH-V${VERSION}
if [[ -d $ROOT/../../$MNH ]] ; then
   MNH_ROOT=$ROOT/../..
else
   # By default, MNH is installed next to HighTune
   MNH_ROOT=$ROOT/..
fi
MNH_ROOT=$(readlink -f "$MNH_ROOT")
MNH_DIR=$MNH_ROOT/$MNH

echo '#####################################################################'
echo ' Installing MESONH if needed'
echo ' To force reinstalling, remove your installation of MNH ' $MNH_DIR
echo '#####################################################################'

echo $MNH_DIR
if [ ! -d  $MNH_DIR ] ; then
  get_mesonh
fi

compile_mesonh_master
echo $VER_USER_
if [ ! -u $VER_USER_ ] ; then 
  reset_mesonh_env
  compile_mesonh_user
fi

prof=$(ls -1rt $MNH_DIR/conf/profile_mesonh-*|tail -1) # with path
echo "export MNH_DIR="$MNH_DIR > $install_dir/env_MESONH.profile
echo "export MNH_PROF="$prof >> $install_dir/env_MESONH.profile

echo '#####################################################################'
echo ' Installing DEPHY-SCM and DEPHY-MNH if needed'
echo '#####################################################################'

install_dephy-tools dephy-scm https://github.com/gdr-dephy/dephy-scm.git
install_dephy-tools dephy-mnh https://github.com/gdr-dephy/dephy-mnh.git

echo "export DEPHY_SCM_DIR="$install_dir"/dephy-scm/" >> $install_dir/env_MESONH.profile
echo "export DEPHY_MNH_DIR="$install_dir"/dephy-mnh/" >> $install_dir/env_MESONH.profile

echo '#####################################################################'
echo ' End of setup_MESONH.sh'
echo '#####################################################################'
