#!/bin/ksh

nemo_i=4
nemo_j=4

while getopts i:j: option
do
 case $option in
  i) nemo_i=$OPTARG;;
  j) nemo_j=$OPTARG;;
  \?) exit 1;;
 esac
done

date
set -xuve

MPI=/opt/osshpc/mpich-mx/64
NetCDF=/gpfs/apps/NETCDF/64
UDUNITS=/gpfs/apps/UDUNITS/1.12.4/64

settings=settings.ibmpower5.orca1.MareNostrum

cd ..
ECEARTH=`pwd`
cd -

cat > $settings <<EOF
# General settings
export MPIDIR=$MPI
export NETCDFDIR=$NetCDF
export UDUNITSDIR=$UDUNITS
which xlf_r

# settings for EC-Earth
export ECEARTH=$ECEARTH

# Settings for OASIS3
export OASIS3ARCH=MareNostrum
export PSMILE=\$ECEARTH/oasis3/prism_2-5/prism/\$OASIS3ARCH

# Settings of IFS
export ARCH=ibmpower5
export IFSROOT=\$ECEARTH/ifs
PATH=\$IFSROOT/scripts:\$PATH

# Settings for NEMO
export NEMOARCH=MareNostrum
export NEMOPROCX=$nemo_i
export NEMOPROCY=$nemo_j
export NEMOGRID=ORCA1
export NEMOCLEAN=no

# Settings for FOcean and SlabOcean models
export FORTRANCOMPILER=xlf90_r
export FORTRANFLAGS='-qfree=F90 -qsuffix=cpp=F90 -c -q64 -qarch=ppc970 -O3 -qstrict -qsource -qrealsize=8 -qextname=utdec,utopen,utinv,utfree,utmake,utdiv,etime,dtime,utmult,utscal,utenc,utcut,flush,utcvt,utexp,utorigin,uttime,utcaltime,utcpy,flush,abor1,follow,addrdiff,broadcint,broadcreal,c_drhook_init_signals,c_drhook_print,dr_hook_util,dr_hook_util_multi,ec_getenv,ec_mpi_atexit,ecmwf_transfer,ec_numenv,ec_putenv,ecqsort,ec_raise,ec_strenv,fft992,getcurheap,getmaxstk,get_opt,getpag,getrss,getstk,gstats,gstats,follow,gstats_label,gstats_label,follow,gstats_print,gstats_psut,gstats_setup,hostnm,ifssig,ifssigb,isrcheq,isrcheq,follow,isrchfge,isrchflt,minv,minv,follow,mxmaop,mxmaop,follow,n_precision,profile_heap_get,rdot,rg,rsort32_func,rsort64,rsum,set99,sgtsl,sigmaster,user_clock,bubox,bucrkey,bufren,bufrex,bunexs,bunpck,bunpks,bupkey,buprq,buprs0,buprs1,buprs2,buprs3,buprt,buprtbox,bus012,busel,busrq,buukey,fmmh,pbbufr,setlalo,c_drhook_start,c_drhook_end,abor1fl,cdrhookinit,cdrhooksetlhook,cmplmyrank,drhookprocinfo,drhookprt,c_drhook_set_lhook,c_drhook_init,dr_hook_procinfo,dr_hook_procinfo,irtc_rate,dr_hook_prt,dr_hook_prt,cmpl_myrank'
export FORTRANLINK=' -Wl,--allow-multiple-definition'
export FOCEANARCH=MareNostrum
export FOCEANCLEAN=yes
export SLABOCEANARCH=MareNostrum
export SLABOCEANCLEAN=yes
EOF
cat $settings

ln -sf $ECEARTH/build/$settings settings

./cleanoasis3.ksh
./makeoasis3.ksh

./cleannemo.ksh
./makenemo.ksh

source /usr/share/modules/module/init/bash
./cleanifs.ksh

cp $MPI/include/mpif.h $ECEARTH/ifs/src/ifsaux/include

module load bsc

### First round, old compilers
module switch fortran/12.1 fortran/10.1
./makeifs.ksh

### Second round, new compilers
module switch fortran/10.1 fortran/12.1
./makeifs.ksh

date
