#!/bin/bash

# **************************** LICENSE START ***********************************
#
# Copyright 2014 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************


# Script to compile external inline macro functions
#
# The upper part is created by Make in order to copy
# site and platform specific compiler macros from
# Metview configuration files. The rest of the script
# - below - should be site and platform independent.
#

#-- MACRO_EXTRA_LIBS can be set externally to link with custom libs!
#-- MACRO_EXTRA_F77_FLAGS can be set externally for extra Fortran 77 flags!
#-- MACRO_EXTRA_F90_FLAGS can be set externally for extra Fortran 90 flags!
#-- MACRO_EXTRA_CC_FLAGS can be set externally for extra C/C++ flags!
#-- MACRO_EXTRA_INCS is reserved for C programs, for extra -I flags!

# NOTE that we use environment variables METVIEW_DDIR and METVIEW_VVERSION instead
# of METVIEW_DIR and METVIEW_VERSION. We set these in extern.cc when we call the
# popen() routine; we can't always use the normal env vars because these can be
# overwritten by the default modules command at ECMWF.


set -e
CC=/usr/bin/cc
CC_FLAGS="  -g -O2 -fdebug-prefix-map=/build/metview-9YUiPq/metview-5.0.3=. -fstack-protector-strong -Wall -pedantic -fPIC -Wdate-time -D_FORTIFY_SOURCE=2 -pipe"
CXX=/usr/bin/c++
CXX_FLAGS=" -g -O2 -fdebug-prefix-map=/build/metview-9YUiPq/metview-5.0.3=. -fstack-protector-strong -std=c++11 -Wdate-time -D_FORTIFY_SOURCE=2 -pipe -fpermissive -Wno-write-strings -Wno-deprecated"
F77=/usr/bin/gfortran-8
F77_FLAGS="-g -O2 -fdebug-prefix-map=/build/metview-9YUiPq/metview-5.0.3=. -fstack-protector-strong -fdefault-real-8 -fdefault-double-8"
F90=/usr/bin/gfortran-8
F90_FLAGS="-g -O2 -fdebug-prefix-map=/build/metview-9YUiPq/metview-5.0.3=. -fstack-protector-strong -fdefault-real-8 -fdefault-double-8"
FC_LIBS=""
LIBNAME="lib"
GRIBAPI_LIB=" -L/usr/lib/lib -L/usr/lib/lib64 -Wl,-rpath,/usr/lib/lib:/usr/lib/lib64:/usr/lib/powerpc-linux-gnu -leccodes -leccodes_f90 /usr/lib/powerpc-linux-gnu/libpng.so /usr/lib/powerpc-linux-gnu/libz.so /usr/lib/powerpc-linux-gnu/libaec.so /usr/lib/powerpc-linux-gnu/libm.so /usr/lib/powerpc-linux-gnu/libopenjp2.so /usr/lib/powerpc-linux-gnu/libpthread.so -Wl,-rpath,/usr/include/../lib64:/usr/include/../lib"
GRIBAPI_INC=" -I/usr/lib/include -I/usr/include -I/usr/lib/python3/dist-packages/numpy/core/include -I/usr/include/openjpeg-2.1"
MV_EMOSLIB=" /usr/lib/powerpc-linux-gnu/libemosR64.a"
LDFLAGS_INLINE=""
IMP_FORT_LIBS="-lgcc  -lgcc_s  -lc  -lgcc  -lgcc_s  -lm  -lgcc  -lgcc_s  -lm  -lgfortran  "
EMOSLIB_TPLS=" -L/usr/lib/powerpc-linux-gnu -Wl,-rpath,/usr/lib/powerpc-linux-gnu /usr/lib/powerpc-linux-gnu/libfftw3.so -leccodes -leccodes_f90 -L/usr/lib/powerpc-linux-gnu -Wl,-rpath,/usr/lib/powerpc-linux-gnu /usr/lib/powerpc-linux-gnu/libpng.so -L/usr/lib/powerpc-linux-gnu -Wl,-rpath,/usr/lib/powerpc-linux-gnu /usr/lib/powerpc-linux-gnu/libz.so -L/usr/lib/powerpc-linux-gnu -Wl,-rpath,/usr/lib/powerpc-linux-gnu /usr/lib/powerpc-linux-gnu/libaec.so -L/usr/lib/powerpc-linux-gnu -Wl,-rpath,/usr/lib/powerpc-linux-gnu /usr/lib/powerpc-linux-gnu/libm.so -L/usr/lib/powerpc-linux-gnu -Wl,-rpath,/usr/lib/powerpc-linux-gnu /usr/lib/powerpc-linux-gnu/libopenjp2.so"

