# autoconf file for CODA
AC_INIT([CODA],[2.25])
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(CODA_VERSION, ["$VERSION"], [This is the current version of coda])

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

AH_TOP([
#ifndef CODA_CONFIG_H
#define CODA_CONFIG_H
])

AH_BOTTOM([
#ifdef HAVE_PREAD
/* some platforms (e.g. Linux) require this definition in order to enable support for pread */
/* Use 600 instead of 500 to prevent Mac OS X to complain about missing strdup definitions */
#define _XOPEN_SOURCE 600
#endif

#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 1.6 compatibility API for HDF 1.8 */
#define H5_USE_16_API

/* Make sure we build the PCRE library statically on Windows */
#define PCRE2_STATIC

#ifdef WIN32

/* include windows specific headers */
#include <windows.h>
#include <io.h>
#include <direct.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

/* we need to redefine ELEMENT_TYPE because it conflicts with io.h contents */
#define ELEMENT_TYPE ELEMENT_TYPE_RENAMED

/* redefines for special string handling functions */
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#if defined(_MSC_VER) && _MSC_VER < 1400
#define snprintf _snprintf
#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 YYMALLOC malloc
#define YYFREE free

#endif

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

# Other files generated by configure
AC_CONFIG_HEADERS([coda.h:libcoda/coda.h.in])
AC_CONFIG_FILES([Makefile fortran/Makefile])
AC_CONFIG_FILES([java/build.xml])
AC_CONFIG_FILES([libcoda/doc/Doxyfile java/doc/Doxyfile])
AC_CONFIG_FILES([idl/coda_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.
LIBCODA_CURRENT=15
LIBCODA_REVISION=7
LIBCODA_AGE=0
AC_SUBST(LIBCODA_CURRENT)
AC_SUBST(LIBCODA_REVISION)
AC_SUBST(LIBCODA_AGE)

# *** checks for programs ***

AC_PROG_CC
AC_PROG_F77

# 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

# check for fortran 77 compiler characteristics needed for wrapping c to fortran
ST_CHECK_WRAPFORTRAN_F77_OPTIONS

# 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(DOXYGEN, [The documentation generation program Doxygen])
if test "$DOXYGEN" = "" ; then
  AC_PATH_PROG([DOXYGEN], [doxygen])
fi

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

AC_ARG_VAR(SWIG, [The SWIG wrapper generation program])
if test "$SWIG" = "" ; then
  AC_PATH_PROG([SWIG], [swig])
fi
AC_SUBST(SWIGFLAGS)

# *** checks for libraries ****

ST_CHECK_LIB_M

# *** checks for header files ***

AC_CHECK_HEADERS([dirent.h unistd.h strings.h sys/socket.h sys/mman.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

# sets WORDS_BIGENDIAN
AC_C_BIGENDIAN

# defines _FILE_OFFSET_BITS and _LARGE_FILES if necessary
AC_SYS_LARGEFILE

# check for thread local storage specifier
ST_THREAD_LOCAL_STORAGE

# *** checks for structures ***

AC_STRUCT_TM

# *** checks for compiler characteristics ***

# check for warning flags
VL_PROG_CC_WARNINGS

# check for no-long-long warning flag
ST_PROG_CC_OPT([-Wno-long-long], [ac_cv_no_long_long_warning])
if test "$ac_cv_no_long_long_warning" != "" ; then
  CFLAGS="$CFLAGS $ac_cv_no_long_long_warning"
fi

# *** checks for library functions ***

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

# *** sub-package mode ***

AC_ARG_ENABLE([coda-subpackage-mode],
  [AS_HELP_STRING([--enable-coda-subpackage-mode],[only build the CODA library and codadd, but don't install anything for CODA. This is intended for when CODA is used as a sub-package within a larger software pacakge])],
  [ac_cv_enable_coda_subpackage_mode=$enableval],
  [AC_CACHE_CHECK([build CODA as sub-package],ac_cv_enable_coda_subpackage_mode,ac_cv_enable_coda_subpackage_mode=no)])
AM_CONDITIONAL(SUBPACKAGE_MODE, test $ac_cv_enable_coda_subpackage_mode = yes)

# *** directories ***

if test $ac_cv_enable_coda_subpackage_mode = yes ; then
  bindir=
  includedir=
  libdir=
  fortrandir=
  definitionsdir=
  pkgdatadir=
else
  fortrandir="\${pkgdatadir}/fortran"
  definitionsdir="\${pkgdatadir}/definitions"
  pkgdatadir="\${datadir}/$PACKAGE"
fi
AC_SUBST(fortrandir)
AC_SUBST(definitionsdir)
AC_SUBST(pkgdatadir)

# The directories for optional components start empty and will be set later

idldlmexecdir=
matlabmexexecdir=
codapythondir=
javadir=

AC_SUBST(idldlmexecdir)
AC_SUBST(matlabmexexecdir)
AC_SUBST(codapythondir)
AC_SUBST(javadir)

# *** coda-idl ***

if test $ac_cv_enable_coda_subpackage_mode = yes ; then
  ac_cv_enable_idl=no
else
  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)])
fi

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)

# *** coda-matlab ***

if test $ac_cv_enable_coda_subpackage_mode = yes ; then
  ac_cv_enable_matlab=no
else
  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)])
fi

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)

# *** coda-python ***

if test $ac_cv_enable_coda_subpackage_mode = yes ; then
  ac_cv_enable_python=no
else
  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)])
