#!/bin/bash

# stop as soon as a comand returns an error:
set -e

## Default values for various script options
installdir="trunk"
svn_planeto="HEAD"
arch="local"
compiler_suite="gnu"

machine=`hostname`
#if [[ ${machine:7:10} == "occigen" ]] ; then
#  arch=X64_OCCIGEN
#fi
#if [[ ${machine:0:5} == "ada33" ]] ; then
#  arch=X64_ADA
#fi

# 0.1 Check that prerequisits are there
for logiciel in svn wget tar gzip make gfortran ; do
if [ "`which $logiciel`" = "" ] ; then
echo You must first install $logiciel on your system
exit
fi
done

if [ "`which fcm`" = "" ] ; then
echo "You must first install fcm and add it to your PATH, e.g.:"
echo "cd ; svn co http://forge.ipsl.jussieu.fr/fcm/svn/PATCHED/FCM_V1.2"
echo "and add FCM_V1.2/bin to your PATH environment variable,"
echo "e.g. add in your .bashrc the following line:"
echo 'export PATH=$PATH:$HOME/FCM_V1.2/bin'
exit
fi

# 0.2 Get user options from command line
while (($# > 0))
   do
   case $1 in
    "-h") cat <<........end
    Script usage:
    $0 [option1 value1 option2 value2 ...]
    where available options are:
    -installdir name of subdirectory to install in (default: $installdir)
    -planeto_rev revision number of planeto repository (default: $svn_planeto)
........end
     exit ;;
    "-installdir") installdir=$2 ; shift ; shift ;;
    "-planeto_rev") svn_planeto==$2 ; shift ; shift ;;
    "-compiler") compiler_suite=$2 ; shift ; shift ;;
    *) $0 -h ; exit
   esac
done

# check compiler is available
if [[ ${compiler_suite} == "gnu" ]] ; then
  f_compiler="gfortran"
  c_compiler="gcc"
elif [[ ${compiler_suite} == "intel" ]] ; then
  f_compiler="ifort"
  c_compiler="icc"
else
  echo "unknown compiler family $compiler_suite"
  echo "might as well stop here"
  exit
fi
for logiciel in $f_compiler $c_compiler ; do
if [ "`which $logiciel`" = "" ] ; then
echo You must first install $logiciel on your system
exit
fi
done

rootdir=`pwd -P`

# 1. Check out "Planeto" repository
cd $rootdir
svn co --revision $svn_planeto http://svn.lmd.jussieu.fr/Planeto/trunk --depth empty  $installdir
cd $installdir
svn update LMDZ.COMMON LMDZ.TITAN DOC

# 1.2 Download bench test case
cd $rootdir
wget -nv http://www.lmd.jussieu.fr/~lmdz/planets/titan/bench_32x48x55.tar.gz

# 1.3. Check out and build the NetCDF library
cd $rootdir
mkdir netcdf
cd netcdf
wget -nv --no-check-certificate http://www.lmd.jussieu.fr/~lmdz/pub/script_install/install_netcdf4_hdf5_seq.bash
chmod u=rwx install_netcdf4_hdf5_seq.bash
netcdflog=$rootdir/netcdf.log
./install_netcdf4_hdf5_seq.bash -compiler $compiler_suite > $netcdflog 2>&1
export NETCDF=$rootdir/netcdf
export NETCDFINCLUDE=$NETCDF/include
export NETCDFDIR=$NETCDF/lib

# 2.1 Compile IOIPSL
cd $rootdir/$installdir/LMDZ.COMMON/ioipsl
if  [[ ${compiler_suite} == "gnu" ]] ; then
sed -i -e s:'NETCDF_HOME=$(nf-config --prefix)':"NETCDF_HOME=$NETCDF":1 install_ioipsl_gfortran.bash
./install_ioipsl_gfortran.bash > install_ioipsl_gfortran.out 2>&1
else
sed -i -e s:'NETCDF_HOME=$(nf-config --prefix)':"NETCDF_HOME=$NETCDF":1 install_ioipsl_ifort.bash
./install_ioipsl_ifort.bash > install_ioipsl_ifort.out 2>&1
fi

# 2.2 Compile LMDZ
cd $rootdir/$installdir/LMDZ.COMMON
# 2.2.1 Make arch files
cd arch
echo 'ROOT=$PWD' > arch-local.path
echo "" >> arch-local.path
echo 'NETCDF_LIBDIR="-L'$NETCDF'/lib"' >> arch-local.path
echo 'NETCDF_LIB="-lnetcdf -lnetcdff"' >> arch-local.path
echo 'NETCDF_INCDIR="-I'$NETCDF'/include"' >> arch-local.path
echo "" >> arch-local.path
echo 'IOIPSL_INCDIR="-I$ROOT/ioipsl/modipsl/lib"' >> arch-local.path
echo 'IOIPSL_LIBDIR="-L$ROOT/ioipsl/modipsl/lib"' >> arch-local.path
echo 'IOIPSL_LIB="-lioipsl"' >> arch-local.path
echo "" >> arch-local.path
echo 'XIOS_INCDIR="-I$ROOT/../XIOS/inc"' >> arch-local.path
echo 'XIOS_LIBDIR="-L$ROOT/../XIOS/lib"' >> arch-local.path
echo 'XIOS_LIB="-lxios -lstdc++"' >> arch-local.path

if  [[ ${compiler_suite} == "gnu" ]] ; then
cp arch-gfortran.fcm arch-local.fcm
else
cp arch-linux-ifort.fcm arch-local.fcm
fi
# remove reference to using LAPACK
sed -i -e s:"LAPACK":"":1 arch-local.fcm
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:'${NETCDF}'/lib' > arch-local.env

# 2.2.2 Compile GCM
cd $rootdir/$installdir/LMDZ.COMMON
./makelmdz_fcm -arch $arch -p titan -d 32x48x55 -b 23x23 -s 1 -j 8 gcm > makelmdz_fcm.out 2>&1

if [ -f bin/gcm_32x48x55_phytitan_seq.e ] ; then
  echo "success! Executable is in $rootdir/$installdir/LMDZ.COMMON/bin"
else
  echo "something went wrong"
  echo "check file $rootdir/$installdir/LMDZ.COMMON/makelmdz_fcm.out"
fi

# 3. Run Bench
cd $rootdir
tar xvzf bench_32x48x55.tar.gz
cp $installdir/LMDZ.COMMON/bin/gcm_32x48x55_phytitan_seq.e bench_32x48x55/
cd bench_32x48x55/

echo 'source '$rootdir'/'$installdir'/LMDZ.COMMON/arch.env' > run_gcm.sh
echo './gcm_32x48x55_phytitan_seq.e > gcm.out 2>&1' >> run_gcm.sh
chmod u=rwx run_gcm.sh
./run_gcm.sh
echo "Bench testcase run in $rootdir/bench_32x48x55/ using run_gcm.sh"
echo "Outputs are in gcm.out which ends with:"
tail gcm.out
