# autoconf file for HARP
AC_INIT([harp],[1.20.1])
AC_CONFIG_AUX_DIR([.])
AM_INIT_AUTOMAKE(foreign subdir-objects 1.12 -Wall)

AC_PREREQ(2.63)

AC_CONFIG_MACRO_DIR([m4])

# set extra version variable
AC_DEFINE_UNQUOTED(HARP_VERSION, ["$VERSION"], [This is the current version of HARP])

# autoheader config include
AC_CONFIG_HEADERS([config.h])

AH_TOP([
#ifndef HARP_CONFIG_H
#define HARP_CONFIG_H
])

AH_BOTTOM([
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#else
# ifdef HAVE_STDINT_H
#  include <stdint.h>
# else
#  ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#  endif
# endif
#endif

/* needed for expat */
#ifdef WORDS_BIGENDIAN
#define BYTEORDER 4321
#else
#define BYTEORDER 1234
#endif
#define XML_DEV_URANDOM 1

#ifndef HAVE_STRDUP
char *strdup(const char *);
#endif

#ifndef HAVE_STRCASECMP
int strcasecmp(const char *, const char *);
#endif

#if !defined(HAVE_STRNCASECMP) || !defined(HAVE_VSNPRINTF)
/* include string.h for size_t definition */
#include <string.h>
#endif

#ifndef HAVE_STRNCASECMP
int strncasecmp(const char *, const char *, size_t);
#endif

#if defined(HAVE_STRCASECMP) || defined(HAVE_STRNCASECMP)
#ifdef HAVE_STRINGS_H
/* include strings.h for definition of strcasecmp and strncasecmp */
#include <strings.h>
#endif
#endif

#ifndef HAVE_VSNPRINTF
/* include stdarg.h for va_list definition */
#include <stdarg.h>
int vsnprintf(const char *, size_t, const char *, va_list ap);
#endif

/* Make sure we use the HDF 1.6 compatibility API */
#define H5_USE_16_API

/* H5Oget_info did not exist in HDF 1.6, so make sure to use 1.8 for this */
#define H5Oget_info_vers 1
#define H5Oget_info_by_name_vers 1
#define H5O_info_t_vers 1

#ifdef WIN32

/* need to include this before we redefine lseek, etc. */
#include <io.h>

/* include sys/stat.h because we are going to override stat */
#include <sys/stat.h>

#if (_MSC_VER < 1600) && !defined(__MINGW32__)
/* For Visual Studio > 2010 and MinGW we can use stdint.h
 * For earlier versions of Visual Studio we need to provide our own defines
 */
#ifndef int8_t
#define int8_t  signed char
#endif
#ifndef int16_t
#define int16_t signed short
#endif
#ifndef int32_t
#define int32_t int
#endif
#ifndef int64_t
#define int64_t __int64
#endif

#ifndef uint8_t
#define uint8_t  unsigned char
#endif
#ifndef uint16_t
#define uint16_t unsigned short
#endif
#ifndef uint32_t
#define uint32_t unsigned int
#endif
#ifndef uint64_t
#define uint64_t unsigned __int64
#endif
#endif

/* redefines for special string handling functions */
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf _snprintf
#endif
#if defined(_MSC_VER) && _MSC_VER < 1500
#define vsnprintf _vsnprintf
#endif

/* redefines for file handling functions */
#define open(arg1,arg2) _open(arg1,arg2)
#define close(arg1) _close(arg1)
/* WARNING: On Windows we can't read blocks larger than 4GB with the read() function */
#define read(arg1,arg2,arg3) _read(arg1,arg2,(unsigned int)arg3)
#define lseek(arg1,arg2,arg3) _lseeki64(arg1,arg2,arg3)
#define off_t __int64
#define stat _stati64
#define S_IFREG _S_IFREG
#define getcwd(arg1,arg2) _getcwd(arg1,arg2)

/* define ssize_t */
#ifdef _WIN64
#define ssize_t __int64
#else
#define ssize_t long
#endif

/* prevent HDF5 header files from redefining the above types */
#define H5_SIZEOF_INT64_T 8
#define H5_SIZEOF_UINT32_T 4
#define H5_SIZEOF_UINT64_T 8
#ifdef _WIN64
#define H5_SIZEOF_SSIZE_T 8
#else
#define H5_SIZEOF_SSIZE_T 4
#endif

#define YYMALLOC malloc
#define YYFREE free

#endif

#endif /* !defined(HARP_CONFIG_H) */
])

# autoheader config include
AC_CONFIG_HEADERS([libharp/harp.h])

# Other files generated by configure
AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([doc/Doxyfile])
AC_CONFIG_FILES([R/DESCRIPTION])
AC_CONFIG_FILES([R/create_rds.r])
AC_CONFIG_FILES([idl/harp_idl.dlm])

