dnl Process this file with autoconf to produce a configure script.
AC_INIT
AC_CONFIG_SRCDIR([aprx.h])

dnl For automake
VERSION="`cat VERSION`"
PACKAGE=aprx
dnl AM_INIT_AUTOMAKE($PACKAGE, $VERSION)

AC_PROG_MAKE_SET

AC_CONFIG_HEADERS([config.h])

dnl Checks for programs.
AC_PROG_CC
AC_PROG_GCC_TRADITIONAL

dnl AC_PATH_PROG(LD, ld, ld)dnl
if test -z "$LD" ; then
  LD="$CC"
fi
AC_SUBST(LD,"$LD")

dnl If on i686, we'll need -march=i686 to get the atomic instructions
dnl On FreeBSD, the architecture is i386.
MACHINE="`uname -m`"
if test "$MACHINE" == "i686" -o "$MACHINE" == "i386"; then
  CFLAGS_ARCH="-march=i686"
fi
OS="`uname`"
if test "$OS" == "Darwin"; then
  CFLAGS_ARCH=""
fi
AC_SUBST(CFLAGS_ARCH)

dnl Check for GNU make
dnl Fails with autoconf2.70 (#978766): AX_CHECK_GNU_MAKE()


dnl Check for headers.
AC_CHECK_HEADERS(time.h sys/time.h stdlib.h stddef.h stdint.h)
AC_CHECK_HEADERS(string.h strings.h)
AC_CHECK_HEADERS(pty.h)
AC_CHECK_HEADERS(pthread.h)

dnl Checks for system headers
AC_CHECK_HEADERS([alloca.h],    AC_DEFINE([HAVE_ALLOCA_H]))
AC_CHECK_HEADERS([poll.h],      AC_DEFINE([HAVE_POLL_H]))
dnl AC_CHECK_FUNC(ppoll,,[Probably have ppoll of Linux])
AC_CHECK_HEADERS([sys/epoll.h], AC_DEFINE([HAVE_SYS_EPOLL_H]))

dnl SCTP checks
AC_CHECK_HEADERS([netinet/sctp.h], AC_DEFINE([HAVE_NETINET_SCTP_H]))



dnl Check for varargs
AC_CHECK_HEADERS(stdarg.h varargs.h sys/wait.h)
AC_FUNC_VPRINTF


dnl This group must be after header tests


AC_MSG_RESULT([** Using C compiler: $CC])
AC_MSG_RESULT([** Using CFLAGS:     $CFLAGS])
AC_MSG_RESULT([** Using CPPDEP:     $CPPDEP])

AC_C_BIGENDIAN
dnl AC_INLINE
AC_CHECK_SIZEOF(void *)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(double)

dnl AC_DEFINE_UNQUOTED(CONFIGURE_CMD,"$0 $ac_configure_args")
AC_DEFINE_UNQUOTED(CONFIGURE_CMD,
                   "CC='$CC' CFLAGS='$CFLAGS' $0 $ac_configure_args",
		   [Configuration command line])


dnl Checks for libraries.
AC_SUBST(LIBM)
AC_CHECK_FUNCS(atan2f,,
	       AC_CHECK_LIB(m, atan2f,
			    [LIBM="-lm"]))

AC_CHECK_FUNCS(memchr memrchr gettimeofday)

dnl Checks for library functions.
AC_CHECK_FUNCS(openpty,,
	       AC_CHECK_LIB(util, openpty,
			    [AC_DEFINE(HAVE_OPENPTY,1,[])] [LIBS="$LIBS -lutil"]))

dnl Check for user options

dnl The "EMBEDDED" is now always on, replaced with --with-erlangstorage
AC_DEFINE(EMBEDDED,1,[Define for an embedded target])

AC_ARG_WITH(embedded, [  --with-embedded  When desiring to target as embedded],
		      [AC_DEFINE(EMBEDDED,1,[Define for an embedded target]) EMBEDDED=1])

AC_ARG_WITH(erlangstorage, [  --with-erlangstorage  When desiring a longer term backing storage on erlang datasets.  NOT compatible with EMBEDDED, REQUIRES FILESYSTEM!],
		      [AC_DEFINE(ERLANGSTORAGE,1,[Define for a non-embedded system with filesystem based Erlang history storage]) ERLANGSTORAGE=1])

AC_ARG_ENABLE(igate,    [  --disable-igate         Disable all IGate codes],
[if test "${enable_igate}" = no ; then
    AC_DEFINE(DISABLE_IGATE,1,[Define to 1 if you want to disable all IGATE codes.])
fi])

AC_ARG_ENABLE(agwpe,    [  --enable-agwpe          Enable AGWPE socket interface code.],
[if test "${enable_agwpe}" != no ; then
    AC_DEFINE(ENABLE_AGWPE,1,[Define to 1 if you want to enable AGWPE socket interface.])
fi])


AC_ARG_WITH(pthread,  [  --without-pthread   When desiring not to use pthread subsystem],
                      [AC_DEFINE(DISABLE_PTHREAD,1,[Define for pthread(3p) disabling]) DISABLE_PTHREAD=1],
		      [AC_DEFINE(ENABLE_PTHREAD,1,[Define for pthread(3p) enabling]) ENABLE_PTHREAD=1])
AC_ARG_WITH(pthreads, [  --with-pthreads  (mistyped pthread) When desiring use pthread subsystem],
		      [AC_DEFINE(ENABLE_PTHREAD,1,[Define for pthread(3p) enabling]) ENABLE_PTHREAD=1])

dnl search for pthread libs and compilation option
AC_SUBST(LIBPTHREAD)
AC_SUBST(CCPTHREAD)
if test "${ENABLE_PTHREAD}" = "1" ; then
    AC_MSG_RESULT([The --without-pthread option is not set, looking for pthread_create() function.])
    have_pthread=no
    if test $have_pthread = no; then
       t_oldLibs="$LIBS"
       LIBS="$LIBS -pthread"
       t_oldCflags="$CFLAGS"
       CFLAGS="$CFLAGS -pthread"
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]],[[pthread_t pt;
pthread_attr_t pat;
int rc = pthread_create(&pt, &pat, NULL, NULL)]])],
                      [have_pthread=yes;AC_MSG_RESULT([Have pthread_create()])],
                      [AC_MSG_RESULT([Not have pthread_create()])])

       if test $have_pthread = no ; then
          AC_MSG_RESULT([Failure at HAVE_PTHREAD_CREATE])
       else
          AC_DEFINE(HAVE_PTHREAD_CREATE,1,[Have pthread_create() function])
          AC_MSG_RESULT([Success at HAVE_PTHREAD_CREATE])
          LIBPTHREAD="-pthread"
          CCPTHREAD="-pthread"
       fi
       LIBS="$t_oldLibs"
       CFLAGS="$t_oldCflags"
    fi
    if test $have_pthread = no; then
       AC_MSG_RESULT([Failure at HAVE_PTHREAD_CREATE, trying second way.])
       t_oldLibs="$LIBS"
       LIBS="$LIBS -lpthread"
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]],[[pthread_t pt;
pthread_attr_t pat;
int rc = pthread_create(&pt, &pat, NULL, NULL)]])],
                      [have_pthread=yes;AC_MSG_RESULT([Have pthread_create()])],
                      [AC_MSG_RESULT([Not have pthread_create()])])

       if test $have_pthread = no ; then
          AC_MSG_RESULT([Failure at HAVE_PTHREAD_CREATE])
       else
          AC_DEFINE(HAVE_PTHREAD_CREATE,1,[Have pthread_create() function])
          AC_MSG_RESULT([Success at HAVE_PTHREAD_CREATE])
          LIBPTHREAD="-lpthread"
          CCPTHREAD=""
       fi
       LIBS="$t_oldLibs"
       CFLAGS="$t_oldCflags"
    fi
    if test $have_pthread = no; then
       AC_MSG_RESULT([Still failure at HAVE_PTHREAD_CREATE, Run out of ways to set it up.])
    fi
