dnl The configure script is generated by autogen.sh from configure.base 
dnl and the various configure.add files in the source tree.  Edit 
dnl configure.base and reprocess rather than modifying ./configure.

### Copyright (C) 2018-2023 John Donoghue <john.donoghue@ieee.org>
###
### This program is free software; you can redistribute it and/or
### modify it under the terms of the GNU General Public License as
### published by the Free Software Foundation; either version 3 of the
### License, or (at your option) any later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
### General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, see
### <http://www.gnu.org/licenses/>.

dnl autoconf 2.13 certainly doesn't work! What is the minimum requirement?
AC_PREREQ(2.2)

AC_INIT([octave netcdf package],[1.0.17])
AC_CONFIG_HEADERS([config.h])

# Avoid warnings for redefining AH-generated preprocessor symbols of
# Octave.
AH_TOP([#include "undef-ah-octave.h"])

AC_CONFIG_MACRO_DIRS([m4])

AC_CANONICAL_HOST
AC_CANONICAL_TARGET

dnl Kill caching --- this ought to be the default
define([AC_CACHE_LOAD], )dnl
define([AC_CACHE_SAVE], )dnl

dnl uncomment to put support files in another directory
dnl AC_CONFIG_AUX_DIR(admin)

dnl if tools we need
AC_PROG_AWK
AC_PROG_SED
AC_PROG_CXX
AC_LANG(C++)


dnl Check for mkoctfile and octave
AC_CHECK_PROG([MKOCTFILE],mkoctfile,mkoctfile)
test -z "$MKOCTFILE" &&	AC_MSG_WARN([no mkoctfile found on path])

if test "X${IGNORE_MINGW_PATH_MODIFICATION}" == "X"; then
case $host_os in
  mingw*)
    # try demangle spaces in escaped input strings  
    MKOCTFILE=`echo $MKOCTFILE | $SED "s,\\\\\ ,?,g"`
    ;;
  *)
    ;;
esac
fi

dnl **********************************************************
dnl checking for nc-config

dnl AC_CHECK_PROG(NC_CONFIG, nc-config, nc-config)
AC_CHECK_TOOL(NC_CONFIG, nc-config, [])
if test -z "$NC_CONFIG"; then
    AC_MSG_ERROR([nc-config not found], 1);
fi

NETCDF_LIBS=`$NC_CONFIG --libs`
NETCDF_CPPFLAGS=`$NC_CONFIG --cflags`
AC_SUBST(NETCDF_LIBS)
AC_SUBST(NETCDF_CPPFLAGS)


dnl The following will be either "yes" or "no"
AC_MSG_CHECKING(for netcdf4 support)
NETCDF_V4=`$NC_CONFIG --has-nc4`
if test x$NETCDF_V4 == xyes; then
   AC_MSG_RESULT(yes)
else
   AC_MSG_RESULT(no)
   AC_MSG_ERROR(netcdf4 support missing. Did you disable netcdf4 features?)
fi

dnl **********************************************************
# Checks for octave depreciated symbols
# Simple symbol alternatives of different Octave versions.
save_altsyms_CXX="$CXX"
save_altsyms_CXXFLAGS="$CXXFLAGS"
save_altsyms_LDFLAGS="$LDFLAGS"
save_altsyms_LIBS="$LIBS"
OCTINCLUDEDIR="${OCTINCLUDEDIR:-`$MKOCTFILE -p OCTINCLUDEDIR`}/.."
OCTLIBDIR=${OCTLIBDIR:-`$MKOCTFILE -p OCTLIBDIR`}

if test "X${IGNORE_MINGW_PATH_MODIFICATION}" == "X"; then
  MSYSTEM="${MSYSTEM}"
else
  MSYSTEM="none"
fi

case X$MSYSTEM in
  XMINGW64*)
    OCTAVE_HOME=`${MKOCTFILE} -p OCTAVE_HOME | $SED 's,\\\\,/,g'`
    # change \ to / and replace octave home part with mingw part
    OCTINCLUDEDIR=`echo $OCTINCLUDEDIR | $SED -e 's,\\\\,/,g' -e "s,${OCTAVE_HOME},/mingw64,g"`
    OCTLIBDIR=`echo $OCTLIBDIR | $SED -e 's,\\\\,/,g' -e "s,${OCTAVE_HOME},/mingw64,g"`
    ;;
  XMINGW32*)
    OCTAVE_HOME=`${MKOCTFILE} -p OCTAVE_HOME | $SED 's,\\\\,/,g'`
    # change \ to / and replace octave home part with mingw part
    OCTINCLUDEDIR=`echo $OCTINCLUDEDIR | $SED -e 's,\\\\,/,g' -e "s,${OCTAVE_HOME},/mingw32,g"`
    OCTLIBDIR=`echo $OCTLIBDIR | $SED -e 's,\\\\,/,g -e "s,${OCTAVE_HOME},/mingw32,g"'`
    ;;
  *)
    ;;
esac

CXX=`${MKOCTFILE} -p CXX`
CXXFLAGS="-I$OCTINCLUDEDIR $CXXFLAGS"
LDFLAGS="-L$OCTLIBDIR $LDFLAGS"
LIBS="-loctinterp $LIBS"

OF_OCTAVE_LIST_ALT_SYMS([

[dnl
  [is_cell],
  [iscell],
  [[octave_value ().iscell ();]],
  [OV_ISCELL],
  [],
  []
],

[dnl
  [is_empty],
  [isempty],
  [[octave_value ().isempty ();]],
  [OV_ISEMPTY],
  [],
  []
]

],[oct-alt-includes.h])

CXX=$save_altsyms_CXX
CXXFLAGS=$save_altsyms_CXXFLAGS
LDFLAGS=$save_altsyms_LDFLAGS
LIBS=$save_altsyms_LIBS

dnl **********************************************************

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

AC_MSG_RESULT([
octave-forge is configured with
   mkoctfile:	  $MKOCTFILE
   nc-config:     $NC_CONFIG
   netCDF compiler flags: $NETCDF_CPPFLAGS
   netCDF libraries: $NETCDF_LIBS
])