# Set dynamic library version

# Rules to update the library version information:
# 1. Start with version information of `0:0:0' for each libtool library.
# 2. Update the version information only immediately before a public release.
# 3. If the library source code has changed at all since the last update,
#    then increment revision (`c:r:a' becomes `c:r+1:a').
# 4. If any interfaces have been added, removed, or changed since the last
#    update, increment current, and set revision to 0.
# 5. If any interfaces have been added since the last public release, then
#    increment age.
# 6. If any interfaces have been removed or changed since the last public
#    release, then set age to 0.
LIBHARP_CURRENT=14
LIBHARP_REVISION=8
LIBHARP_AGE=1
AC_SUBST(LIBHARP_CURRENT)
AC_SUBST(LIBHARP_REVISION)
AC_SUBST(LIBHARP_AGE)

# HARP format version
HARP_FORMAT_VERSION_MAJOR=1
HARP_FORMAT_VERSION_MINOR=0
AC_SUBST(HARP_FORMAT_VERSION_MAJOR)
AC_SUBST(HARP_FORMAT_VERSION_MINOR)
AC_DEFINE_UNQUOTED(HARP_FORMAT_VERSION_MAJOR, [$HARP_FORMAT_VERSION_MAJOR], [Current HARP format major version number.])
AC_DEFINE_UNQUOTED(HARP_FORMAT_VERSION_MINOR, [$HARP_FORMAT_VERSION_MINOR], [Current HARP format minor version number.])
AC_DEFINE_UNQUOTED(HARP_CONVENTION, ["HARP-$HARP_FORMAT_VERSION_MAJOR.$HARP_FORMAT_VERSION_MINOR"], [String to use in netCDF Conventions attribute])

AC_DEFINE([HARP_EXPAT_NAME_MANGLE], 1, [Define to enable name mangling of symbols for the built-in expat library.])
AC_DEFINE([HARP_NETCDF_NAME_MANGLE], 1, [Define to enable name mangling of symbols for the built-in netcdf library.])
AC_DEFINE([HARP_UDUNITS2_NAME_MANGLE], 1, [Define to enable name mangling of symbols for the built-in udunits2 library.])

# codadef versions
AC_SUBST(CODADEFSH, $srcdir/codadef.sh)
ST_CODADEF_VERSION([definitions],[ACE_FTS])
ST_CODADEF_VERSION([definitions],[ACSAF])
ST_CODADEF_VERSION([definitions],[AEOLUS])
ST_CODADEF_VERSION([definitions],[AURA_HIRDLS])
ST_CODADEF_VERSION([definitions],[AURA_MLS])
ST_CODADEF_VERSION([definitions],[AURA_OMI])
ST_CODADEF_VERSION([definitions],[AURA_TES])
ST_CODADEF_VERSION([definitions],[CALIPSO])
ST_CODADEF_VERSION([definitions],[CLOUDNET])
ST_CODADEF_VERSION([definitions],[EARLINET])
ST_CODADEF_VERSION([definitions],[EARTHCARE])
ST_CODADEF_VERSION([definitions],[ECMWF])
ST_CODADEF_VERSION([definitions],[ENVISAT_GOMOS])
ST_CODADEF_VERSION([definitions],[ENVISAT_MIPAS])
ST_CODADEF_VERSION([definitions],[ENVISAT_SCIAMACHY])
ST_CODADEF_VERSION([definitions],[EPS])
ST_CODADEF_VERSION([definitions],[ERS_GOME])
ST_CODADEF_VERSION([definitions],[ESACCI_AEROSOL])
ST_CODADEF_VERSION([definitions],[ESACCI_CLOUD])
ST_CODADEF_VERSION([definitions],[ESACCI_GHG])
ST_CODADEF_VERSION([definitions],[ESACCI_OZONE])
ST_CODADEF_VERSION([definitions],[GEOMS])
ST_CODADEF_VERSION([definitions],[GOSAT])
ST_CODADEF_VERSION([definitions],[NPP_SUOMI])
ST_CODADEF_VERSION([definitions],[OCO])
ST_CODADEF_VERSION([definitions],[ODIN_OSIRIS])
ST_CODADEF_VERSION([definitions],[ODIN_SMR])
ST_CODADEF_VERSION([definitions],[QA4ECV])
ST_CODADEF_VERSION([definitions],[S5P_PAL])
ST_CODADEF_VERSION([definitions],[Sentinel5P])
ST_CODADEF_VERSION([definitions],[TEMIS])

# *** checks for programs ***

AC_PROG_CC

# AM_PROG_AR is only available since automake 1.11.2
m4_define_default([AM_PROG_AR])
AM_PROG_AR
AM_PROG_CC_C_O

