# -*- shell-script -*-
#
# $HEADER$
#

#
# Main function.  This will be invoked in the middle of the templated
# configure script.
#
AC_DEFUN([MCA_CONFIGURE_STUB],[

    # Additional --with flags that can be specified

    AC_ARG_WITH(ptl-gm, 
        AC_HELP_STRING([--with-ptl-gm=DIR],
                       [Specify the installation directory of GM]))
    AC_ARG_WITH(ptl-gm-libdir,
        AC_HELP_STRING([--with-ptl-gm-libdir=DIR],
                       [directory where the GM library can be found, if it is not in \$GMDIR/lib or \$GMDIR/binary/lib]))

    # Add to CPPFLAGS if necessary

    EXTRA_CPPFLAGS=
    if test -n "$with_ptl_gm"; then
        if test -d "$with_ptl_gm/include"; then
            EXTRA_CPPFLAGS="-I$with_ptl_gm/include"
        else
            AC_MSG_WARN([*** Warning: cannot find $with_ptl_gm/include])
            AC_MSG_WARN([*** Will still try to configure gm ptl anyway...])
        fi
    fi

    # See if we can find gm.h

    CPPFLAGS="$CPPFLAGS $EXTRA_CPPFLAGS"
    AC_CHECK_HEADERS(gm.h,,
        AC_MSG_ERROR([*** Cannot find working gm.h.]))

    # Add to LDFLAGS if necessary

    EXTRA_LDFLAGS=
    if test -n "$with_ptl_gm_libdir"; then
        if test -d "$with_ptl_gm_libdir/lib"; then
            EXTRA_LDFLAGS="-L$with_ptl_gm_libdir/lib"
        elif test -d "$with_ptl_bm_libdir/binary/lib"; then
            EXTRA_LDFLAGS="-L$with_ptl_gm_libdir/binary/lib"
        else
            AC_MSG_WARN([*** Warning: cannot find $with_ptl_gm_libdir/lib])
            AC_MSG_WARN([*** or $with_ptl_gm_libdir/binary/lib])
            AC_MSG_WARN([*** Will still try to configure gm ptl anyway...])
        fi
    elif test -n "$with_ptl_gm"; then
        if test -d "$with_ptl_gm/lib"; then
            EXTRA_LDFLAGS="-L$with_ptl_gm/lib"
        elif test -d "$with_ptl_bm/binary/lib"; then
            EXTRA_LDFLAGS="-L$with_ptl_gm/binary/lib"
        else
            AC_MSG_WARN([*** Warning: cannot find $with_ptl_gm/lib])
            AC_MSG_WARN([*** or $with_ptl_gm/binary/lib])
            AC_MSG_WARN([*** Will still try to configure gm ptl anyway...])
        fi
    fi

    # Try to find libgm

    LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
    AC_CHECK_LIB([gm], [main], [],
        AC_MSG_ERROR([*** Cannot find libgm]))

    #
    # See if we have GM_API_VERSION.  If we do, use it.  If not, find the
    # highest one available.  It seems that GM_API_VERSION was introduced
    # somewhere after 1.3 but before 1.6. :-\
    #

    AC_MSG_CHECKING(for GM_API_VERSION)
    AC_TRY_COMPILE([#include<gm.h>],
    [int foo = GM_API_VERSION;], 
        have_gm_api_ver_msg=yes gm_api_ver=GM_API_VERSION,
        have_gm_api_ver_msg=no  gm_api_ver="")
    AC_MSG_RESULT([$have_gm_api_ver_msg])
    if test "$gm_api_ver" = ""; then
        found=0
        for val in 5 4 3; do
            if test "$found" = "0"; then
                var="GM_API_VERSION_1_$val"
                AC_MSG_CHECKING(for $var)
                AC_TRY_COMPILE([#include<gm.h>],
                    [int foo = $var;], 
                    msg=yes found=1 gm_api_ver=$var,
                    msg=no found=0 gm_api_ver="")
                AC_MSG_RESULT($msg)
            fi
        done
    fi
    if test "$gm_api_ver" = ""; then
        AC_MSG_WARN([*** Could not find a supported GM_API_VERSION])
        AC_MSG_ERROR([*** Cannot continue])
    fi
    AC_DEFINE_UNQUOTED(OMPI_MCA_PTL_GM_API_VERSION, $gm_api_ver,
        [Version of the GM API to use])
    unset gm_api_ver have_gm_api_ver_msg found val msg

    #
    # Do we have gm_put()?
    # gm_put() was introduced in gm 2.0, and is exactly identical to gm
    # 1.6's gm_directed_send_with_callback().  The name was simply changed
    # for consistency/symmtery with gm_get().
    #

    AC_MSG_CHECKING([for gm_put()])
    AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include<gm.h>
    ]], 
    [[gm_put(0, 0, 0, 0, 0, 0, 0, 0, 0);]])], 
        [HAVE_RDMA_PUT=1 MSG=yes], 
        [HAVE_RDMA_PUT=0 MSG="no, use gm_directed_send_with_callback()"])
    AC_DEFINE_UNQUOTED(OMPI_MCA_PTL_GM_HAVE_RDMA_PUT, $HAVE_RDMA_PUT,
        [Whether we have gm_put() or gm_directed_send_with_callback()])
    AC_MSG_RESULT([$MSG])

    #
    # Do we have gm_get()?
    # gm_get() was introduced in gm 2.0.
    #

    AC_MSG_CHECKING([for gm_get()])
    AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include<gm.h>
    ]], 
    [[gm_get(0, 0, 0, 0, 0, 0, 0, 0, 0);]])], 
        [HAVE_RDMA_GET=1 MSG=yes], 
        [HAVE_RDMA_GET=0 MSG=no])
    AC_DEFINE_UNQUOTED(OMPI_MCA_PTL_GM_HAVE_RDMA_GET, $HAVE_RDMA_GET,
        [Whether we have get_get() or not])
    AC_MSG_RESULT([$MSG])

    #
    # Save extra compiler/linker flags so that they can be added in
    # the wrapper compilers, if necessary
    #

    WRAPPER_EXTRA_LDFLAGS="$EXTRA_LDFLAGS"
    WRAPPER_EXTRA_LIBS="-lgm"
])dnl