#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.64])
AC_INIT([FULL-PACKAGE-NAME], 1.2.0, [BUG-REPORT-ADDRESS])

AM_INIT_AUTOMAKE(hdf_tools, 1.2.0)

AC_CONFIG_SRCDIR([config.h.in])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

# Checks for libraries.

# -------------------- dependencies of HDF4 -------------------
# zlib, jpeg, szip [OPTIONAL]
AC_CHECK_LIB( z,    compress2,             HAVE_LIBZ="yes"    , AC_MSG_ERROR([zlib not found]) )
if test "$HAVE_LIBZ" == "yes"; then
	LIBS="-lz $LIBS"
fi

AC_CHECK_LIB( jpeg, jpeg_start_decompress, HAVE_LIBJPEG="yes" , AC_MSG_ERROR([jpeg not found]) )
if test "$HAVE_LIBJPEG" == "yes"; then
	LIBS="-ljpeg $LIBS"
fi

# SZIP is OPTIONAL
AC_CHECK_LIB( sz,   SZ_BufftoBuffCompress, HAVE_LIBSZ="yes"   , HAVE_LIBSZ="no" )

# -----------------------------------------
# --- Look for HDF4 headers and library ---
# -----------------------------------------
HAVE_HDF4="no"

# --- HDF4 headers ---
# 1) Try to read it the environment variables
AC_SUBST(HDFINC, `echo $HDFINC`)
# 2) Try to read it from the configure comand line arguments
withval="";
AC_ARG_WITH( hdf4-inc,
             [  --with-hdf4-inc=HDF4_INCLUDE_DIR     Path to the hdf includes directory, that contains mfhdf.h (required if not in default path or not set in the HDFINC environment variable)],
             HDFINC="$withval",)

if test -n "$HDFINC"; then
	CPPFLAGS="-I$HDFINC $CPPFLAGS"
fi

# Checks for header files.
AC_CHECK_HEADERS([mfhdf.h hdf.h],,AC_MSG_ERROR(cannot find mfhdf.h or hdf.h in hdf4))
HAVE_HDF4_HEADERS="yes"

#---  Check that optional HDF4 libraries that HDF4 needs have been found ---
needszip=`grep 'define H4_HAVE_LIBSZ 1' $HDFINC/h4config.h`
if test -n "$needszip"; then
	if test "$HAVE_SZIP" == "yes"; then
			LIBS="-lsz $LIBS"
	else
			AC_MSG_ERROR([ HDF4 library needs SZIP, SZIP not defined.])
	fi
fi

# --- HDF4 libraries ---
# 1) Try to read it the environment variables
AC_SUBST(HDFLIB, `echo $HDFLIB`)
# 2) Try to read it from the configure comand line arguments
withval="";
AC_ARG_WITH(hdf4-lib,[ --with-hdf4-lib=HDF4_LIBRARY_DIR     Path to the hdf libraries directory, that contains libhdf.a (required if not in default path or not set in the HDFLIB environment variable)],
             HDFLIB="$withval",)

if test -n "$HDFLIB"; then
	LIBS="-L$HDFLIB $LIBS"
fi

# Checks for libraries
AC_CHECK_LIB(df, Hopen,,AC_MSG_ERROR([invalid df in hdf4]))
AC_CHECK_LIB(mfhdf, hdf_read_attrs,, AC_MSG_ERROR([invalid mfhdf in hdf4]))
HAVE_HDF4_LIBS="yes"
HAVE_HDF4="yes"

# --------------------------------------------------------------
# --- Check if NETCDF support is enabled in the HDF4 library ---
# --------------------------------------------------------------

AC_MSG_CHECKING([NETCDF support in HDF4])
supportsnetcdf=`grep '#define H4_HAVE_NETCDF 1' $HDFINC/h4config.h`
if test -z "$supportsnetcdf"; then
	AC_MSG_ERROR([ NETCDF support not enabled in HDF4 library])
else
	AC_MSG_RESULT([OK])
fi


# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([memset strrchr strstr])

AC_CONFIG_FILES([Makefile
                 include/Makefile
                 src/Makefile])
AC_OUTPUT