MV_EMOSLIB=${MV_EMOSLIB:="-L/usr/local/lib -lemosR64"}


# installed version with own-built GRIB_API - make sure we pick up the right GRIB libs
case $METVIEW_VVERSION in
 prod)

    if [ "x$MV_BUNDLE_TYPE" == "xMMG" -o "x$MV_BUNDLE_SRC" != "x" ]
    then
        GRIBAPI_INC="-I$METVIEW_DDIR/include $GRIBAPI_INC"
        GRIBAPI_LIB="-L$METVIEW_DDIR/$LIBNAME -Wl,-rpath,/$METVIEW_DDIR/$LIBNAME $GRIBAPI_LIB"
    fi
	;;
esac



#-- Note that we end up with a duplicated '-lgrib_api' in $FLIBS, but this is because this needs to
#-- come after the fortran library (and it also exists within $GRIBAPI_LIB).

FLIB="-L$METVIEW_DDIR/${LIBNAME} -Wl,-rpath,$METVIEW_DDIR/${LIBNAME} -lmacro_api_f90 -lmacro_api_c -lMvMarsStatic  $MACRO_EXTRA_LIBS $MV_EMOSLIB $GRIBAPI_INC $GRIBAPI_LIB $EMOSLIB_TPLS $LDFLAGS_INLINE -lm -lc "
CLIB="$MACRO_EXTRA_LIBS $GRIBAPI_INC -L$METVIEW_DDIR/${LIBNAME} -Wl,-rpath,$METVIEW_DDIR/${LIBNAME} -lmacro_api_c -lMvMarsStatic  $MV_EMOSLIB $GRIBAPI_LIB $LDFLAGS_INLINE -lm $FC_LIBS   $EMOSLIB_TPLS $IMP_FORT_LIBS"
CINC="$MACRO_EXTRA_INCS $GRIBAPI_INC -I$METVIEW_DDIR/include"
MX77="$MACRO_EXTRA_F77_FLAGS"
MX90="$MACRO_EXTRA_F90_FLAGS"
MXCC="$MACRO_EXTRA_CC_FLAGS"

cd $METVIEW_TMPDIR

#-- to set trace use macro function 'trace(n)' where n>1

trace=0

if [ "$1" = "-d" ]
then
	trace=1
	set -x
	shift
fi

type="$1"
in="$2"
out="$3"

#-- uncomment if you need to debug...
#set -x

case "$type" in

	shell)
		## Simply copy input to output and make executable
		ln -f "$in" "$out"
		chmod +x "$out"
		;;

	fortran|f77)
		ln -s -f "$in" compile$$.f
		$F77 -o "$out" $F77_FLAGS $MX77 compile$$.f $FLIB
		[ x$MACRO_FORTRAN_DBG != x ] ||  rm -f compile$$.f
		;;

	fortran90|f90)
		ln -s -f "$in" compile$$.f90
		$F90 -o "$out" $F90_FLAGS $MX90 compile$$.f90 $FLIB
		[ x$MACRO_FORTRAN_DBG != x ] || rm -f compile$$.f90
		;;

	C|c)
		ln -s -f "$in" compile$$.c
		$CXX -o "$out" $CC_FLAGS $MXCC $CINC compile$$.c $CLIB
		[ x$MACRO_FORTRAN_DBG != x ] || rm -f compile$$.c
		;;

	C++|c++)
		ln -s -f "$in" compile$$.cc
		$CXX -o "$out" $CXX_FLAGS $MXCC $CINC compile$$.cc $CLIB
		[ x$MACRO_FORTRAN_DBG != x ] || rm -f compile$$.c
		;;

	*)
		# Consider the string as a command
		cat <<@ > "$out"
#!/bin/bash


cat <<\end_of_inline_macro$$ > $METVIEW_TMPDIR/\$\$
@
		cat "$in" >> "$out"
		cat <<@ >> "$out"
end_of_inline_macro$$
$type $METVIEW_TMPDIR/\$\$
e=\$?
rm -f $METVIEW_TMPDIR/\$\$
exit \$e
@
		;;

esac
