AC_PREREQ(2.59)
dnl Process this file with autoconf to produce a configure script.
dnl
dnl aclocal_cache.m4, included by sowing/confdb/aclocal.m4, fixes 
dnl bugs in autoconf caching.
dnl
dnl The file name here refers to a file in the source being configured
AC_INIT(mpiexec.c)
dnl
dnl Definitions will be placed in this file rather than in the DEFS variable
AC_CONFIG_HEADER(smpdconf.h)
AH_TOP([/* -*- Mode: C; c-basic-offset:4 ; -*- */
/*  
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */
#ifndef SMPDCONF_H_INCLUDED
#define SMPDCONF_H_INCLUDED
])
AH_BOTTOM([#endif])
dnl
dnl Set the directory that contains support scripts such as install-sh and
dnl config.guess
AC_CONFIG_AUX_DIR(../../../confdb)
dnl
echo "RUNNING CONFIGURE FOR THE SMPD PM"
dnl
dnl Use AC_ARG_ENABLE to look for --enable-feature and AC_ARG_WITH to look for
dnl --with-capability
dnl
dnl Enable better caching control
PAC_ARG_CACHING
dnl
AC_ARG_ENABLE(echo, 
[--enable-echo  - Turn on strong echoing. The default is enable=no.] ,set -x)
dnl
AC_ARG_ENABLE(strict,[--enable-strict - Turn on strict debugging with gcc],
CFLAGS="$CFLAGS -O2 -Wall -Wstrict-prototypes -Wmissing-prototypes")
dnl
dnl
dnl Set single for now
AC_ARG_ENABLE(g,
[--enable-g=option - Control the level of debugging support in the PMI
implementation.  option may be a list of common separated names including
    none  - No debugging
    all   - All of the above choices],,enable_g=none)
dnl
dnl
dnl First check that we have a clean build if we are doing a VPATH build
PAC_VPATH_CHECK()
dnl
dnl Process any enable or with values
dnl
dnl enable-g
case "$enable_g" in 
    no|none)
    ;;
    mem|dbg|all|yes)
    CFLAGS="$CFLAGS -g"
    ;;
    *)
    AC_MSG_WARN([Unknown value $enable_g for enable-g])
    ;;
esac
dnl
dnl This test is complicated by the fact that top_srcdir is not set until
dnl the very end of configure.  Instead, we get it ourselves
if test -z "$top_srcdir" ; then
   use_top_srcdir=$srcdir   
else
   use_top_srcdir=$top_srcdir
fi
dnl The recommended order for items in a configure.in file is
dnl     check for programs
dnl     check for libraries
dnl     check for header files
dnl     check for typedefs
dnl     check for structures
dnl     check for compiler characteristics
dnl     check for library functions
dnl     check for system services
dnl Here are some sample tests
dnl
dnl Find a C compiler (choose gcc first)
AC_PROG_CC
AC_PROG_INSTALL
PAC_PROG_CHECK_INSTALL_WORKS
PAC_PROG_MKDIR_P
PAC_PROG_MAKE
dnl
dnl     check for compiler characteristics
AC_C_CONST
PAC_C_VOLATILE
PAC_C_RESTRICT
AC_C_INLINE
dnl
dnl Look for Standard headers
AC_HEADER_STDC
dnl
dnl Setup other replaceable values
MPILIBNAME=${MPILIBNAME:-"mpich"}
AC_SUBST(MPILIBNAME)

AC_CHECK_HEADERS( sys/types.h sys/wait.h signal.h unistd.h stdlib.h errno.h stdarg.h pthread.h sys/socket.h sys/stat.h ctype.h )
AC_CHECK_HEADERS( math.h stdio.h termios.h string.h sys/stat.h malloc.h uuid/uuid.h )
AC_CHECK_HEADERS( stddef.h md5.h openssl/md5.h )
AC_CHECK_FUNCS( sigaction usleep CFUUIDCreate uuid_generate setenv md5_calc )
AC_SEARCH_LIBS( uuid_generate, uuid )
AC_MSG_CHECKING([for poll stdin capability])
AC_TRY_RUN([
#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    int fd;
    struct pollfd a[1];
    int result;
    int flags;

    fd = fileno(stdin);
    flags = fcntl(fd, F_GETFL, 0);
    if (flags == -1)
    {
	return -1;
    }
    flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    if (flags == -1)
    {
	return -1;
    }

    a[0].fd = fd;
    a[0].events = POLLIN;
    a[0].revents = 0;

    result = poll(a, 1, 1);
    if (result == 1)
    {
	if (a[0].revents & POLLNVAL)
	{
	    return 0;
	}
    }
    return -1;
}
], AC_MSG_RESULT(no)
   AC_DEFINE(USE_PTHREAD_STDIN_REDIRECTION,1,[define if the file descriptor for stdin cannot be passed to poll]), 
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(not checking when cross compiling))
AC_SEARCH_LIBS( MD5, ssl crypto )
# Make configure check for MD5 so that the variable will be set
AC_CHECK_FUNCS(MD5)
#
AC_SEARCH_LIBS( md5_calc, md5 )
AC_MSG_CHECKING([for md5_calc function])
AC_TRY_COMPILE([
#ifdef HAVE_STDDEF_H
 #include <stddef.h>
#endif
#include <md5.h>
],[unsigned char hash[16];
unsigned char c='a';
md5_calc(hash,&c,1);],ac_cv_have_md5_calc=yes,ac_cv_have_md5_calc=no)
if test "$ac_cv_have_md5_calc" = yes ; then
    AC_DEFINE(HAVE_MD5_CALC,1,[Define if you have the md5_calc function.])
    AC_MSG_RESULT(yes)
else
    AC_MSG_RESULT(no)
fi

# md5 appears to be required for SMPD.  Test that we have either mp5_calc
# or MP5 (may be part of openssl/md5.h)
if test "$ac_cv_have_md5_calc" != yes -a \
  \( "$ac_cv_header_openssl_md5_h" != yes -o "$ac_cv_func_MD5" != yes \) ; then
    AC_MSG_ERROR([SMPD requires MD5 support, and configure could not find either md5_calc in md5.h or MD5 in openssl/md5.h])
fi

dnl Note that # does not comment out lines that are passed to autoconf,
dnl and thus # MUST NEVER be used to comment out AC_xxx commands
dnl AC_SEARCH_LIBS( MD5, crypto, SMPD_LIBS="$SMPD_LIBS -lcrypto" )

SMPD_LIBS="$SMPD_LIBS $LIBS"
AC_SUBST(SMPD_LIBS)
AC_SUBST(AR)
AC_SUBST(RANLIB)
AC_SUBST(MPILIBNAME)
AC_SUBST(CC)
AC_SUBST(CPPFLAGS)
AC_SUBST(CFLAGS)
AC_SUBST(CC_SHL)
AC_SUBST(C_LINK_SHL)
AC_SUBST(ENABLE_SHLIB)
AC_SUBST(master_top_srcdir)
dnl
dnl Dependency handling
AC_SUBST(MAKE_DEPEND_C)
dnl
dnl Get any LIBS required by the sock implementation
dnl
. ../../mpid/common/sock/localdefs
. ../../pmi/smpd/localdefs
dnl
dnl Generate the Makefiles from Makefile.in
AC_OUTPUT(Makefile localdefs)
