#!/bin/bash

function log()
{
    echo $*
    $*
}

function full_dir()
{
    (cd -P $1 ; pwd)
}

function cmd_clean() # cleanup, invoked by : './make_python clean'
{
    rm -f lib/*.so lib/dynamico
    cd $KERNELS/src
    rm -rf *.o *.so build/
}

#--------------- Generate kernels, invoked by : './make_python kernels' ---------------

function update()
{
    diff $2 $1 || log cp -pr $1 $2
}

function update_kernels()
{
    cd $KERNELS/src/$1
    for x in *.k90 ; do
	update "$x" "$DYNAMICO_ROOT/src/$2/$x"
    done

}

function cmd_kernels() # this function is invoked by : './make_python kernels'
{
    cmd_clean
    cd $KERNELS
    ./codegen hexagonal hex_master unstructured
    update_kernels hexagonal kernels_hex
    update_kernels hex_master kernels_hex_master
    update_kernels unstructured kernels_unst
# make sure that ./rebuild recompiles dynamics
    cd $DYNAMICO_ROOT
    touch src/dynamics/*.F90 src/diagnostics/*.F90 src/transport/*.F90 src/unstructured/*.F90
}

#------------------ Default action , invoked by : './make_python' --------------------

function make_so()
{
    ARLIB=$1
    SOLIB=$2

    if [[ $ARLIB -nt $SOLIB ]]; then
        echo $ARLIB is newer than $SOLIB
	rm -f tmp.a
	cp $ARLIB tmp.a
	if [[ "$4" != "" ]] ; then ar vd tmp.a $4 ; fi
	if [[ "$5" != "" ]] ; then ar vr tmp.a $5 ; fi
        log $LINK $SOFLAGS $WHOLE_ARCHIVE tmp.a $NO_WHOLE_ARCHIVE $3 -o $SOLIB
	rm -f tmp.a
        $LDD $SOLIB
    fi
}


function cmd_()
{
    echo "XIOS_ROOTDIR : $XIOS_ROOTDIR"
    MISSING=""
    for module in netCDF4 jinja2 mpi4py; do
	echo "import $module"
	python -c "import $module" || MISSING="$MISSING $module"
    done
    if [[ "$MISSING" == "" ]] ; then
	echo "All required Python modules seem present."
    else
	echo "Missing/dysfunctional python modules : $MISSING"
	echo "Please fix your Python configuration"
	exit
    fi

    log mpicc -c -std=c99 -fPIC $PARMETIS_CFLAGS Python/src/partition.c -o obj/partition.o
    make_so $XIOS_ROOTDIR/lib/libxios.a $LIBDIR/libxios.so "$NETCDF_LDFLAGS -lstdc++"
    make_so $LIBDIR/libicosa.a $LIBDIR/libicosa.so "-L$LIBDIR -lxios $KERNELS_LDFLAGS" "physics_external_mod.o physics_mod.o timeloop_gcm_mod.o icosa_init_mod.o" obj/partition.o

    # Cythonize Python modules
    cd $DYNAMICO_ROOT/Python/src
    NUMPY=$(python -c 'import numpy as np ; print np.get_include()')
    CYTHONFLAGS="$SOFLAGS $LDFLAGS -L$LIBDIR -licosa $KERNELS_LDFLAGS"
    LDSHARED="$LINK" CFLAGS="-fno-strict-aliasing -O2 -I$NUMPY" LDFLAGS="$CYTHONFLAGS" log python setup.py build_ext --inplace || exit

    cp -fpr $DYNAMICO_ROOT/Python/dynamico $DYNAMICO_ROOT/lib
    cp -fp *.so $DYNAMICO_ROOT/lib/dynamico/
    cd $DYNAMICO_ROOT

    for module in cxios unstructured meshes dyn time_step DCMIP; do
	echo "from dynamico import $module"
	LD_PRELOAD=$PYTHON_PRELOAD python -c "from dynamico import $module"
    done
}

#------------------------- script starts here ------------------------

DYNAMICO_ROOT=$PWD
LIBDIR=$DYNAMICO_ROOT/lib
KERNELS=$DYNAMICO_ROOT/Python

. arch.env
. arch.path

echo "Selected architecture : $CURRENT_ARCH"

if [[ "$CURRENT_ARCH" == "" ]] ; then echo "Current architecture undefined or unsupported" ; exit ; fi

cd Python/env ;  . ${CURRENT_ARCH}.env ; cd $DYNAMICO_ROOT

time cmd_$1
