#!/bin/sh

LINKER="$1"
shift
RPATH=""
LDOPTS=""

#Special hack for HP-UX
LD_PXDB=/dev/null
export LD_PXDB

#Special hack for SGI
if test "x$SGI_ABI" = "x"; then
  SGI_ABI=-n32
fi
export SGI_API

#set -x

SET_N32=""

while test "$#" != 0; do
  case $1 in
    -R*)
     if test x$1 = x-R ; then
       tmp="$2"
       shift
     else
       tmp=`echo $1 | sed -e 's/^-R//g'`
     fi
     RPATH="$RPATH:$tmp"
    ;;
    -n32)
     SET_N32="-n32"
    ;;
    *)
    LDOPTS="$LDOPTS $1"
  esac
  shift
done

LDOPTS="$SET_N32 $LDOPTS"

RPATH="$RPATH:${LD_LIBRARY_PATH-}"

OLDIFS="$IFS"
IFS=:
set dummy $RPATH
shift
IFS="$OLDIFS"

while test x$1 = x -a x$# != x0; do
  shift
done

if test -x /usr/bin/uname ; then
  UNAME=`/usr/bin/uname -sr`
elif test -x /bin/uname ; then
  UNAME=`/bin/uname -sr`
else
  UNAME=unknown
fi

if test x$# != x0 ; then

  done="::$1:"
  RPATH="$1"
  shift

  while test x$# != x0 ;
  do
    case $done in
      *:$1:*) ;;
      *)
       RPATH="$RPATH:$1"
       done="$done$1:"
      ;;
    esac
    shift
  done

  case "$LINKER $LDOPTS " in
    *\ -c\ * | cpp\ * | *\ -E\ *)
      LINKING=no
    ;;

    *)
      LINKING=maybe
    ;;
  esac

  case "$UNAME" in
   OSF1\ V4.*)
     LDOPTS="$LDOPTS -rpath $RPATH"
   ;;
   SunOS\ 5.*)
     LDOPTS="$LDOPTS -R$RPATH"
   ;;
   Linux\ 2.*)
     if test x$LINKING != xno -a "x$RPATH" != x ; then
      LDOPTS="$LDOPTS -Wl,-rpath,$RPATH"
     fi
   ;;
   *)
    LD_LIBRARY_PATH="$RPATH"
    export LD_LIBRARY_PATH
   ;;
  esac
fi

#case "x$UNAME" in
#  AIX*)
#    # AIX cc doesn't work with ulimit set
#    :
#  ;;
#  *)
#    if test "x$NO_ULIMIT" = "x"; then
#      # Limit data segment size to 48MB.
#      ulimit -d 49152 >/dev/null 2>&1
#    fi
#  ;;
#esac

# Used to be exec, but Digital-Unix's stupid sh has buggy variable-expansion.
# /grubba
eval $LINKER $LDOPTS
