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)

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

AC_DEFINE(HAVE_MPIDI_CH3_CONF,1,[Define so that we can test whether the mpidi_ch3_conf.h file has been included])

echo "RUNNING CONFIGURE FOR CH3 DEVICE"

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

# -----------------------------------------------------------------------------
# experiment with creating a cache file.  Load if it exists
#if test -n "$CONF_BASE_CACHEFILE" -a -s "$CONF_BASE_CACHEFILE" ; then
#    echo "Loading base cachefile $CONF_BASE_CACHEFILE"
#    . $CONF_BASE_CACHEFILE
#    export CONF_BASE_CACHEFILE
#fi
PAC_LOAD_BASE_CACHE
# -----------------------------------------------------------------------------

# Extract the device and channel name
file=${master_top_srcdir}/src/mpid/${device_name}/setup_device.args
if test -f ${file} ; then 
    . ${file}
else
    echo "ERROR: ${file} not found"
    exit 1
fi
export channel_name
export channel_args
AC_SUBST(device_name)
AC_SUBST(channel_name)

if test ! -d $srcdir/channels/${channel_name} ; then
    AC_MSG_ERROR([Channel ${channel_name} is unknown])
elif test ! -x $srcdir/channels/${channel_name}/configure ; then
    AC_MSG_ERROR([Channel ${channel_name} has no configure])
fi

PAC_PROG_MAKE

# Some channels will make use of common utility routines (in ch3/src/util)
# For modularity, the channel can specify these here
# FIXME: Some utility routines are specified at the top level (devsubsystem)
# even though their scope is really ch3 in practice.  We haven't resolved
# the best way to handle these systems
# Note that the "setup_<channelname>" script must also set 
#   needs_sock=yes
# if util/sock is used, and
#   needs_shm=yes
# if util/shm is used.
if test -s $srcdir/channels/$channel_name/setup_$channel_name ; then
    # Typically, this will set ch3subsystems.  For now,
    # we use this configure to create the configuration file
    # for those subsystems.  Eventually, they may have their	
    # own configure
    . $srcdir/channels/$channel_name/setup_$channel_name
    if test "$needs_sock" = yes ; then
        AC_DEFINE(HAVE_UTIL_SOCK,1,[Define if the channel needs util/sock])
	CPPFLAGS="${CPPFLAGS} -I${master_top_srcdir}/src/mpid/ch3/util/sock"
    fi				   	
    if test "$needs_shm" = yes ; then
        AC_DEFINE(HAVE_UTIL_SHM,1,[Define if the channel needs util/shm])
	#CPPFLAGS="${CPPFLAGS} -I${master_top_srcdir}/src/mpid/ch3/util/shm"
    fi				   	
fi
AC_SUBST(ch3subsystems)

AC_CHECK_HEADERS(assert.h limits.h string.h sys/types.h sys/uio.h uuid/uuid.h \
    time.h ctype.h unistd.h arpa/inet.h sys/socket.h net/if.h)

# Check for special types
AC_TYPE_PID_T

# Check for functions
AC_CHECK_FUNCS(inet_pton)
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

AC_CHECK_FUNCS(CFUUIDCreate uuid_generate time)
AC_SEARCH_LIBS(uuid_generate, uuid)

AC_CACHE_CHECK([whether CPP accepts variable length argument lists],
pac_cv_have_cpp_varargs,[
AC_TRY_COMPILE([
#include <stdio.h>
#define MY_PRINTF(rank, fmt, args...)  printf("%d: " fmt, rank, ## args)
],[
MY_PRINTF(0, "hello");
MY_PRINTF(1, "world %d", 3);
], pac_cv_have_cpp_varargs=yes, pac_cv_have_cpp_varargs=no)
])
if test $pac_cv_have_cpp_varargs = "yes" ; then
    AC_DEFINE(HAVE_CPP_VARARGS,,[Define if CPP supports macros with a variable number arguments])
