#! /bin/sh -e

# DP:  * libltdl.dpatch: Search the versioned extension, when loading a shared
# DP:    library (i.e. gij looks for .so.5 for loading additional libraries
# DP:    like the GTK based AWT peers.

dir=
if [ $# -eq 3 -a "$2" = '-d' ]; then
    pdir="-d $3"
    dir="$3/"
elif [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch)
        patch $pdir -f --no-backup-if-mismatch -p0 < $0
        #cd ${dir}gcc && autoconf
        ;;
    -unpatch)
        patch $pdir -f --no-backup-if-mismatch -R -p0 < $0
        #rm ${dir}gcc/configure
        ;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0

Index: libltdl/acinclude.m4
===================================================================
RCS file: /cvs/gcc/gcc/libjava/libltdl/acinclude.m4,v
retrieving revision 1.4.8.1
diff -u -r1.4.8.1 acinclude.m4
--- libjava/libltdl/acinclude.m4	24 Jun 2004 05:31:44 -0000	1.4.8.1
+++ libjava/libltdl/acinclude.m4	7 Sep 2004 18:14:54 -0000
@@ -6091,6 +6091,9 @@
 if test -n "$libltdl_cv_shlibext"; then
   AC_DEFINE_UNQUOTED(LTDL_SHLIB_EXT, "$libltdl_cv_shlibext",
     [Define to the extension used for shared libraries, say, ".so".])
+  libltdl_cv_shlibversionext=.so.`grep -v '^\#' ${srcdir}/../libtool-version | awk -F: '{ print $'1' }'`
+  AC_DEFINE_UNQUOTED(LTDL_SHLIB_VERSION_EXT, "$libltdl_cv_shlibversionext",
+    [Define to the versioned extension used for shared libraries, say, ".so.5".])
 fi
 ])# AC_LTDL_SHLIBEXT
 
Index: libltdl/config-h.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/libltdl/config-h.in,v
retrieving revision 1.3.16.1
diff -u -r1.3.16.1 config-h.in
--- libjava/libltdl/config-h.in	24 Jun 2004 05:31:45 -0000	1.3.16.1
+++ libjava/libltdl/config-h.in	7 Sep 2004 18:14:54 -0000
@@ -158,6 +158,10 @@
 /* Define to the extension used for shared libraries, say, ".so". */
 #undef LTDL_SHLIB_EXT
 
+/* Define to the versioned extension used for shared libraries, say, ".so.5".
+   */
+#undef LTDL_SHLIB_VERSION_EXT
+
 /* Define to the system default library search path. */
 #undef LTDL_SYSSEARCHPATH
 
Index: libltdl/configure
===================================================================
RCS file: /cvs/gcc/gcc/libjava/libltdl/configure,v
retrieving revision 1.10.8.1
diff -u -r1.10.8.1 configure
--- libjava/libltdl/configure	24 Jun 2004 05:31:46 -0000	1.10.8.1
+++ libjava/libltdl/configure	7 Sep 2004 18:15:01 -0000
@@ -19623,6 +19623,12 @@
 #define LTDL_SHLIB_EXT "$libltdl_cv_shlibext"
 _ACEOF
 
+  libltdl_cv_shlibversionext=.so.`grep -v '^\#' ${srcdir}/../libtool-version | awk -F: '{ print $'1' }'`
+
+cat >>confdefs.h <<_ACEOF
+#define LTDL_SHLIB_VERSION_EXT "$libltdl_cv_shlibversionext"
+_ACEOF
+
 fi
 
 
Index: libltdl/ltdl.c
===================================================================
RCS file: /cvs/gcc/gcc/libjava/libltdl/ltdl.c,v
retrieving revision 1.7
diff -u -r1.7 ltdl.c
--- libjava/libltdl/ltdl.c	25 Dec 2003 19:33:06 -0000	1.7
+++ libjava/libltdl/ltdl.c	7 Sep 2004 18:15:02 -0000
@@ -859,6 +859,7 @@
 static	const char	objdir[]		= LTDL_OBJDIR;
 static	const char	archive_ext[]		= LTDL_ARCHIVE_EXT;
 #ifdef	LTDL_SHLIB_EXT
+static	const char	shlib_version_ext[]	= LTDL_SHLIB_VERSION_EXT;
 static	const char	shlib_ext[]		= LTDL_SHLIB_EXT;
 #endif
 #ifdef	LTDL_SYSSEARCHPATH
@@ -2388,17 +2389,14 @@
   cur = *handle;
   if (filename)
     {
-      /* Comment out the check of file permissions using access.
-	 This call seems to always return -1 with error EACCES.
-      */
       /* We need to catch missing file errors early so that
-	 file_not_found() can detect what happened.
+	 file_not_found() can detect what happened. */
       if (access (filename, R_OK) != 0)
 	{
 	  LT_DLMUTEX_SETERROR (LT_DLSTRERROR (FILE_NOT_FOUND));
 	  ++errors;
 	  goto done;
-	} */
+	}
 
       cur->info.filename = lt_estrdup (filename);
       if (!cur->info.filename)
@@ -3425,10 +3423,11 @@
   return 0;
 }
 
-/* If FILENAME has an ARCHIVE_EXT or SHLIB_EXT extension, try to
-   open the FILENAME as passed.  Otherwise try appending ARCHIVE_EXT,
-   and if a file is still not found try again with SHLIB_EXT appended
-   instead.  */
+/* If FILENAME has an ARCHIVE_EXT, SHLIB_VERSION_EXT or SHLIB_EXT
+   extension, try to open the FILENAME as passed.  Otherwise try
+   appending ARCHIVE_EXT, and if a file is still not found try again
+   with SHLIB_VERSION_EXT appended.  If the file is still not found,
+   try again with SHLIB_EXT appended.  */
 lt_dlhandle
 lt_dlopenext (filename)
      const char *filename;
@@ -3453,6 +3452,7 @@
      to try appending additional extensions.  */
   if (ext && ((strcmp (ext, archive_ext) == 0)
 #ifdef LTDL_SHLIB_EXT
+	      || (strcmp (ext, shlib_version_ext) == 0)
 	      || (strcmp (ext, shlib_ext) == 0)
 #endif
       ))
@@ -3460,6 +3460,29 @@
       return lt_dlopen (filename);
     }
 
+#ifdef LTDL_SHLIB_EXT
+  /* First try appending SHLIB_VERSION_EXT.   */
+  tmp = LT_EMALLOC (char, len + LT_STRLEN (shlib_version_ext) + 1);
+  if (!tmp)
+    return 0;
+
+  strcpy (tmp, filename);
+  strcat(tmp, shlib_version_ext);
+  errors = try_dlopen (&handle, tmp);
+
+  /* As before, if the file was found but loading failed, return now
+     with the current error message.  */
+  if (handle || ((errors > 0) && !file_not_found ()))
+    {
+      LT_DLFREE (tmp);
+      return handle;
+    }
+  else
+    {
+      LT_DLFREE (tmp);
+    }
+#endif
+
   /* First try appending ARCHIVE_EXT.  */
   tmp = LT_EMALLOC (char, len + LT_STRLEN (archive_ext) + 1);
   if (!tmp)
@@ -3482,7 +3505,7 @@
 
 #ifdef LTDL_SHLIB_EXT
   /* Try appending SHLIB_EXT.   */
-  if (LT_STRLEN (shlib_ext) > LT_STRLEN (archive_ext))
+  if (LT_STRLEN (shlib_ext) > LT_STRLEN (shlib_version_ext))
     {
       LT_DLFREE (tmp);
       tmp = LT_EMALLOC (char, len + LT_STRLEN (shlib_ext) + 1);
