dnl
dnl %CopyrightBegin%
dnl
dnl Copyright Ericsson AB 2005-2014. All Rights Reserved.
dnl
dnl Licensed under the Apache License, Version 2.0 (the "License");
dnl you may not use this file except in compliance with the License.
dnl You may obtain a copy of the License at
dnl
dnl     http://www.apache.org/licenses/LICENSE-2.0
dnl
dnl Unless required by applicable law or agreed to in writing, software
dnl distributed under the License is distributed on an "AS IS" BASIS,
dnl WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dnl See the License for the specific language governing permissions and
dnl limitations under the License.
dnl
dnl %CopyrightEnd%
dnl

dnl define([AC_CACHE_LOAD], )dnl
dnl define([AC_CACHE_SAVE], )dnl

if test "x$no_recursion" != "xyes" -a "x$OVERRIDE_CONFIG_CACHE" = "x"; then
    # We do not want to use a common cache!
    cache_file=/dev/null
fi



dnl Process this file with autoconf to produce a configure script.
AC_INIT(c_src/odbcserver.c)

if test -z "$ERL_TOP" || test ! -d $ERL_TOP ; then
  AC_CONFIG_AUX_DIRS(autoconf)
else
  erl_top=${ERL_TOP}
  AC_CONFIG_AUX_DIRS($erl_top/erts/autoconf)
fi

if test "X$host" != "Xfree_source" -a "X$host" != "Xwin32"; then
    AC_CANONICAL_HOST
else
    host_os=win32
fi