# configure libtool
LT_INIT
AC_SUBST(LIBTOOL_DEPS)

# lex and yacc
AM_PROG_LEX
AC_PROG_YACC

# install tool
AC_PROG_INSTALL

AC_PROG_AWK

AM_PATH_PYTHON

AC_ARG_VAR(R, [The R statistical software program])
if test "$R" = "" ; then
  AC_PATH_PROG([R], [R])
fi

AC_ARG_VAR(DOXYGEN, [The documentation generation program Doxygen])
if test "$DOXYGEN" = "" ; then
  AC_PATH_PROG([DOXYGEN], [doxygen])
fi

AC_ARG_VAR(SPHINXBUILD, [The documentation generation program sphinx-build])
if test "$SPHINXBUILD" = "" ; then
  AC_PATH_PROG([SPHINXBUILD], [sphinx-build])
fi

AC_ARG_VAR(INDENT, [The GNU indent program])
if test "$INDENT" = "" ; then
  AC_PATH_PROG([INDENT], [indent], :)
fi

# *** checks for libraries ****

ST_CHECK_LIB_M

# *** checks for header files ***

AC_HEADER_STDBOOL
AC_CHECK_HEADERS([dirent.h unistd.h strings.h])

# *** checks for types ***

AC_TYPE_SIZE_T

AC_TYPE_INT8_T
AC_TYPE_UINT8_T
AC_TYPE_INT16_T
AC_TYPE_UINT16_T
AC_TYPE_INT32_T
AC_TYPE_UINT32_T
AC_TYPE_INT64_T
AC_TYPE_UINT64_T

AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(float)
AC_CHECK_SIZEOF(double)
AC_CHECK_SIZEOF(off_t)
AC_CHECK_SIZEOF(size_t)

# sets WORDS_BIGENDIAN
AC_C_BIGENDIAN

# defines _FILE_OFFSET_BITS and _LARGE_FILES if necessary
AC_SYS_LARGEFILE

# *** checks for structures ***

AC_STRUCT_TM

# *** checks for compiler characteristics ***

# check for warning flags
VL_PROG_CC_WARNINGS

# *** checks for library functions ***

AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([floor pread stat memmove bcopy strerror])
AC_REPLACE_FUNCS([strdup strcasecmp strncasecmp vsnprintf])

# *** directories ***

examplesdir="\${pkgdatadir}/examples"

# Directories for optional components start empty and will be set later.
harppythondir=
harprdir=
idldlmexecdir=
matlabmexexecdir=
codamatlabexamplesdir=
harpmatlabexamplesdir=
definitionsdir="\${datadir}/coda/definitions"

AC_SUBST(examplesdir)

AC_SUBST(harppythondir)
AC_SUBST(harprdir)
AC_SUBST(harprlibdir)
AC_SUBST(harprMetadir)
AC_SUBST(harprRdir)
AC_SUBST(idldlmexecdir)
AC_SUBST(matlabmexexecdir)
AC_SUBST(codamatlabexamplesdir)
AC_SUBST(harpmatlabexamplesdir)
AC_SUBST(definitionsdir)

# *** harp-python ***

AC_ARG_ENABLE([python],
  [AS_HELP_STRING([--enable-python],[build the Python interface])],
  [ac_cv_enable_python=$enableval],
  [AC_CACHE_CHECK([build Python interface], ac_cv_enable_python, ac_cv_enable_python=no)])

if test $ac_cv_enable_python = yes ; then
  if test -z "$PYTHON" ; then
    AC_MSG_ERROR(BOXED_TEXT([ERROR: Python is required to build the Python interface.
Try setting the PYTHON environment variable to the location of your Python interpreter.]))
  fi
  harppythondir="$pythondir/harp"
fi

AM_CONDITIONAL(BUILD_PYTHON, test $ac_cv_enable_python = yes)

# *** harp-R ***

AC_ARG_ENABLE([R],
  [AS_HELP_STRING([--enable-R],[build the R interface])],
  [ac_cv_enable_R=$enableval],
  [AC_CACHE_CHECK([build R interface], ac_cv_enable_R, ac_cv_enable_R=no)])

if test $ac_cv_enable_R = yes ; then
  if test $ac_cv_path_R = no ; then
    AC_MSG_ERROR(BOXED_TEXT([ERROR: R is required to build the R interface.
Try setting the R environment variable to the location of your R program.]))
  fi
  
  RHOME=`${R} RHOME`
  RCPPFLAGS=-I$RHOME/include

  harprdir="$libdir/R/site-library/harp"
  harprlibdir="\${harprdir}/libs"
  harprMetadir="\${harprdir}/Meta"
  harprRdir="\${harprdir}/R"
