SDL_image: Sort libraries by version and pick the shortest symlink (thanks @jpalus!)

From 79f99950ca2ad83eb0c18df86fc397398e8a20a6 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 12 Jul 2022 13:02:31 -0700
Subject: [PATCH] Sort libraries by version and pick the shortest symlink
 (thanks @jpalus!)

Fixes https://github.com/libsdl-org/SDL_image/issues/289
---
 configure    | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 configure.ac |  7 ++++++-
 2 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index 69959324..2a0dda0a 100755
--- a/configure
+++ b/configure
@@ -11887,6 +11887,53 @@ END
 fi
 
 
+for ac_prog in gawk mawk nawk awk
+do
+  # Extract the first word of "$ac_prog", so it can be a program name with args.
+set dummy $ac_prog; ac_word=$2
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+printf %s "checking for $ac_word... " >&6; }
+if test ${ac_cv_prog_AWK+y}
+then :
+  printf %s "(cached) " >&6
+else $as_nop
+  if test -n "$AWK"; then
+  ac_cv_prog_AWK="$AWK" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  case $as_dir in #(((
+    '') as_dir=./ ;;
+    */) ;;
+    *) as_dir=$as_dir/ ;;
+  esac
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if as_fn_executable_p "$as_dir$ac_word$ac_exec_ext"; then
+    ac_cv_prog_AWK="$ac_prog"
+    printf "%s\n" "$as_me:${as_lineno-$LINENO}: found $as_dir$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+fi
+fi
+AWK=$ac_cv_prog_AWK
+if test -n "$AWK"; then
+  { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $AWK" >&5
+printf "%s\n" "$AWK" >&6; }
+else
+  { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+fi
+
+
+  test -n "$AWK" && break
+done
+
 ac_ext=c
 ac_cpp='$CPP $CPPFLAGS'
 ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
@@ -13341,6 +13388,10 @@ printf "%s\n" "no" >&6; }
 	fi
 fi
 
+if  test -z "$AWK" ; then
+    as_fn_error $? "*** awk not found, aborting" "$LINENO" 5
+fi
+
 case "$host" in
     *-*-beos*)
         ac_default_prefix=/boot/develop/tools/gnupro
@@ -13426,7 +13477,7 @@ find_lib()
         host_lib_path="$ac_default_prefix/$base_libdir $ac_default_prefix/$base_bindir /usr/$base_libdir /usr/local/$base_libdir"
     fi
     for path in $env_lib_path $gcc_bin_path $gcc_lib_path $host_lib_path; do
-        lib=`ls -- $path/$1 2>/dev/null | sed -e '/\.so\..*\./d' -e 's,.*/,,' | sort | tail -1`
+        lib=`ls -- $path/$1 2>/dev/null | sed 's,.*/,,' | sort -V -r | $AWK 'BEGIN{FS="."}{ print NF, $0 }' | sort -n -s | sed 's,[0-9]* ,,' | head -1`
         if test x$lib != x; then
             echo $lib
             return
diff --git a/configure.ac b/configure.ac
index ca1013ae..221fd86a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -61,6 +61,7 @@ dnl Setup for automake
 AM_INIT_AUTOMAKE([foreign tar-ustar])
 
 dnl Check for tools
+AC_PROG_AWK
 AC_PROG_CC
 AC_PROG_OBJC
 AC_CHECK_TOOL(RC,[windres],[:])
@@ -68,6 +69,10 @@ AC_PROG_INSTALL
 AC_PROG_MAKE_SET
 PKG_PROG_PKG_CONFIG
 
+if [ test -z "$AWK" ]; then
+    AC_MSG_ERROR([*** awk not found, aborting])
+fi
+
 case "$host" in
     *-*-beos*)
         ac_default_prefix=/boot/develop/tools/gnupro
@@ -132,7 +137,7 @@ find_lib()
         host_lib_path="$ac_default_prefix/$base_libdir $ac_default_prefix/$base_bindir /usr/$base_libdir /usr/local/$base_libdir"
     fi
     for path in $env_lib_path $gcc_bin_path $gcc_lib_path $host_lib_path; do
-        lib=[`ls -- $path/$1 2>/dev/null | sed -e '/\.so\..*\./d' -e 's,.*/,,' | sort | tail -1`]
+        lib=[`ls -- $path/$1 2>/dev/null | sed 's,.*/,,' | sort -V -r | $AWK 'BEGIN{FS="."}{ print NF, $0 }' | sort -n -s | sed 's,[0-9]* ,,' | head -1`]
         if test x$lib != x; then
             echo $lib
             return