#!/bin/bash

# ./compilation.sh >& compile.log &
# or
# ./compilation.sh -i 6 -j 6 >& compile.log &

# Modify key_nproci and key_nprocj in the following "fcm"
# file according to no. of proc to be used.
# vi ORCA2_LIM/cpp_ORCA2_LIM.fcm

# Modify compiler options in the appropriat
# file located at the following path.
# vi ../ARCH/arch-*.fcm

nemo_i=6
nemo_j=6

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

source /opt/modules/init/bash
module load intel/12.1
module load openmpi/1.6-intel

set -xuve
date

compile=ifort_linux
cat > ../ARCH/arch-${compile}.fcm <<EOF
# generic ifort compiler options for linux
# NCDF_INC    netcdf include file
# NCDF_LIB    netcdf library
# FC          Fortran compiler command
# FCFLAGS     Fortran compiler flags
# FFLAGS      Fortran 77 compiler flags
# LD          linker
# LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
# FPPFLAGS    pre-processing flags
# AR          assembler
# ARFLAGS     assembler flags
# MK          make
# USER_INC    additional include files for the compiler,  e.g. -I<include dir>
# USER_LIB    additional libraries to pass to the linker, e.g. -l<library>


%NCDF_INC            -I/scratch/cfu/opt/netcdf-3.6.3/intel12/include 
%NCDF_LIB            -L/scratch/cfu/opt/netcdf-3.6.3/intel12/lib -lnetcdf
%FC                  mpif90
%FCFLAGS             -r8 -O3  -traceback
%FFLAGS              -r8 -O3  -traceback
%LD                  mpif90
%FPPFLAGS            -P -C -traditional
%LDFLAGS
%AR                  ar
%ARFLAGS             -r
%MK                  gmake
%USER_INC            %NCDF_INC
%USER_LIB            %NCDF_LIB
EOF
cat ../ARCH/arch-${compile}.fcm

set +e
./makenemo clean
set -e

config=ORCA1_LIM2
./makenemo -n $config -d "OPA_SRC LIM_SRC_2"
cat > $config/cpp_${config}.fcm <<EOF
bld::tool::fppkeys key_trabbl key_vectopt_loop key_orca_r1 key_lim2 key_dynspg_flt key_diaeiv key_ldfslp key_traldf_c2d key_traldf_eiv key_dynldf_c3d key_dtatem key_dtasal key_tradmp key_zdftke key_zdfddm key_nproci=$nemo_i key_nprocj=$nemo_j key_mpp_mpi
EOF
cat $config/cpp_${config}.fcm
./makenemo -m $compile -n $config

config=ORCA1_LIM3
./makenemo -n $config -d "OPA_SRC LIM_SRC_3"
cat > $config/cpp_${config}.fcm <<EOF
bld::tool::fppkeys key_trabbl key_vectopt_loop key_orca_r1 key_lim3 key_dynspg_flt key_diaeiv key_ldfslp key_traldf_c2d key_traldf_eiv key_dynldf_c3d key_dtatem key_dtasal key_tradmp key_zdftke key_zdfddm key_nproci=$nemo_i key_nprocj=$nemo_j key_mpp_mpi
EOF
cat $config/cpp_${config}.fcm
./makenemo -m $compile -n $config

date
