#!/bin/bash

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

source /opt/modules/default/etc/modules.sh
module swap PrgEnv-pgi PrgEnv-intel
module load xt-libsci fftw

set -xuve
date

MPI=/opt/cray/mpt/5.5.1/gni/mpich2-intel/120
LAPACK=
SZIP=
HDF5=/opt/cray/hdf5/default/intel/120
NetCDF=/opt/cray/netcdf/4.2.0/intel/120
GribAPI=/cfs/klemming/nobackup/a/asifsami/opt/grib_api-1.9.9/intel-12.0
GribEX=/cfs/klemming/nobackup/a/asifsami/opt/gribex_000370/intel-12.0

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

PLATFORM=lindgren-intel-12-craympi
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>

    </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>

    </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>

    </Translation>

    <Platform name="$PLATFORM">
        <Description>
            HOST:        emil-login2.pdc.kth.se
            ARCH:        linux_x86_64
            CPU MODEL:   6-Core AMD Opteron(tm) Processor 23 (D0)
            COMPILER:    intel-compilers (icc+ifort) w.r.t cc+ftn wrappers
            MPI:         Cray MPI
            BLAS/LAPACK: Cray BLAS
        </Description>

        <Parameter name="ECEARTH_SRC_DIR">
            <Description>EC-Earth base directory</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>fmpich mpich</Value>
        </Parameter>

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

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

        <Parameter name="LAPACK_LIBS_WITHOUT_L">
            <Description>LAPACK libraries (without -l prefix)</Description>
            <Type>STRING</Type>
            <Value></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>ftn</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>cc</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>ftn</Value>
        </Parameter>

        <Parameter name="LDFLAGS">
            <Description>General flags for linking</Description>
            <Type>STRING</Type>
            <Value>-O2 -g -traceback -L${HDF5}/lib -lhdf5 -lhdf5_hl</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>cc</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/
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