fi

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 full path to your python executable if you have Python installed.]))
  fi
  codapythondir="$pythondir/coda"
fi

AM_CONDITIONAL(BUILD_PYTHON, test $ac_cv_enable_python = yes)


# *** coda-java ***

if test $ac_cv_enable_coda_subpackage_mode = yes ; then
  ac_cv_enable_java=no
else
  AC_ARG_ENABLE([java],
    [AS_HELP_STRING([--enable-java],[build the Java interface])],
    [ac_cv_enable_java=$enableval],
    [AC_CACHE_CHECK([build Java interface], ac_cv_enable_java, ac_cv_enable_java=no)])
fi

if test $ac_cv_enable_java = yes ; then
  ST_CHECK_JAVA
  if test $st_cv_have_java = no ; then
    AC_MSG_ERROR(BOXED_TEXT([ERROR: The Sun Java Development Kit (JDK) is required to build the Java interface.
Try setting the JAVA and JAVAC environment variables to the full paths to your java and javac executables if you have Java installed.]))
  fi
  if test `uname -s` = Darwin; then
    JNIEXT=.jnilib
  else
    JNIEXT=.so
  fi
  if test `uname -s` = Darwin && ! test -e $JAVA_HOME/include; then
    if test -x /usr/bin/javaconfig; then
      JNICPPFLAGS=-I`javaconfig Headers`
    elif test -e /System/Library/Frameworks/JavaVM.framework/Headers; then # javaconfig does not exist on Mac 10.8
      JNICPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers
    fi
  else
    AC_CHECK_FILE($JAVA_HOME/include/jni.h, ,
      [AC_MSG_ERROR([BOXED_TEXT([ERROR: The Java Native Interface (jni.h) is required for the Java interface. Make sure you have the full JDK installed, not just the Runtime Environment (JRE).])])])
    JNICPPFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/"`uname -s | tr A-Z a-z`" $CPPFLAGS"
  fi
  AC_SUBST(JNICPPFLAGS)
  AC_SUBST(JNIEXT)
  javadir="\${pkgdatadir}/java"
fi

AM_CONDITIONAL(BUILD_JAVA, test $ac_cv_enable_java = yes)

# *** hdf ***

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

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
  AC_ARG_ENABLE(hdf4-vdata-attributes,
                [AS_HELP_STRING([--disable-hdf4-vdata-attributes],[use this option if you have HDF4.2r1 or earlier])],
                [ac_cv_enable_hdf4_vdata_attributes=$enableval],
                [AC_CACHE_CHECK([enable HDF4 Vdata attributes], ac_cv_enable_hdf4_vdata_attributes,
                                 ac_cv_enable_hdf4_vdata_attributes=yes)])
  if test $ac_cv_enable_hdf4_vdata_attributes = yes ; then
    AC_DEFINE([ENABLE_HDF4_VDATA_ATTRIBUTES], 1, [Define to 1 if HDF4 Vdata/Vgroup attributes can be used (only enable if CODA is linked against HDF4 library version 4.2r2 or higher).])
  fi
fi
AC_SUBST(HDF4LIBS)
AM_CONDITIONAL(WITH_HDF4, test $ac_cv_with_hdf4 = yes)

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

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)

# *** 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_DEFINE([CODA_EXPAT_NAME_MANGLE], 1, [Define to enable name mangling of symbols for the built-in expat library.])

# *** extra ***

AC_ARG_ENABLE([printf-warnings],
  [AS_HELP_STRING([--enable-printf-warnings],[enables compiler warnings for the printf-like functions in CODA. This only works if compiling with the GNU compiler. Don't use this option for official installations of CODA (since this will prevent coda.h being used by other compilers).])],
  [ac_cv_enable_printf_warnings=$enableval],
  [AC_CACHE_CHECK([enable printf warnings],ac_cv_enable_printf_warnings,ac_cv_enable_printf_warnings=no)])

if test $ac_cv_enable_printf_warnings = yes && test "$GCC" = yes ; then
  AC_DEFINE(PRINTF_ATTR,
            [__attribute__ ((format(printf, 2, 3)))],
            [Define to the compiler attribute to test for printf format compliance])
fi

AC_OUTPUT
