AC_PREREQ(2.59)
AC_INIT(configure.in)

dnl
dnl Set the directory that contains support scripts such as install-sh and
dnl config.guess
dnl
AC_CONFIG_AUX_DIR(../../../../../confdb)

AC_ARG_ENABLE(sock-debug,[--enable-sock-debug - Turn on tests of the socket data structures],,enable_sock_debug=no)

dnl
dnl Definitions will be placed in this file rather than in the DEFS variable
dnl
AC_CONFIG_HEADER(mpidu_socki_conf.h)
AH_TOP([/* -*- Mode: C; c-basic-offset:4 ; -*- */
/*  
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */
#ifndef MPIDU_SOCKI_CONF_H_INCLUDED
#define MPIDU_SOCKI_CONF_H_INCLUDED
])
AH_BOTTOM([#endif])

echo "RUNNING CONFIGURE FOR THE POLL IMPLEMENTATION OF SOCK"

AC_ARG_ENABLE(echo,
   [--enable-echo - turn on echoing from within configure script],
   set -x)

dnl
dnl First check that we have a clean build if we are doing a VPATH build
PAC_VPATH_CHECK()

PAC_LOAD_BASE_CACHE

if test "$enable_sock_debug" = yes ; then
    AC_DEFINE(USE_SOCK_VERIFY,1,[Define it the socket verify macros should be enabled])
fi

PAC_PROG_MAKE

AC_C_INLINE

dnl
dnl _XOPEN_SOURCE_EXTENDED=1 is required for Solaris to build properly */
dnl (TEMPORARY - Setting the Unix source type must happen before 
dnl any tests are performed to ensure that all configure tests use the
dnl same environment.  In particular, under AIX, this causes 
dnl struct timespec to *not* be defined, 
dnl CFLAGS="$CFLAGS -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1"

AC_CHECK_FUNC(poll,pac_cv_have_func_poll=yes,pac_cv_have_func_poll=no)
if test "$pac_cv_have_func_poll" != yes ; then
    if test -f /sw/include/sys/poll.h ; then
        dnl This is for Mac OSX (Darwin) which doesn't have a poll function is the standard libraries.  Instead we use a
        dnl  contributedlibrary found off in la la land (/sw).
        CFLAGS="$CFLAGS -I/sw/include"
        TCP_LIBS="$TCP_LIBS -L/sw/lib -lpoll"
    else
        AC_MSG_ERROR([This device requires the poll function])
    fi
fi

AC_CHECK_HEADERS(                               \
    assert.h                                    \
    errno.h                                     \
    fcntl.h                                     \
    limits.h                                    \
    netdb.h                                     \
    netinet/in.h                                \
    netinet/tcp.h                               \
    poll.h					\
    stdlib.h                                    \
    sys/param.h                                 \
    sys/poll.h                                  \
    sys/types.h                                 \
    sys/uio.h					\
    unistd.h)

AC_MSG_CHECKING([if struct poll is defined]);
AC_TRY_COMPILE([
#if defined(HAVE_POLL_H)
#include <poll.h>
#endif
#if defined(HAVE_SYS_POLL_H)
#include <sys/poll.h>
#endif
],[
struct pollfd pollfd;
pollfd.fd = -1;
],[
AC_MSG_RESULT([yes])
],[
AC_MSG_RESULT([no])
AC_MSG_ERROR([This device requires the poll function])
])

AC_MSG_CHECKING([if a simple program using poll() can be compiled]);
AC_TRY_COMPILE([
#if defined(HAVE_POLL_H)
#include <poll.h>
#endif
#if defined(HAVE_SYS_POLL_H)
#include <sys/poll.h>
#endif
],[
struct pollfd pollfds[2];
int n_fds;
pollfds[0].fd = -1;
pollfds[1].fd = -1;
n_fds = poll(pollfds, 2, -1);
],[
AC_MSG_RESULT([yes])
],[
AC_MSG_RESULT([no])
AC_MSG_ERROR([This device requires the poll function])
])

dnl
dnl needed on AIX
dnl
AC_MSG_CHECKING([whether bit fields work in ip.h]);
AC_TRY_COMPILE([
#include <netinet/tcp.h>
],[
int i;
],[
AC_MSG_RESULT([yes])
bit_fields=yes
],[
AC_MSG_RESULT([no])
bit_fields=no
])
if test "$bit_fields" = "no" ; then
     AC_MSG_RESULT([Adding -D_NO_BITFIELDS to CFLAGS])
     CFLAGS="$CFLAGS -D_NO_BITFIELDS"
fi


AC_CHECK_FUNCS(gethostname)
if test "$ac_cv_func_gethostname" = "yes" ; then
    # Do we need to declare gethostname?
    PAC_FUNC_NEEDS_DECL([#include <unistd.h>],gethostname)
fi

saveLIBS="$LIBS"
LIBS=""
AC_SEARCH_LIBS(socket,socket,,,$saveLIBS)
AC_SEARCH_LIBS(gethostbyname,nsl,,,$saveLIBS)
TCP_LIBS="$TCP_LIBS $LIBS"
LIBS="$saveLIBS $LIBS"
#
# Check first for sys/socket.h .  We check not only for existence but whether
# it can be compiled (!), as we have seen some problems with this.
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/socket.h>
],[int a;],ac_cv_header_sys_socket_h=yes,ac_cv_header_sys_socket_h=no)
if test "$ac_cv_header_sys_socket_h" = yes ; then
    AC_DEFINE(HAVE_SYS_SOCKET_H,1,[Define if you have the <sys/socket.h> header file.])
fi
# Check for socklen_t .  If undefined, define it as int
# (note the conditional inclusion of sys/socket.h)
AC_CACHE_CHECK([whether socklen_t is defined (in sys/socket.h if present)],
pac_cv_have_socklen_t,[
AC_TRY_COMPILE([
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
]
typedef struct { double a; int b; } socklen_t;,[socklen_t a;a.a=1.0;],pac_cv_have_socklen_t=no,pac_cv_have_socklen_t=yes)])
if test "$pac_cv_have_socklen_t" = no ; then
    AC_DEFINE(socklen_t,int,[Define if socklen_t is not defined])
fi
AC_SUBST(TCP_LIBS)

AC_SUBST(AR)
AC_SUBST(RANLIB)
AC_SUBST(MPILIBNAME)
AC_SUBST(CC)
AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
PAC_CC_SUBDIR_SHLIBS
AC_SUBST(master_top_srcdir)
AC_SUBST(master_top_builddir)
dnl
dnl Dependency handling
AC_SUBST(MAKE_DEPEND_C)
dnl
dnl Etags
AC_CHECK_PROGS(ETAGS,etags,true)
AC_SUBST(ETAGS)
AC_SUBST(ETAGSADD)
export ETAGS
export ETAGSADD

PAC_UPDATE_BASE_CACHE

AC_OUTPUT(Makefile localdefs)