fi

AC_SUBST(RCPPFLAGS)
AM_CONDITIONAL(BUILD_R, test $ac_cv_enable_R = yes)

# *** harp-idl ***

AC_ARG_ENABLE(idl,
              [AS_HELP_STRING([--enable-idl],[build the IDL interface])],
              [ac_cv_enable_idl=$enableval],
              [AC_CACHE_CHECK([build IDL interface], ac_cv_enable_idl, ac_cv_enable_idl=no)])

if test $ac_cv_enable_idl = yes ; then
  ST_CHECK_IDL
  if test $st_cv_have_idl = no ; then
    AC_MSG_ERROR(BOXED_TEXT([ERROR: IDL is required to build the IDL interface.
Try setting the IDL environment variable to the root of your IDL installation
if you have IDL installed.]))
  fi
  idldlmexecdir="\${pkglibdir}/idl"
fi

AM_CONDITIONAL(BUILD_IDL, test $ac_cv_enable_idl = yes)

# *** harp-matlab ****

# check whether we should build the MATLAB interface
AC_ARG_ENABLE([matlab],
              [AS_HELP_STRING([--enable-matlab],[build the MATLAB interface])],
              [ac_cv_enable_matlab=$enableval],
              [AC_CACHE_CHECK([build MATLAB interface], ac_cv_enable_matlab, ac_cv_enable_matlab=no)])

if test $ac_cv_enable_matlab = yes ; then
  ST_CHECK_MATLAB
  if test $st_cv_have_matlab = no ; then
    AC_MSG_ERROR(BOXED_TEXT([ERROR: MATLAB is required to build the MATLAB interface.
Try setting the MATLAB environment variable to the root of your MATLAB
installation if you have MATLAB installed.]))
  fi
  matlabmexexecdir="\${pkglibdir}/matlab"
fi

AM_CONDITIONAL(BUILD_MATLAB, test $ac_cv_enable_matlab = yes)

# *** HDF4 ***

AC_ARG_WITH([hdf4],
  [AS_HELP_STRING([--with-hdf4],[build HDF4 support into HARP])],
  [ac_cv_with_hdf4=$withval],
  [AC_CACHE_CHECK([use HDF4], ac_cv_with_hdf4, ac_cv_with_hdf4=yes)])

if test $ac_cv_with_hdf4 = yes ; then
  ST_CHECK_HDF4
  if test $st_cv_have_hdf4 = no ; then
    AC_MSG_ERROR(BOXED_TEXT([ERROR: HDF4 libraries and/or header files are not found.
Try setting the HDF4_LIB and HDF4_INCLUDE environment variables to the
location of your HDF4 library and include files.]))
  fi
fi
AC_SUBST(HDF4LIBS)
AM_CONDITIONAL(WITH_HDF4, test $ac_cv_with_hdf4 = yes)

# *** HDF5 ***

AC_ARG_WITH([hdf5],
  [AS_HELP_STRING([--with-hdf5],[build HDF5 support into HARP])],
  [ac_cv_with_hdf5=$withval],
  [AC_CACHE_CHECK([use HDF5],ac_cv_with_hdf5,ac_cv_with_hdf5=yes)])

if test $ac_cv_with_hdf5 = yes ; then
  ST_CHECK_HDF5
  if test $st_cv_have_hdf5 = no ; then
    AC_MSG_ERROR(BOXED_TEXT([ERROR: HDF5 libraries and/or header files are not found.
Try setting the HDF5_LIB and HDF5_INCLUDE environment variables to the
location of your HDF5 library and include files.]))
  fi
fi
AC_SUBST(HDF5LIBS)
AM_CONDITIONAL(WITH_HDF5, test $ac_cv_with_hdf5 = yes)

# *** CODA ***

# make sure to perform this check after checking for HDF4/HDF5
ST_CHECK_CODA
if test $st_cv_have_coda = no ; then
  AC_MSG_ERROR(BOXED_TEXT([ERROR: CODA library and/or header file not found.
Try setting the CODA_LIB and CODA_INCLUDE environment variables to the
location of your CODA library and include file.]))
fi
AC_SUBST(CODALIBS)

# *** udunits2/xml ****

AC_DEFINE([XML_NS], 1, [Define to make XML Namespaces functionality available.])
AC_DEFINE([XML_DTD], 1, [Define to make parameter entity parsing functionality available.])
AC_DEFINE([XML_LARGE_SIZE], 1, [Define to set the size of the XML_Size and XML_Index integer types to be at least 64 bits in size.])
AC_DEFINE([XML_CONTEXT_BYTES], 1024, [Define to specify how much context to retain around the current parse point.])

AC_OUTPUT