AC_ARG_WITH(odbc,
[  --with-odbc=PATH        specify location of ODBC include and lib
  --with-odbc             use ODBC (default)
  --without-odbc          don't use ODBC])

if test "$with_odbc" = "no"; then

   rm -f "$ERL_TOP/lib/odbc/SKIP"
   echo "odbc disabled by user." > "$ERL_TOP/lib/odbc/SKIP"

else dnl "$with_odbc" != "no"

ERL_XCOMP_SYSROOT_INIT

dnl Checks for programs.
AC_PROG_CC

dnl ---------------------------------------------------------------------
dnl Special windows stuff regarding CFLAGS and details in the environment...
dnl ---------------------------------------------------------------------
LM_WINDOWS_ENVIRONMENT
	
AC_PROG_MAKE_SET

AC_CHECK_PROGS(LD, ld.sh)
AC_CHECK_TOOL(LD, ld, '$(CC)')

AC_SUBST(LD)

# Sockets
#--------------------------------------------------------------------
#	Check for the existence of the -lsocket and -lnsl libraries.
#	The order here is important, so that they end up in the right
#	order in the command line generated by make.  Here are some
#	special considerations:
#	1. Use "connect" and "accept" to check for -lsocket, and
#	   "gethostbyname" to check for -lnsl.
#	2. Use each function name only once:  can't redo a check because
#	   autoconf caches the results of the last check and won't redo it.
#	3. Use -lnsl and -lsocket only if they supply procedures that
#	   aren't already present in the normal libraries.  This is because
#	   IRIX 5.2 has libraries, but they aren't needed and they're
#	   bogus:  they goof up name resolution if used.
#	4. On some SVR4 systems, can't use -lsocket without -lnsl too.
#	   To get around this problem, check for both libraries together
#	   if -lsocket doesn't work by itself.
#--------------------------------------------------------------------
erl_checkBoth=0
AC_CHECK_FUNC(connect, erl_checkSocket=0, erl_checkSocket=1)
if test "$erl_checkSocket" = 1; then
    AC_CHECK_LIB(socket, socket, LIBS="$LIBS -lsocket", erl_checkBoth=1)
fi
if test "$erl_checkBoth" = 1; then
    tk_oldLibs=$LIBS
    LIBS="$LIBS -lsocket -lnsl"
    AC_CHECK_FUNC(accept, odbc_erl_checkNsl=0, [LIBS=$tk_oldLibs])
fi
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, main, [LIBS="$LIBS -lnsl"]))

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([fcntl.h netdb.h stdlib.h string.h sys/socket.h winsock2.h])
AC_CHECK_HEADERS([windows.h])
AC_CHECK_HEADERS([sql.h sqlext.h], [odbc_required_headers=yes], [odbc_required_headers=no],
[[#ifdef HAVE_WINDOWS_H
     # include <windows.h>
     #endif
     ]])

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_CHECK_MEMBERS([struct sockaddr_in6.sin6_addr], [], [],
			 [#if HAVE_WINSOCK2_H
			  #include <winsock2.h>
			  #include <ws2tcpip.h>
			  #else
			  #include <netinet/in.h>
			  #endif])

dnl Checks for library functions.
AC_CHECK_FUNCS([memset socket])
	
# ODBC 
/bin/rm -f "$ERL_TOP/lib/odbc/SKIP"

LM_CHECK_THR_LIB
AC_SUBST(THR_DEFS)
AC_SUBST(THR_LIBS)

odbc_lib_link_success=no
AC_SUBST(TARGET_FLAGS)
    case $host_os in
        darwin1[[0-4]].*|darwin[[0-9]].*)
                TARGET_FLAGS="-DUNIX"
	        if test ! -d "$with_odbc" || test "$with_odbc" = "yes"; then
		    ODBC_LIB= -L"/usr/lib"
		    ODBC_INCLUDE="-I/usr/lib/include"
		else
		    ODBC_LIB=-L"$with_odbc/lib"
		    ODBC_INCLUDE="-I$with_odbc/include"
		fi
			    
               AC_CHECK_LIB(iodbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -liodbc"; odbc_lib_link_success=yes])
            ;;
        win32|cygwin)
		TARGET_FLAGS="-DWIN32"
		AC_CHECK_LIB(ws2_32, main)
		if test ! -d "$with_odbc" || test "$with_odbc" = "yes"; then
		    ODBC_LIB=""   
		    ODBC_INCLUDE=""
		else
		    ODBC_LIB=-L"$with_odbc/lib"
		    ODBC_INCLUDE="-I$with_odbc/include"
		fi
		AC_CHECK_LIB(odbc32, main, [ODBC_LIB="$ODBC_LIB -lodbc32"; odbc_lib_link_success=yes])
	    ;;
	    *)
	        TARGET_FLAGS="-DUNIX"
		case "$erl_xcomp_without_sysroot-$with_odbc" in
		    yes-yes | yes- )
			msg="Dont know where to search for odbc (setting erl_xcomp_sysroot will help)"
			AC_MSG_WARN([$msg])
			echo "$msg" > "$ERL_TOP/lib/odbc/SKIP"
			odbc_lib_link_success=wont_try
			;;
		    no-yes | no- )
			AC_CHECK_SIZEOF(void *)
			AC_MSG_CHECKING([for odbc in standard locations])
			for rdir in /usr/local/odbc /usr/local /usr/odbc \
			            /usr /opt/local/pgm/odbc /usr/local/pgm/odbc \
			            "$with_odbc"; do
			  test -f "$erl_xcomp_isysroot$rdir/include/sql.h" || continue
			  is_odbc_std_location=yes
			  libdir="$erl_xcomp_sysroot$rdir/lib"
			  if test "$ac_cv_sizeof_void_p" = "8"; then
			      dnl "/." in test is important (dir symlinks)
			      if test -d "${libdir}64/."; then
				  libdir="${libdir}64"
			      elif test -d "${libdir}/64/."; then
				  libdir="${libdir}/64"
			      fi
			  fi			  
			  ODBC_LIB="-L$libdir"
			  ODBC_INCLUDE="-I$erl_xcomp_isysroot$rdir/include"
			  break
			done
			if  test "x$is_odbc_std_location" != "xyes"; then
			    AC_MSG_RESULT(no)
			    AC_MSG_WARN([No odbc library found skipping odbc])
			    echo "No odbc library found" > "$ERL_TOP/lib/odbc/SKIP"
			else
			    AC_MSG_RESULT($ODBC_LIB)
			    AC_CHECK_LIB(odbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -lodbc"; odbc_lib_link_success=yes])			 
			fi
			;;

		    *)
			ODBC_LIB=-L"$with_odbc/lib"
			ODBC_INCLUDE="-I$with_odbc/include"
			AC_CHECK_LIB(odbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -lodbc"; odbc_lib_link_success=yes])
			;;
		esac
	  ;;
esac

if test $odbc_required_headers = no;  then
    AC_MSG_WARN(["ODBC library - header check failed"])
    echo "ODBC library - header check failed" > $ERL_TOP/lib/odbc/SKIP
fi
if test $odbc_lib_link_success = no;  then
    AC_MSG_WARN(["ODBC library - link check failed"])
    echo "ODBC library - link check failed" > $ERL_TOP/lib/odbc/SKIP
fi

AC_SUBST(ODBC_LIB)
AC_SUBST(ODBC_INCLUDE)

fi dnl "$with_odbc" != "no"

if test "x$GCC" = xyes; then
    # Treat certain GCC warnings as errors
    LM_TRY_ENABLE_CFLAG([-Werror=return-type], [CFLAGS])
fi

dnl ----------------------------------------------------------------------
dnl Enable -fsanitize= flags.
dnl ----------------------------------------------------------------------

m4_define(DEFAULT_SANITIZERS, [address,undefined])
AC_ARG_ENABLE(
    sanitizers,
    AS_HELP_STRING(
        [--enable-sanitizers@<:@=comma-separated list of sanitizers@:>@],
	    [Default=DEFAULT_SANITIZERS]),
[
case "$enableval" in
    no) sanitizers= ;;
    yes) sanitizers="-fsanitize=DEFAULT_SANITIZERS" ;;
    *) sanitizers="-fsanitize=$enableval" ;;
esac
CFLAGS="$CFLAGS $sanitizers"
LDFLAGS="$LDFLAGS $sanitizers"
])

AC_OUTPUT(c_src/$host/Makefile:c_src/Makefile.in)