#!/bin/bash
if [ -n "$LD_LIBRARY_PATH" ]; then
  # arguments as file
  if [ "${1:0:1}" = "@" ]; then
    FILE="${1:1}"
    grep -v -- "--sysroot=" "$FILE" > "$FILE.fixed"
    mv "$FILE.fixed" "$FILE"
  fi

  # cross ld does not work correctly using LD_LIBRARY_PATH, use arm version
  args=()
  for i in "$@"; do
    if [ "${i:0:10}" != "--sysroot=" ]; then
      args=(${args[@]} "$i")
    fi
  done
  exec /usr/bin/qemu-arm /usr/bin/ld "${args[@]}"
fi
for i in "$@"; do
  if [ "${i:0:10}" = "--sysroot=" ]; then
    exec -a "$0" /emul/i586-for-arm/usr/arm-tizen-linux-gnueabi/bin/ld.real "$@"
  fi
done

exec -a "$0" /emul/i586-for-arm/usr/arm-tizen-linux-gnueabi/bin/ld.real --sysroot=/ "$@"