fi



dnl Check for clock_gettime, and a library to have it
AC_SUBST(LIBRT)
AC_SEARCH_LIBS([clock_gettime], [rt], [
  AC_DEFINE(HAVE_CLOCK_GETTIME, 1,[Have clock_gettime])
])

if test "$ac_cv_search_clock_gettime" = "-lrt"; then
  LIBRT="-lrt"
fi

dnl Solaris resolver solution:
AC_SUBST(LIBGETADDRINFO)
AC_CHECK_FUNCS(getaddrinfo,,
	       AC_CHECK_LIB(nsl, getaddrinfo,
			    [LIBGETADDRINFO="-lnsl"]))

AC_SUBST(LIBSOCKET)dnl
AC_SUBST(LIBRESOLV)dnl

#
#  We check for various libraries
#  - SysVr4 style of "-lsocket" at first (unless in libc)
#    The hallmark is  connect()  routine (we presume)
#
ac_cv_libsocket_both=1
AC_CHECK_FUNC(connect, ac_cv_libsocket_both=0)
AC_CHECK_FUNC(gethostbyname, ac_cv_libsocket_both=0)
if test "$ac_cv_libsocket_both" = 1 ; then
  # Check cache
  if test "$ac_cv_func_socket_lxnet" = yes ; then
    AC_MSG_RESULT([need -lxnet library (cached)])
    LIBSOCKET="-lnsl -lsocket -lxnet"
  else
  if test "$ac_cv_func_socket_lsocket" = yes ; then
    AC_MSG_RESULT([need -lsocket library (cached)])
    LIBSOCKET="-lsocket"
    if test "$ac_cv_func_gethostbyname_lnsl" = yes ; then
        LIBSOCKET="-lnsl -lsocket"
    fi
  else
    # Well, will this work ?  SysVR4, but not Sun Solaris ?
    AC_CHECK_LIB(xnet, connect, [LIBSOCKET="-lnsl -lsocket -lxnet"
                                 ac_cv_func_socket_lsocket=no
                                 ac_cv_func_socket_lxnet=yes],[
      AC_CHECK_LIB(socket, connect, [LIBSOCKET="-lsocket"
                                     ac_cv_func_socket_lsocket=yes],
                                     ac_cv_func_socket_lsocket=no)
      if test "$ac_cv_func_socket_lsocket" = yes ; then
        t_oldLibs="$LIBS"
        LIBS="$LIBS -lsocket $LIBRESOLV"
        AC_TRY_LINK([],[gethostbyname();], ,[
          LIBS="$LIBS -lnsl" # Add this Solaris library..
          AC_TRY_LINK([],[gethostbyname();],[
                        LIBSOCKET="-lsocket -lnsl"
                        ac_cv_func_gethostbyname_lnsl=yes
                ], [
                   AC_MSG_ERROR([Weird, '$LIBS' not enough to find  gethostnyname() ?!])
                ])
          ])
        LIBS="$t_oldLibs"
      fi
    ])
  fi
  fi
