#!/bin/bash

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

source /opt/modules/default/etc/modules.sh
module swap PrgEnv-pgi PrgEnv-intel

set -xuve
date

MPI=/opt/cray/mpt/5.4.1/xt/gemini/mpich2-intel/120
LAPACK=
NetCDF=/tmp/proj/cli055/opt/netcdf-3.6.3/intel-12.1
GribAPI=/tmp/proj/cli055/opt/grib_api-1.9.16/intel-12.1
GribEX=/tmp/proj/cli055/opt/gribex_000370/intel-12.1

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

PLATFORM=jaguar-intel-craympi
CONFIG=config.xml

cat > $CONFIG <<EOF
<experiment>

    <!-- FILESET "default" <<< -->
    <fileset longname="Default fileset" name="default">

        <!-- NEMO 3.3.1 files -->
        <translation longname="Nemo 3.3.1 config file" name="arch_eexcon_fcm">
            <template filename="../nemo-3.3.1/ARCH/arch-eexcon.fcm.tmpl"/>
            <target filename="../nemo-3.3.1/ARCH/arch-eexcon.fcm"/>
        </translation>

        <!-- IFS 36r4 files -->
        <translation longname="IFS/36r4 makefile configuration" name="ifs36r4_makefile_config">
            <template filename="../ifs-36r4/Makefile.d/Makefile.config.eexcon.tmpl"/>
            <target filename="../ifs-36r4/Makefile.d/Makefile.config.eexcon"/>
        </translation>

        <!-- OASIS3 files -->
        <translation longname="OASIS makefile configuration" name="oasis_makefile_config">
            <template filename="../oasis3/util/make_dir/Makefile.d/Makefile.config.eexcon.tmpl"/>
            <target filename="../oasis3/util/make_dir/Makefile.d/Makefile.config.eexcon"/>
        </translation>

    </fileset>
    <!-- END of FILESET "default" >>> -->

    <!-- PLATFORM "$PLATFORM" <<< -->
    <!--
        HOST:        jaguar.ccs.ornl.gov
        ARCH:        Cray XK6 (x86_64 GNU/Linux)
        CPU MODEL:   AMD Opteron
        COMPILER:    i-compilers v12.1 (icc+ifort)
        MPI:         craympi
        BLAS/LAPACK: 
    -->
    <platform name="$PLATFORM">

        <param name="ECEARTH_BASE_DIR"
               longname="EC-Earth base directory"
               type="PATH">
            $ECEARTH
        </param>

        <!-- MPI -->
        <param name="MPI_BASE_DIR"
               longname="MPI base directory"
               type="PATH">
            $MPI
        </param>

        <param name="MPI_INC_SUBDIR"
               longname="MPI include directory relative to base dir"
               type="PATH">
            include
        </param>

        <param name="MPI_LIB_SUBDIR"
               longname="MPI lib directory relative to base dir"
               type="PATH">
            lib
        </param>

        <param name="MPI_LIBS_WITHOUT_L"
               longname="MPI libraries (without -l prefix)"
               type="STRING">
            fmpich mpich
        </param>

        <!-- LAPACK -->
        <param name="LAPACK_BASE_DIR"
               longname="LAPACK base directory"
               type="PATH">
        </param>

        <param name="LAPACK_LIB_SUBDIR"
               longname="LAPACK lib directory relative to base dir"
               type="PATH">
        </param>

        <param name="LAPACK_LIBS_WITHOUT_L"
               longname="LAPACK libraries (without -l prefix)"
               type="STRING">
        </param>

        <!-- NetCDF -->
        <param name="NETCDF_BASE_DIR"
               longname="NetCDF base directory"
               type="PATH">
            $NetCDF
        </param>

        <param name="NETCDF_INC_SUBDIR"
               longname="NetCDF include directory relative to base dir"
               type="PATH">
            include
        </param>

        <param name="NETCDF_LIB_SUBDIR"
               longname="NetCDF lib directory relative to base dir"
               type="PATH">
            lib
        </param>

        <param name="NETCDF_LIBS_WITHOUT_L"
               longname="NetCDF libraries (without -l prefix)"
               type="STRING">
            netcdf
        </param>

        <!-- GRIB API -->
        <param name="GRIBAPI_BASE_DIR"
               longname="GRIB API base directory"
               type="PATH">
            $GribAPI
        </param>

        <param name="GRIBAPI_INC_SUBDIR"
               longname="GRIB API include directory relative to base dir"
               type="PATH">
            include
        </param>

        <param name="GRIBAPI_LIB_SUBDIR"
               longname="GRIB API lib directory relative to base dir"
               type="PATH">
            lib
        </param>

        <param name="GRIBAPI_LIBS_WITHOUT_L"
               longname="GRIB_API libraries (without -l prefix)"
               type="STRING">
            grib_api_f90 grib_api
        </param>

        <!-- GRIBEX -->
        <param name="GRIBEX_BASE_DIR"
               longname="GRIBEX base directory"
               type="PATH">
            $GribEX
        </param>

        <param name="GRIBEX_LIB_SUBDIR"
               longname="GRIBEX lib directory relative to base dir"
               type="PATH">
            lib
        </param>

        <param name="GRIBEX_LIBS_WITHOUT_L"
               longname="GRIBEX libraries (without -l prefix)"
               type="STRING">
            gribexR64
        </param>

        <!-- MAKE -->
        <param name="MAKE"
               longname="Make command (GNU make >3.81 needed!)"
               type="STRING">
            make
        </param>

        <!-- F90 Compiler -->
        <param name="F90"
               longname="F90 Compiler"
               type="STRING">
            ftn
        </param>

        <param name="F90FLAGS"
               longname="General F90 flags for compiling"
               type="STRING">
            -O2 -g -traceback -vec-report0 -r8
        </param>

        <!-- C Compiler -->
        <param name="CC"
               longname="C Compiler"
               type="STRING">
            cc
        </param>

        <param name="CFLAGS"
               longname="General C flags for compiling"
               type="STRING">
            -O2 -g -traceback
        </param>

        <!-- Linker -->
        <param name="LD"
               longname="Linker"
               type="STRING">
            ftn
        </param>

        <param name="LDFLAGS"
               longname="General flags for linking"
               type="STRING">
            -O2 -g -traceback
        </param>

        <!-- Library builder -->
        <param name="AR"
               longname="Command for building libraries from object files (usually ar)"
               type="STRING">
            ar
        </param>

        <param name="ARFLAGS"
               longname="Flags for library building command (When using ar: include u)"
               type="STRING">
            curv
        </param>

        <!-- C Preprocessor -->
        <param name="CPP"
               longname="C preprocessor command"
               type="STRING">
            cc
        </param>

        <param name="CPPFLAGS"
               longname="C preprocessor flags"
               type="STRING">
            -P -C
        </param>

        <!-- OASIS specific stuff -->
        <param name="OASIS_ADD_F90FLAGS"
               longname="More F90 flags for Oasis"
               type="STRING">
            -mcmodel=medium -132 -check pointers -check uninit
        </param>

        <param name="OASIS_ADD_CPPDEFS"
               longname="More CPP/FPP macros for Oasis"
               type="STRING">
            use_oasis_para
        </param>

        <param name="OASIS_ADD_LDFLAGS"
               longname="More LD flags for Oasis"
               type="STRING">
            -mcmodel=medium
        </param>

        <!-- NEMO specific stuff -->
        <param name="NEMO_ADD_F90FLAGS"
               longname="More F90 flags for Nemo"
               type="STRING">
            -check pointers -check uninit
        </param>

        <param name="NEMO_ADD_LDFLAGS"
               longname="More LD flags for Nemo"
               type="STRING">
        </param>

        <!-- IFS specific stuff -->
        <param name="IFS_FPPDEFS"
               longname="Preprocessor defs for IFS sources"
               type="STRING">
            linux LINUX LITTLE LITTLE_ENDIAN POINTER_64 BLAS
        </param>

        <param name="IFSAUX_ADD_F90FLAGS"
               longname="More F90 flags for ifs/ifsaux"
               type="STRING">
        </param>

        <!-- F90 Dependency generator -->
        <param name="MAKEDEPF90"
               longname="F90 dependency generator"
               type="STRING">
            \$(ECEARTH_BASE_DIR)/util/makedepf90/bin/makedepf90
        </param>

    </platform>
    <!-- END of PLATFORM "$PLATFORM" >>> -->

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

# EEXCON Usage
# ============

$ECEARTH/util/eexcon/eexcon -p $PLATFORM $CONFIG

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

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

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

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

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

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

date
