#!/bin/bash

function log()
{
    echo $*
    $*
}

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

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

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

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

function cmd_kernels() # this function is invoked by : './make_python kernels'
{
    cd $KERNELS
    ./codegen hexagonal unstructured

    cd $KERNELS/src/hexagonal
    for x in *.k90 ; do
	update "$x" "$DYNAMICO_ROOT/src/kernels_hex/$x"
    done

    cd $KERNELS/src/unstructured
    for x in *.k90 ; do
	update "$x" "$DYNAMICO_ROOT/src/kernels_unst/$x"
    done

# make sure to recompile 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"
    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
    CYTHONFLAGS="$SOFLAGS $LDFLAGS -L$LIBDIR -licosa $KERNELS_LDFLAGS"
    LDSHARED="$LINK" CFLAGS="-fno-strict-aliasing -O2" LDFLAGS="$CYTHONFLAGS" log python setup.py build_ext --inplace
    cp -fpr $DYNAMICO_ROOT/Python/dynamico $DYNAMICO_ROOT/lib
    cp -fp *.so $DYNAMICO_ROOT/lib/dynamico/
    cd $DYNAMICO_ROOT

    for module in xios meshes dyn time_step DCMIP; do
	echo "from dynamico import $module"
	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

. Python/env/${CURRENT_ARCH}.env

cmd_$1
