#!/bin/bash

# nohup ./compilation.cmd >& compiation.log &

source /opt/modules/init/bash
module load intel/13.0.1
module load openmpi/1.6-intel
module load szip/2.1
module load hdf5/1.8.10
module load netcdf/4.1.3

set -xuve
date

MPI=/share/mpi/openmpi/1.6/ics2012.0.032
LAPACK=/share/compilers/ics/2013.0.028/mkl
SZIP=/share/libs/szip/2.1
HDF5=/share/libs/hdf5/1.8.10
NetCDF=/share/libs/netcdf/4.1.3
GribAPI=/scratch/cfu/opt/grib_api-1.9.9/intel-13
GribEX=/scratch/cfu/opt/gribex_000370/intel-13

cd ..
export ECEARTH=$(pwd)
cd -

PLATFORM=ithaca-intel-openmpi
CONFIG=config.xml

cat > $CONFIG <<EOF
<?xml version="1.0"?>

<Configuration>

    <Translation name="arch_ecconf_fcm">
        <Description>Nemo 3.3.1 config file</Description>
        <Template>nemo-3.3.1/ARCH/arch-ecconf.fcm.tmpl</Template>
        <Target>nemo-3.3.1/ARCH/arch-ecconf.fcm</Target>
        <Properties></Properties>

    </Translation>

    <Translation name="ifs36r4_makefile_config">
        <Description>IFS/36r4 makefile configuration</Description>
        <Template>ifs-36r4/Makefile.d/Makefile.config.ecconf.tmpl</Template>
        <Target>ifs-36r4/Makefile.d/Makefile.config.ecconf</Target>
        <Properties></Properties>

    </Translation>

    <Translation name="oasis_makefile_config">
        <Description>OASIS makefile configuration</Description>
        <Template>oasis3/util/make_dir/Makefile.d/Makefile.config.ecconf.tmpl</Template>
        <Target>oasis3/util/make_dir/Makefile.d/Makefile.config.ecconf</Target>
        <Properties></Properties>

    </Translation>

    <Platform name="$PLATFORM">
        <Description>
            HOST:        ic3.cat
            ARCH:        linux_x86_64
            CPU MODEL:   Intel Xeon X5570 @ 2.93GHz
            USER:        masif
            COMPILER:    Intel (ifort and icc 12.1)
            MPI:         OpenMPI (1.6)
            BLAS/LAPACK: Intel MKL
        </Description>

        <Parameter name="ECEARTH_SRC_DIR">
            <Description>Base directory for EC-Earth sources</Description>
            <Type>PATH</Type>
            <Value>$ECEARTH</Value>
        </Parameter>

        <Parameter name="MPI_BASE_DIR">
            <Description>MPI base directory</Description>
            <Type>PATH</Type>
            <Value>$MPI</Value>
        </Parameter>

        <Parameter name="MPI_INC_SUBDIR">
            <Description>MPI include directory relative to base dir</Description>
            <Type>PATH</Type>
            <Value>include</Value>
        </Parameter>

        <Parameter name="MPI_LIB_SUBDIR">
            <Description>MPI lib directory relative to base dir</Description>
            <Type>PATH</Type>
            <Value>lib</Value>
        </Parameter>

        <Parameter name="MPI_LIBS_WITHOUT_L">
            <Description>MPI libraries (without -l prefix)</Description>
            <Type>STRING</Type>
            <Value>mpi_f77 mpi</Value>
        </Parameter>

        <Parameter name="LAPACK_BASE_DIR">
            <Description>LAPACK base directory</Description>
            <Type>PATH</Type>
            <Value>$LAPACK</Value>
        </Parameter>

        <Parameter name="LAPACK_LIB_SUBDIR">
            <Description>LAPACK lib directory relative to base dir</Description>
            <Type>PATH</Type>
            <Value>lib/intel64</Value>
        </Parameter>

        <Parameter name="LAPACK_LIBS_WITHOUT_L">
            <Description>LAPACK libraries (without -l prefix)</Description>
            <Type>STRING</Type>
            <Value>mkl_intel_lp64 mkl_core mkl_sequential</Value>
        </Parameter>

        <Parameter name="NETCDF_BASE_DIR">
            <Description>NetCDF base directory</Description>
            <Type>PATH</Type>
            <Value>$NetCDF</Value>
        </Parameter>

        <Parameter name="NETCDF_INC_SUBDIR">
            <Description>NetCDF include directory relative to base dir</Description>
            <Type>PATH</Type>
            <Value>include</Value>
        </Parameter>

        <Parameter name="NETCDF_LIB_SUBDIR">
            <Description>NetCDF lib directory relative to base dir</Description>
            <Type>PATH</Type>
            <Value>lib</Value>
        </Parameter>

        <Parameter name="NETCDF_LIBS_WITHOUT_L">
            <Description>NetCDF libraries (without -l prefix)</Description>
            <Type>STRING</Type>
            <Value>netcdf netcdff</Value>
        </Parameter>

        <Parameter name="GRIBAPI_BASE_DIR">
            <Description>GRIB API base directory</Description>
            <Type>PATH</Type>
            <Value>$GribAPI</Value>
        </Parameter>

        <Parameter name="GRIBAPI_INC_SUBDIR">
            <Description>GRIB API include directory relative to base dir</Description>
            <Type>PATH</Type>
            <Value>include</Value>
        </Parameter>

        <Parameter name="GRIBAPI_LIB_SUBDIR">
            <Description>GRIB API lib directory relative to base dir</Description>
            <Type>PATH</Type>
            <Value>lib</Value>
        </Parameter>

        <Parameter name="GRIBAPI_LIBS_WITHOUT_L">
            <Description>GRIB_API libraries (without -l prefix)</Description>
            <Type>STRING</Type>
            <Value>grib_api_f90 grib_api</Value>
        </Parameter>

        <Parameter name="GRIBEX_BASE_DIR">
            <Description>GRIBEX base directory</Description>
            <Type>PATH</Type>
            <Value>$GribEX</Value>
        </Parameter>

        <Parameter name="GRIBEX_LIB_SUBDIR">
            <Description>GRIBEX lib directory relative to base dir</Description>
            <Type>PATH</Type>
            <Value>lib</Value>
        </Parameter>

        <Parameter name="GRIBEX_LIBS_WITHOUT_L">
            <Description>GRIBEX libraries (without -l prefix)</Description>
            <Type>STRING</Type>
            <Value>gribexR64</Value>
        </Parameter>

        <Parameter name="MAKE">
            <Description>Make command (GNU make >3.81 needed!)</Description>
            <Type>STRING</Type>
            <Value>make</Value>
        </Parameter>

        <Parameter name="F90">
            <Description>F90 Compiler</Description>
            <Type>STRING</Type>
            <Value>ifort</Value>
        </Parameter>

        <Parameter name="F90FLAGS">
            <Description>General F90 flags for compiling</Description>
            <Type>STRING</Type>
            <Value>-O2 -g -traceback -vec-report0 -r8</Value>
        </Parameter>

        <Parameter name="CC">
            <Description>C Compiler</Description>
            <Type>STRING</Type>
            <Value>icc</Value>
        </Parameter>

        <Parameter name="CFLAGS">
            <Description>General C flags for compiling</Description>
            <Type>STRING</Type>
            <Value>-O2 -g -traceback</Value>
        </Parameter>

        <Parameter name="LD">
            <Description>Linker</Description>
            <Type>STRING</Type>
            <Value>ifort</Value>
        </Parameter>

        <Parameter name="LDFLAGS">
            <Description>General flags for linking</Description>
            <Type>STRING</Type>
            <Value>-O2 -g -traceback -L${HDF5}/lib -lhdf5 -lhdf5_hl -L${SZIP}/lib -lsz</Value>
        </Parameter>

        <Parameter name="AR">
            <Description>Command for building libraries from object files (usually ar)</Description>
            <Type>STRING</Type>
            <Value>ar</Value>
        </Parameter>

        <Parameter name="ARFLAGS">
            <Description>Flags for library building command (When using ar: include u)</Description>
            <Type>STRING</Type>
            <Value>curv</Value>
        </Parameter>

        <Parameter name="CPP">
            <Description>C preprocessor command</Description>
            <Type>STRING</Type>
            <Value>icc</Value>
        </Parameter>

        <Parameter name="CPPFLAGS">
            <Description>C preprocessor flags</Description>
            <Type>STRING</Type>
            <Value>-P -C</Value>
        </Parameter>

        <Parameter name="OASIS_ADD_F90FLAGS">
            <Description>More F90 flags for Oasis</Description>
            <Type>STRING</Type>
            <Value>-132 -check pointers -check uninit</Value>
        </Parameter>

        <Parameter name="OASIS_ADD_CPPDEFS">
            <Description>More CPP/FPP macros for Oasis</Description>
            <Type>STRING</Type>
            <Value>use_oasis_para</Value>
        </Parameter>

        <Parameter name="OASIS_ADD_LDFLAGS">
            <Description>More LD flags for Oasis</Description>
            <Type>STRING</Type>
            <Value></Value>
        </Parameter>

        <Parameter name="NEMO_ADD_F90FLAGS">
            <Description>More F90 flags for Nemo</Description>
            <Type>STRING</Type>
            <Value>-check pointers -check uninit</Value>
        </Parameter>

        <Parameter name="NEMO_ADD_LDFLAGS">
            <Description>More LD flags for Nemo</Description>
            <Type>STRING</Type>
            <Value></Value>
        </Parameter>

        <Parameter name="IFS_FPPDEFS">
            <Description>Preprocessor defs for IFS sources</Description>
            <Type>STRING</Type>
            <Value>linux LINUX LITTLE LITTLE_ENDIAN POINTER_64 BLAS</Value>
        </Parameter>

        <Parameter name="IFSAUX_ADD_F90FLAGS">
            <Description>More F90 flags for ifs/ifsaux</Description>
            <Type>STRING</Type>
            <Value></Value>
        </Parameter>

        <Parameter name="EMOS_ADD_FFLAGS">
            <Description>Additional Fortran compiler flags for EMOSLIB</Description>
            <Type>STRING</Type>
            <Value>-i4</Value>
        </Parameter>

        <Parameter name="EMOS_FPPDEFS">
            <Description>Fortran preprocessor defs for EMOSLIB</Description>
            <Type>STRING</Type>
            <Value>linux LITTLE_ENDIAN POINTER_64 INTEGER_IS_INT REAL_8 REAL_BIGGER_THAN_INTEGER</Value>
        </Parameter>

        <Parameter name="EMOS_CPPDEFS">
            <Description>C preprocessor defs for EMOSLIB</Description>
            <Type>STRING</Type>
            <Value>\$(EMOS_FPPDEFS) FOPEN64</Value>
        </Parameter>

        <Parameter name="MAKEDEPF90">
            <Description>F90 dependency generator</Description>
            <Type>STRING</Type>
            <Value>\$(ECEARTH_SRC_DIR)/util/makedepf90/bin/makedepf90</Value>
        </Parameter>

    </Platform>

