#!/bin/ksh

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

module add python/2.7.2-01
PATH=/perm/ms/spesiccf/c3m/opt/make-3.81:${PATH}

set -xuve
date

MPI=
LAPACK=/usr/local/apps/lapack/3.1.1/LP64
SZIP=/usr/local/apps/szip/2.1/LP64
HDF5=/usr/local/apps/hdf5/1.8.9/LP64
NetCDF=/usr/local/apps/netcdf4/4.1.3/LP64
GribAPI=/usr/local/lib/metaps/lib/grib_api/1.9.16
GribEX=/perm/ms/spesiccf/c3m/opt/gribex_000370/ibm-xlc
JASPER=/usr/local/apps/jasper/1.900.1/LP64

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

PLATFORM=ecmwf-xlc-poe
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:        c2a at ecmwf
            ARCH:        AIX
            CPU MODEL:   IBM Power7
            COMPILER:    IBM XL
            MPI:         IBM POE
            BLAS/LAPACK: IBM ESSL
        </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></Value>
        </Parameter>

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

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

        <Parameter name="MPI_LIBS_WITHOUT_L">
            <Description>MPI libraries (without -l prefix)</Description>
            <Type>STRING</Type>
            <Value></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></Value>
        </Parameter>

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

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

        <Parameter name="GRIBAPI_LIBS_WITHOUT_L">
            <Description>GRIB_API libraries (without -l prefix)</Description>
            <Type>STRING</Type>
            <Value>grib_api grib_api_f90</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="FC">
            <Description>Fortran 90 Compiler</Description>
            <Type>STRING</Type>
            <Value>mpxlf90</Value>
        </Parameter>

        <Parameter name="FFLAGS">
            <Description>General F90 flags for compiling</Description>
            <Type>STRING</Type>
            <Value>-O2 -g -q64 -qrealsize=8 -qport=mod -qspillsize=8162</Value>
        </Parameter>

        <Parameter name="FFLAGS_FREEFORM">
            <Description>Allow for free format Fortran</Description>
            <Type>STRING</Type>
            <Value>-qfree=f90</Value>
        </Parameter>

        <Parameter name="FFLAGS_FIXEDFORM">
            <Description>Expect fixed Fortran format</Description>
            <Type>STRING</Type>
            <Value>-qfixed</Value>
        </Parameter>

        <Parameter name="FFLAGS_FPP_PREFIX">
            <Description>Fortran preprocessor flag prefix</Description>
            <Type>STRING</Type>
            <Value>-WF\$(comma)-D</Value>
        </Parameter>

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

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

        <Parameter name="CFLAGS_CPP_PREFIX">
            <Description>C preprocessor flag prefix</Description>
            <Type>STRING</Type>
            <Value>-D</Value>
        </Parameter>

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

        <Parameter name="LDFLAGS">
            <Description>General flags for linking</Description>
            <Type>STRING</Type>
            <Value>-O2 -g -q64 -qarch=auto -qrealsize=8 -L${HDF5}/lib -lhdf5 -lhdf5_hl -L${SZIP}/lib -lsz -lz -L${JASPER}/lib -ljasper</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 -X64</Value>
        </Parameter>

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

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

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

        <Parameter name="OASIS_ADD_FFLAGS">
            <Description>More F90 flags for Oasis</Description>
            <Type>STRING</Type>
            <Value></Value>
        </Parameter>

        <Parameter name="OASIS_ADD_PPDEFS">
            <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_FFLAGS">
            <Description>More F90 flags for Nemo</Description>
            <Type>STRING</Type>
            <Value></Value>
        </Parameter>

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

        <Parameter name="IFS_PPDEFS">
            <Description>Preprocessor defs for IFS sources</Description>
            <Type>STRING</Type>
            <Value>POINTER_64 BLAS DOUBLE_KIND_EQUALS_REAL</Value>
        </Parameter>

        <Parameter name="IFSAUX_ADD_FFLAGS">
            <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-ecmwf</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
