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['_']main | sed 'y%./+-%__p_%'`
     gem_system_lib=no
     AC_CHECK_LIB($1,main,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\``

            dnl this time it should work

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

            AC_TRY_LINK(,"main() { $testfun(); }",gem_cv_lib_$1=$twice_lib,
               AC_MSG_ERROR("library $1 not available - try makeauxlibs")
            )
            LIBS=$save_LIBS
            LDFLAGS=$save_LDFLAGS
        )
         echo ${gem_cv_lib_$1}

         if eval "test \"`echo '$''{'gem_cv_lib_$1'+set}'`\" = set"; then
          AC_MSG_WARN("Trying to use $1 library in ${gem_cv_lib_$1}");
           LIBS="-l$1 $LIBS"                                                                                             LDFLAGS="$LDFLAGS -L${gem_cv_lib_$1}";                                                                   
	 else
          AC_MSG_WARN("Build $1 library somewhere");
         fi
     else
	LIBS="-l$1 $LIBS" 
     fi 


)

AC_DEFUN(AC_PATH_INCLUDE,
     AC_MSG_CHECKING(for $1 path)
     tmp_path=""
     tmp_path=`dirname \`find $2 -name "$1" | sed 1q\``
     if [test "$tmp_path" = ""]; then
       AC_MSG_ERROR("can\'t find path to $1")
     else
       AC_MSG_RESULT($tmp_path)
       INCLUDES="-I`pwd`/$tmp_path $INCLUDES";
     fi
)
