#!/bin/bash
##################################################################
# 0 or 1 or 2 argument :
#   - 1st = suffix of the physics used (eg: foo --> use phy1dfoo, model name = lmdz1d_foo.e)
#   - 2nd = suffix of the model name (eg : 42 --> model = lmdz1d_foo_42.e)
#      (A dot is equivalent to the absence of a suffix).
# Examples :
#   compile.x norad --> physiq = phy1dnorad, model = lmdz1d_norad.e
#   compile.x norad 42 --> physiq = phy1dnorad, model = lmdz1d_norad_42.e
#   compile.x --> physiq = phy1d, model = lmdz1d.e
#   compile.x . 42 --> physiq = phy1d, model = lmdz1d_42.e
#################################################################
#
opt_comp="-debug"

# opt_comp="-debug -rrtm true -full" # pour forcer a tout recompiler

llm=79

rad=rrtm
main=lmdz1d
physiq=lmd

ici=`pwd`

#
while (($# > 0))
  do
  case $1 in
      "-h")
          echo "Usage : ./compile.x [-L llm] [physique [nom]]" ; exit;;
      "-L")
          llm="$2" ; shift ; shift ;;
      "-rad")
          rad="$2" ; shift ; shift ;;
      "-cosp")
          cosp="$2" ; shift ; shift ;;
      "-opt")
           opt_comp="$2" ; shift ; shift ;;
      "-suf")
           exec="_$2" ; shift ; shift ;;
      "-main")
           main="$2" ; shift ; shift ;;
       "-p")
           physiq="$2" ; shift ; shift ;;
       *) 
	  shift
   esac
done


if [ "$physiq" == "lmd" ] ; then
   physname=""
else
   physname=${physiq}
fi


case $cosp in
     1) cosp_suf=cosp_ ;;
     2) cosp_suf=cosp2_ ;;
     *) cosp_suf=""
esac

suffix=${physname}_${rad}_${cosp_suf}L$llm

##########################################################
# Recherche de l'emplacement de LMDZ
##########################################################
lmdzdir=pastrouve
for mod in ../../.. ../.. .. ; do
for i in LMDZ4 LMDZ5 LMDZ ; do
   if [ -d $mod/modipsl/modeles/$i ] ; then lmdzdir=`pwd`/$mod/modipsl/modeles/$i ; fi
done
done

if [ "$lmdzdir" = "pastrouve" ] ; then echo Probleme : je ne trouve pas LMDZ ; exit ; fi

##########################################################
# Recherche de l'emplacement de LMDZ
# Utile pour la retrocompatibilite avec l'epoque ou 
# phy1d etait dans un repertoire separe
##########################################################
cd $lmdzdir/libf
if [ -d phy1d$physiq ] ; then
   physique=1d${physiq}
elif [ -d phy$physiq ] ; then
   physique=$physiq
elif [ -d phylmd ] ; then
   physique=lmd
fi

##########################################################
echo "compilation sur: "$lmdzdir
##########################################################
#
#
cd $ici ; cd $lmdzdir
pwd
echo OK0
#

fcm=1
if [ `uname` = Darwin ] ; then fcm=1 ; fi
if [ "`grep cosp2 a$opt_comp`" != "" ] ; then fcm=1 ; fi
if [ ${rad} = ecrad ] ; then fcm=1 ; fi
echo OKa

fcm=1

if [ $fcm = 0 ] ; then 
makelmdz=makelmdz
lmdz1d=${main}.e
else
makelmdz="makelmdz_fcm -arch local -parallel none"
lmdz1d="bin/${main}_${llm}_phy${physique}_${rad}_seq.e"
fi
echo OK1

echo ./$makelmdz ${opt_comp} -d $llm -p ${physique} -rad $rad ${main}
./$makelmdz ${opt_comp} -d $llm -p ${physique} -rad $rad ${main}

if [ ! -f ${lmdz1d} ] ; then
   echo La compilation a echoue
   exit 1
else
   echo ===========================================
   echo "Vous avez compile avec:"
   echo ./$makelmdz ${opt_comp} -d $llm -p ${physique} -rad $rad ${main}
   echo ===========================================
fi

suffix=${physname}_${rad}_${cosp_suf}L$llm


echo lmdz1d $lmdz1d
echo main $main
echo suffix $suffix
\mv -f $lmdz1d ${main}${suffix}.e
cd $ici
ln -sf  ${lmdzdir}/${main}${suffix}.e ${main}${suffix}.e
ln -sf ${main}${suffix}.e ${main}.e