</Configuration>
EOF
cat $CONFIG

# MAKEDEP90 Compilation
# =====================

if [ ! -d makedepf90-2.8.8 ]; then
 tar -zxvf ../util/makedepf90/src/makedepf90-2.8.8.tar.gz
 cd makedepf90-2.8.8
 ./configure --prefix=$ECEARTH/util/makedepf90/
 make
 make install
 cd -
fi

# EC-CONF Usage
# =============

cd ..
$ECEARTH/util/ec-conf/ec-conf --platform $PLATFORM build-config/$CONFIG
BA=ecconf # Build Arch
cd -

# OASIS Compilation
# =================

cd $ECEARTH/oasis*/util/make_dir/
#make realclean -f TopMakefileOasis3 BUILD_ARCH=$BA
make -f TopMakefileOasis3 BUILD_ARCH=$BA

# NEMO Compilation
# ================

cd $ECEARTH/nemo*/CONFIG/
for f in $(ls -1 */cpp*.fcm); do
 cat $f
 cat $f | sed -e 's/key_coupled //g' -e 's/key_oasis3 //g' > $f.tmp
 mv $f.tmp $f
 cat $f
done

nemo_configs=`ls -1 | grep ORCA`
#set +e
# ./makenemo clean
#set -e
for nemo_config in $nemo_configs; do
 ./makenemo -m $BA -n $nemo_config -j 8
done

# IFS Compilation
# ===============

#cd $ECEARTH/ifs*/
#make clean BUILD_ARCH=$BA
#make realclean BUILD_ARCH=$BA
#make dep-clean BUILD_ARCH=$BA
#make -j 8 BUILD_ARCH=$BA lib
#make BUILD_ARCH=$BA master

date
