#! /bin/sh -e
## 999_lib64_for_cross.dpatch by Nikita Youshchenko <yoush@cs.msu.su>
##
## DP: Add /usr/${target-alias}/lib${LIBPATH_SUFFIX} to ld's default
## DP: library search path for cross targets. Needed for cross targets that
## DP: try to support both 32bit and 64bit emulations.

if [ $# -lt 1 ]; then
    echo "`basename $0`: script expects -patch|-unpatch as argument" >&2
    exit 1
fi

[ -f debian/patches/00patch-opts ] && . debian/patches/00patch-opts
patch_opts="${patch_opts:--f --no-backup-if-mismatch} ${2:+-d $2}"

case "$1" in
    -patch) patch -p1 ${patch_opts} < $0;;
    -unpatch) patch -R -p1 ${patch_opts} < $0;;
    *)
        echo "`basename $0`: script expects -patch|-unpatch as argument" >&2
        exit 1;;
esac

exit 0

@DPATCH@
diff -urNad /home/nikita/debian/binutils/binutils-2.14.90.0.7.cross/ld/genscripts.sh binutils-2.14.90.0.7.cross/ld/genscripts.sh
--- /home/nikita/debian/binutils/binutils-2.14.90.0.7.cross/ld/genscripts.sh	2003-10-29 20:37:48.000000000 +0300
+++ binutils-2.14.90.0.7.cross/ld/genscripts.sh	2004-07-15 00:29:19.000000000 +0400
@@ -176,6 +176,26 @@
   ::) LIB_PATH=${tool_lib} ;;
   *) LIB_PATH=${tool_lib}:${LIB_PATH} ;;
   esac
+  # For multilib'ed targets, ensure both ${target_alias}/lib${LIBPATH_SUFFIX}
+  # and ${TOOL_LIB}/lib${LIBPATH_SUFFIX} to default search path, because
+  # 64bit libraries may be in both places, depending on cross-development
+  # setup method (e.g.: /usr/s390x-linux/lib64 vs /usr/s390-linux/lib64)
+  case "${LIBPATH_SUFFIX}:${tool_lib}" in
+    :*) ;;
+    *:*${LIBPATH_SUFFIX}) ;;
+    *)
+      paths="${exec_prefix}/${target_alias}/lib${LIBPATH_SUFFIX}"
+      if [ "${TOOL_LIB}" != x ]; then
+        paths="${paths} ${exec_prefix}/${TOOL_LIB}/lib${LIBPATH_SUFFIX}"
+      fi
+      for path in $paths; do
+        case :${LIB_PATH}: in
+          ::: | *:${path}:*) ;;
+          *) LIB_PATH=${path}:${LIB_PATH} ;;
+        esac
+      done
+    ;;
+  esac
 fi
 
 LIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'`