fi

if test "x$LIBRESOLV" = "x"; then
  # Ok, No  -lresolv,  is this enough for the  _res  to appear ?
  t_oldLibs="$LIBS"
  LIBS="$LIBS $LIBSOCKET"
  ac_cv_var__res_options=no
  # This following is for IRIX6.4, and I sincerely hope it
  # will not fail on other systems...  It did! It did!
  # Many systems don't have idemponent headers, they need specific
  # includes before latter ones, or the latter ones won't be successful...
  AC_TRY_LINK([#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>],
              [_res.options = RES_INIT;],
                ac_cv_var__res_options=yes);
  if test "$ac_cv_var__res_options" != "yes"; then
    AC_MSG_RESULT([Can't do without  -lresolv  to link resolver's  _res.options])
    LIBS="$LIBS -lresolv"
  fi
  LIBS="$t_oldLibs"
fi

# See about the routines that possibly exist at the libraries..
LIBS="$t_oldLibs $LIBSOCKET"
AC_CHECK_FUNCS(socket socketpair)
LIBS="$t_oldLibs"

if test "$ac_cv_func_socket" = no -a "$LIBSOCKET" != ""; then
  LIBS="$LIBS $LIBSOCKET"
  AC_TRY_LINK([],[socket();], ac_cv_func_socket=yes)
  if test $ac_cv_func_socket = yes; then
    AC_DEFINE(HAVE_SOCKET)
    AC_MSG_RESULT([Has  socket()  when using  $LIBSOCKET])
  fi
  LIBS="$t_oldLibs"
fi
if test "$ac_cv_func_socketpair" = no -a "$LIBSOCKET" != ""; then
  LIBS="$LIBS $LIBSOCKET"
  AC_TRY_LINK([],[socketpair();], ac_cv_func_socketpair=yes)
  if test $ac_cv_func_socketpair = yes; then
    AC_DEFINE(HAVE_SOCKETPAIR)
    AC_MSG_RESULT([Has  socketpair()  when using  $LIBSOCKET])
  fi
  LIBS="$t_oldLibs"
fi

dnl Check for openssl
AC_ARG_WITH(openssl, [  --with-openssl[=DIR]    Include OpenSSL support (requires OpenSSL >= 0.9.7)], [with_openssl=$withval], [with_openssl=no])
AC_SUBST(LIBSSL)
AC_SUBST(LIBCRYPTO)
if test "$with_openssl" != "no" ; then
  AC_CHECK_HEADERS([openssl/ssl.h])
  AC_SEARCH_LIBS(TLSv1_server_method, ssl, AC_DEFINE(HAVE_TLSV1_SERVER_METHOD, 1, [OpenSSL 0.9.7 or later]))
  AC_SEARCH_LIBS(X509_free, crypto, AC_DEFINE(HAVE_X509_FREE, 1, [OpenSSL 0.9.7 or later]))
  
  if test "$ac_cv_search_TLSv1_server_method" = "-lssl"; then
    LIBSSL="-lssl"
  fi
  if test "$ac_cv_search_X509_free" = "-lcrypto"; then
    LIBCRYPTO="-lcrypto"
  fi
fi

dnl Define compilation variables supplying version information
t_vers="`cat VERSION`"
AC_SUBST(VERSION_STRING, "`cat VERSION`")

dnl Output files
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
