#!/bin/sh

# no ROOT given, thus no dynliker
[ $# -lt 1 ] && exit 0
[ $# -ge 2 -a "$2" = "--print-options" ] && PRINT_OPTIONS=1

ROOT="$1"
MACHINE="$(uname -m)"

DYNLIST="ld-linux.so.2"

[ '(' "$MACHINE" = 'x86_64' -o "$MACHINE" = 'i686' ')' ] && DYNLIST="i686/cmov/ld-linux.so.2 $DYNLIST"
[ "$MACHINE" = 'x86_64' ] && DYNLIST="ld-linux-x86-64.so.2 $DYNLIST"

if [ "$ROOT" -a -d "$ROOT" ]; then
    for dynld in $DYNLIST; do
	ldpath="$ROOT/lib/$dynld"
	if [ -x "$ldpath" ]; then
	    [ "$PRINT_OPTIONS" ] && $ldpath 2>&1 | egrep -q '.--rpath-prefix' && ldpath="$ldpath --rpath-prefix $ROOT"
	    DYNLD="$ldpath"
	    break
	fi
    done
fi

echo "$DYNLD"
