dnl
dnl Check for a library in the system or local 
dnl arguments: 1: the librabry name without lib 2: the path to start searching
dnl for the local lib


AC_DEFUN(AC_CHECK_LIB_TWICE,

     testfun=main
     ac_lib_var=`echo $1['_']$testfun | sed 'y%./+-%__p_%'`

     AC_CHECK_LIB($1,$testfun,gem_system_lib=yes)

     if eval "test \"`echo '$ac_cv_lib_'$ac_lib_var`\" = no" && \
              test "${gem_system_lib-no}" != "yes"; then
        AC_MSG_CHECKING("alternate -l$1 in $2")
        AC_CACHE_VAL(
            gem_cv_lib_$1,
            twice_lib=`dirname \`find $2 -name "lib$1.a" | sed 1q\``

            LDFLAGS="$LDFLAGS -L$twice_lib";

            dnl this time it should work

            save_LIBS=$LIBS
            LIBS="-l$1 $LIBS"
            LDFLAGS="$LDFLAGS -L$twice_lib";

            AC_TRY_LINK(,"main() { $testfun(); }",gem_cv_lib_$1=yes,
               AC_MSG_ERROR("library $1 not available in system or local")
            )
         )
         echo ${gem_cv_lib_$1}
         if eval "test \"`echo '$gem_cv_lib_'$1`\" = yes"; then
          AC_MSG_WARN("Trying to use $1 library in $twice_lib");
         fi
      fi


)

