# -*- 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-mvapi, 
        AC_HELP_STRING([--with-btl-mvapi=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-mvapi-libdir,
        AC_HELP_STRING([--with-btl-mvapi-libdir=IBLIBDIR],
                       [directory where the IB library can be found, if it is not in $IBDIR/lib or $IBDIR/lib64]))

    # Quick sanity check

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

    # Find the right IBDIR

    if test "$with_btl_mvapi" != "" -a "$with_btl_mvapi" != "yes" ; then
        IBDIR="$with_btl_mvapi"
        IBLIBDIR="$with_btl_mvapi"
    fi
    if test "$with_btl_mvapi_libdir" != "" -a "$with_btl_mvapi_libdir" != "yes" -a \
        "$with_btl_mvapi_libdir" != "no"; then
        IBLIBDIR="$with_btl_mvapi_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 ib btl anyway...])
        fi
        if test "$IBDIR" != "" -a -d "$IBDIR/wrap"; then
            EXTRA_CPPFLAGS="-I$IBDIR/wrap $EXTRA_CPPFLAGS"
        else
            AC_MSG_WARN([*** Warning: cannot find $IBDIR/wrap])
            AC_MSG_WARN([*** Will still try to configure ib btl anyway...])
        fi
    fi

    # See if we can find vapi.h

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

    # 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

    # Helpfer function to try to find libvapi (called from below).  In
    # some versions of Mellanox (v3.1), we need to expliitly link in
    # the thread libraries.  #$%#@$%@%#$!!!

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

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



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

    LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
    vapi_badness=
    AC_CHECK_LIB([vapi], [$func1], [], [vapi_badness=true],
                 [-lmtl_common -lmpga -lmosal])
    if test "$vapi_badness" != ""; then
         AC_CHECK_LIB([pthread], [pthread_create], 
		      [pthread=yes LIBS="$LIBS -lpthread"],
		      [pthread=no])
	 if test "$pthread" = "yes"; then
	     AC_CHECK_LIB([vapi], [$func2], [], [],
                          [-lmtl_common -lmpga -lmosal])
	fi
    fi    
}

    # The libraries may be in $IBDIR/lib or $IBDIR/lib64.  Try them
    # both.

    LIBS_save="$LIBS"
    LDFLAGS_save="$LDFLAGS"
    LIBS="$LIBS -lmosal -lmpga -lmtl_common"
    LIBS_orig="$LIBS"

    EXTRA_LDFLAGS=
    if test -d "$IBLIBDIR/lib"; then
        EXTRA_LDFLAGS="-L$IBLIBDIR/lib"
        LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
        mca_btl_mvapi_try_find_libvapi VAPI_open_hca VAPI_query_hca_cap
        if test "$LIBS" != "$LIBS_orig"; then
            echo "--> found libvapi in $IBLIBDIR/lib"
        fi
    fi

    if test "$LIBS" = "$LIBS_orig" -a -d "$IBLIBDIR/lib64"; then
        EXTRA_LDFLAGS="-L$IBLIBDIR/lib64"
        LDFLAGS="$LDFLAGS_save $EXTRA_LDFLAGS"
        mca_btl_mvapi_try_find_libvapi EVAPI_list_hcas EVAPI_open_hca
        if test "$LIBS" != "$LIBS_orig"; then
            echo "--> found libvapi in $IBLIBDIR/lib64"
        fi
    fi

    if test "$LIBS" = "$LIBS_orig"; then
      AC_MSG_ERROR([*** Cannot find working libvapi.])
    fi
    LD_LIBRARY_PATH="$LD_LIBRARY_PATH_save"
    LIBS="$LIBS -lmtl_common -lmpga"

    #
    # 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="-lvapi -lmtl_common -lmpga -lmosal"
])dnl