#!/bin/ksh
# --- head created by Make ---
set -e
CC=gcc
CC_FLAGS=" -g -O0 -I/usr/local/apps/python/2.7.1-02/include/python2.5  -m64 -fPIC -DECMWF -DLITTLE -DLITTLE_END -DH_INCLUDES_CC -DR64 -DREAL_8 -DREAL_BIGGER_THAN_INTEGER -DI32 -DMAGINT_IS_INT -DREAL_IS_DOUBLE"
CXX=g++
CXX_FLAGS="-g -O0  -Wall -W -DMETVIEW_EXPERIMENTAL -DUSE_NEW_IO -m64 -fPIC -fno-gnu-keywords -Wno-deprecated -Wno-write-strings -DMETVIEW_QT -DECMWF -DLITTLE -DLITTLE_END -DH_INCLUDES_CC -DR64 -DREAL_8 -DREAL_BIGGER_THAN_INTEGER -DI32 -DMAGINT_IS_INT -DREAL_IS_DOUBLE"
F77=pgf90
F77_FLAGS="-g -r8 -tp amd64 -r8"
F90=pgf90
F90_FLAGS="-g -r8 -tp amd64 -r8"
FC_LIBS="-L/usr/local/apps/pgi/pgi-10.8/linux86-64/10.8/libso -Wl,-rpath,/usr/local/apps/pgi/pgi-10.8/linux86-64/10.8/libso -Wl,-rpath,/usr/local/apps/pgi/pgi-10.8/linux86-64/10.8/libso -lpgf90rtl -lpgftnrtl -lrt -lpgf90 -lpgf902 -lpghpf -lpgf90_rpm1 -lpghpf2 -lpgc -lpgftnrtl -lpgc -lrt"
LIBNAME="lib64"
GRIBAPI_LIB="-L/usr/local/lib/metaps/lib/grib_api/1.12.3/lib64 -lgrib_api -ljasper"
GRIBAPI_INC="-I/usr/local/lib/metaps/lib/grib_api/1.12.3/include64"
MV_EMOSLIB="-L/usr/local/apps/libemos/000395/lib -lemos.R64.D64.I32"
LDFLAGS_INLINE="-L/usr/local/apps/python/2.7.1-02/lib"
# **************************** LICENSE START ***********************************
#
# Copyright 2012 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 ************************************

# --------------------------------------- head above created by 'make'
# --------------------------------------- rest below is scripts/compile.src
# 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!

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

#-- Warning: here Mars and Emos library names are hardcoded!!
#-- 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_DIR/${LIBNAME} -lmacro_api_f90 -lMars $MACRO_EXTRA_LIBS $MV_EMOSLIB $GRIBAPI_INC $GRIBAPI_LIB $LDFLAGS_INLINE -lgrib_api_f90 -lgrib_api -lm -lc"
CLIB="$MACRO_EXTRA_LIBS $GRIBAPI_INC -L$METVIEW_DIR/${LIBNAME} -lMars $MV_EMOSLIB $GRIBAPI_LIB $LDFLAGS_INLINE -lm $FC_LIBS"
CINC="$MACRO_EXTRA_INCS $GRIBAPI_INC -I$METVIEW_DIR/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
		$CC -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/ksh


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
