# -*- shell-script -*-
#
# Copyright (c) 2004-2005 The Trustees of Indiana University.
#                         All rights reserved.
# Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
#                         All rights reserved.
# Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
#                         University of Stuttgart.  All rights reserved.
# Copyright (c) 2004-2005 The Regents of the University of California.
#                         All rights reserved.
# $COPYRIGHT$
# 
# Additional copyrights may follow
# 
# $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(btl-openib, 
        AC_HELP_STRING([--with-btl-openib=IBDIR],
                       [Specify the installation directory of IB (should enable the correct automatic determination of using the 32 or 64 bit library, if both are present under IBDIR/lib and IBDIR/lib64)]))
    AC_ARG_WITH(btl-openib-libdir,
        AC_HELP_STRING([--with-btl-openib-libdir=IBLIBDIR],
                       [directory where the IB library can be found, if it is not in $IBDIR/lib or $IBDIR/lib/infiniband]))

    # Quick sanity check

    if test "$with_btl_openib" = "no"; then
        AC_MSG_WARN([*** --without-btl-openib specified -- aborting])
        AC_MSG_ERROR([Will not continue])
    fi

    # Find the right IBDIR

    if test "$with_btl_openib" != "" -a "$with_btl_openib" != "yes" ; then
        IBDIR="$with_btl_openib"
        IBLIBDIR="$with_btl_openib"
    fi
    if test "$with_btl_openib_libdir" != "" -a "$with_btl_openib_libdir" != "yes" -a \
        "$with_btl_openib_libdir" != "no"; then
        IBLIBDIR="$with_btl_openib_libdir"
    fi

    # Add to CPPFLAGS if necessary

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

    # See if we can find verbs.h.  First try without any additional
    # -I's to see if we can find it easily.  If we don't find it, then
    # try again with the EXTRA_CPPFLAGS.  This prevents us from adding
    # things like -I/usr/local if we don't need to.

    AC_CHECK_HEADERS(infiniband/verbs.h,,
        [CPPFLAGS="$CPPFLAGS $EXTRA_CPPFLAGS"
         eval "unset ac_cv_header_verbs_h"
         AC_CHECK_HEADERS(infiniband/verbs.h,,
             AC_MSG_ERROR([*** Cannot find working infiniband/verbs.h]))])

    # Many verbs.h's have horrid semantics and don't obey ISOC99
    # standards.  So we have to turn off flags like -pedantic.  Sigh.

# Galen: is this true for Open IB?

    #CFLAGS="`echo $CFLAGS | sed 's/-pedantic//g'`"

    # Note that it is possible to find the library even if -L is not
    # specified, if the LD_LIBRARY_PATH includes the directory where
    # the shared ib library is kept.  Hence, we unset LD_LIBRARY_PATH
    # before running this test.

    LD_LIBRARY_PATH_save="$LD_LIBRARY_PATH"
    unset LD_LIBRARY_PATH

    # Helper function to try to find libibverbs (called from below).

mca_btl_openib_try_find_lib() {
    func1=[$]1
    func2=[$]2

    LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
    ibverbs_badness=
    AC_CHECK_LIB([ibverbs], [$func1], [], [ibverbs_badness=true],
	[-libcm])
    if test "$ibverbs_badness" != ""; then
	AC_TRY_LINK([#include <pthread.h>], [pthread_create(0,0,0,0);], 
	    [pthreads="yes"], 
	    [pthreads="no"])
	AC_CHECK_LIB(sysfs, sysfs_open_class, 
	    [sysfs=yes LIBS="$LIBS -lsysfs"], 
	    [sysfs=no])
	if test "$pthread" = "yes" && test "$sysfs" = "yes"; then
	    AC_CHECK_LIB([ibverbs], [$func2], [], [],
		[-libcm])
	fi
	
	
    fi    
}

    # Open IB does not appear to make lib64 variants -- just try the
    # normal "lib" directory.

    LIBS_save="$LIBS"
    LDFLAGS_save="$LDFLAGS"
# Galen: Are these the right extra libs?
    LIBS="$LIBS -libcm"
    LIBS_orig="$LIBS"

    EXTRA_LDFLAGS=
    if test -d "$IBLIBDIR/lib/infiniband"; then
        EXTRA_LDFLAGS="-L$IBLIBDIR/lib/infiniband -L$IBLIBDIR/lib -L$IBLIBDIR/lib/sysfs"
        LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
# Galen: are these the right symbol names?
        mca_btl_openib_try_find_lib ibv_get_devices ibv_open_device
        if test "$LIBS" != "$LIBS_orig"; then
# Galen: Are we looking for "libibverbs"?
            echo "--> found libibverbs libs in $IBLIBDIR/lib"
        fi
    fi

    if test "$LIBS" = "$LIBS_orig"; then
# Galen: Are we looking for "libibverbs"?
      AC_MSG_ERROR([*** Cannot find working libibverbs.])
    fi
    LD_LIBRARY_PATH="$LD_LIBRARY_PATH_save"
# Galen: Are these the right extra libs?
    LIBS="$LIBS -libcm"

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

    WRAPPER_EXTRA_LDFLAGS="$EXTRA_LDFLAGS"
# Galen: Are these the right extra libs?
    WRAPPER_EXTRA_LIBS="-libverbs -libcm"
])dnl