fi

AC_C_BIGENDIAN

# If we need the socket code, see if we can use struct ifconf
# sys/socket.h is needed on Solaris
AC_CACHE_CHECK([whether we can use struct ifconf],
pac_cv_have_struct_ifconf,[
AC_TRY_COMPILE([
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <net/if.h>
],[struct ifconf conftest;],
pac_cv_have_struct_ifconf=yes,pac_cv_have_struct_ifconf=no)])

if test "$pac_cv_have_struct_ifconf" = "no" ; then
    # Try again with _SVID_SOURCE
    AC_CACHE_CHECK([whether we can use struct ifconf with _SVID_SOURCE],
pac_cv_have_struct_ifconf_with_svid,[
AC_TRY_COMPILE([
#define _SVID_SOURCE
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <net/if.h>
],[struct ifconf conftest;],
pac_cv_have_struct_ifconf_with_svid=yes,
pac_cv_have_struct_ifconf_with_svid=no)])
    if test "$pac_cv_have_struct_ifconf_with_svid" = yes ; then
        AC_DEFINE(USE_SVIDSOURCE_FOR_IFCONF,1,[Define if _SVID_SOURCE needs to be defined for struct ifconf])
    fi
fi
if test "$pac_cv_have_struct_ifconf" = "yes" -o \
        "$pac_cv_have_struct_ifconf_with_svid" = "yes" ; then
    AC_DEFINE(HAVE_STRUCT_IFCONF,1,[Define if struct ifconf can be used])
fi
#
# We need to know whether to use %d or %ld for writing out MPI_Aint values
AC_CHECK_SIZEOF(int,$CROSS_SIZEOF_INT)
AC_CHECK_SIZEOF(long,$CROSS_SIZEOF_LONG)
AC_CHECK_SIZEOF(long long,$CROSS_SIZEOF_LONG)
AC_CHECK_SIZEOF(void *,$CROSS_SIZEOF_VOID_P)
if test "$ac_cv_sizeof_int" = "$ac_cv_sizeof_void_p" ; then
    :
elif test "$ac_cv_sizeof_long" = "$ac_cv_sizeof_void_p" ; then
    AC_DEFINE(MPI_AINT_IS_LONG_INT,1,[Define if the size of an MPI_Aint is long])
elif test "$ac_cv_sizeof_long_long" = "$ac_cv_sizeof_void_p" ; then
    AC_DEFINE(MPI_AINT_IS_LONG_LONG_INT,1,[Define if the size of an MPI_Aint is long long])
fi

#
# bring in the default implementation of the MPID_Thread package
#
MPID_THREAD_SRCDIR="../common/thread"
builtin(include,../common/thread/mpid_thread.m4)

AC_SUBST(AR)
AC_SUBST(RANLIB)
AC_SUBST(MPILIBNAME)
AC_SUBST(CC)
AC_SUBST(DEFS)
AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
PAC_CC_SUBDIR_SHLIBS
AC_SUBST(master_top_srcdir)
# Master_top_builddir is imported from the top; use it for the "top"
AC_SUBST(master_top_builddir)
export master_top_builddir
dnl Dependency handling
AC_SUBST(MAKE_DEPEND_C)
dnl Etags
AC_CHECK_PROGS(ETAGS,etags,true)
AC_SUBST(ETAGS)
AC_SUBST(ETAGSADD)
export ETAGS
export ETAGSADD

AC_SUBST(EXTRA_LIBS)

PAC_SUBDIR_CACHE

PAC_UPDATE_BASE_CACHE

AC_CONFIG_SUBDIRS(channels/${channel_name})

AC_OUTPUT(Makefile src/Makefile util/Makefile util/sock/Makefile util/shm/Makefile util/shmbase/Makefile channels/Makefile localdefs $MPID_THREAD_OUTPUT_FILES)

PAC_SUBDIR_CACHE_CLEANUP
