# CAPABILITY TESTING MACROS
m4_define(
	[M4_HDF5_LIB],
	[cat > nptm_test_hdf5.cpp <<EOF
#include <hdf5.h>
int main(int argc, char **argv) {
  hid_t file_id = 0;
  herr_t status = H5Iis_valid(file_id);
  return (int)status;
}
EOF
	$CXX -o nptm_test_hdf5 nptm_test_hdf5.cpp -I$HDF5_INCLUDE -L$HDF5_LIB -lhdf5 > /dev/null 2>>error.log
	export TEST_HDF5_LIB=$?
	rm nptm_test_hdf5.cpp
	if test "x$TEST_HDF5_LIB" = "x0"; then
	  rm nptm_test_hdf5
	fi]
)
  
# END CAPABILITY TESTING MACROS

# autoconf setup initialization
AC_INIT([np_tmcode], [8.04], [giovanni.lamura@inaf.it])

# Folder structure safety check
AC_CONFIG_SRCDIR([../src/libnptm/TransitionMatrix.cpp])

# Auxiliary build folder
AC_CONFIG_AUX_DIR([build_aux])

# automake initialization
AM_INIT_AUTOMAKE([-Wall -Werror foreign])

# Compiler detection
AM_PROG_AR
AC_PROG_F77([mpif90 gfortran f77])
AC_PROG_CXX([mpicxx g++ clang])

# Libtool initialization
LT_INIT

# Environment setup
AS_IF(
	[test "x$HDF5_INCLUDE" = "x"],
	[export HDF5_INCLUDE=/usr/include/hdf5/serial],
	[AC_MSG_NOTICE(["HDF5_INCLUDE=$HDF5_INCLUDE"])]
)
AS_IF(
	[test "x$HDF5_LIB" = "x"],
	[export HDF5_LIB=/usr/lib/x86_64-linux-gnu/hdf5/serial],
	[AC_MSG_NOTICE(["HDF5_LIB=$HDF5_LIB"])]
)
FFLAGS="-std=legacy -O3"
CXXFLAGS="-O3 -ggdb -Wno-format-contains-nul -I $HDF5_INCLUDE"
SUBDIRS="cluster libnptm sphere testing trapping"

# Check for required headers
AC_CHECK_HEADER(
	[$HDF5_INCLUDE/hdf5.h],
	,
	AC_MSG_ERROR(["Could not find HDF5 headers!]),
	)

# Check for required libraries
M4_HDF5_LIB
AS_IF(
	[test "x$TEST_HDF5_LIB" = "x0"],
	[export HDF5_LDFLAGS="-L${HDF5_LIB} -lhdf5"],
	[AC_MSG_ERROR(["HDF5 library not found!"])]
)
AC_SUBST([HDF5_LDFLAGS])

# Send output to Makefiles
AC_CONFIG_FILES([Makefile])

# Generate the output
AC_OUTPUT