#!/bin/sh

basedir=$(dirname "$0")

add_LD_LIBRARY_PATH() {
  if test -z "$LD_LIBRARY_PATH"
  then
    LD_LIBRARY_PATH="$1"
  else
    LD_LIBRARY_PATH="$1:$LD_LIBRARY_PATH"
  fi
}

# Bug 27552: On CentOS/RHEL 6, we need to add the firefox bundled dir
# to LD_LIBRARY_PATH
if test -f /etc/system-release-cpe
then
  if test "$(cut -d : -f 3 /etc/system-release-cpe)" = centos || \
	test "$(cut -d : -f 3 /etc/system-release-cpe)" = "enterprise_linux"
  then
    if test "$(cut -d : -f 5 /etc/system-release-cpe)" = "6"
    then
      if test -d /usr/lib64/firefox/bundled/lib64
      then
        add_LD_LIBRARY_PATH /usr/lib64/firefox/bundled/lib64
      elif test -d /usr/lib/firefox/bundled/lib
      then
        add_LD_LIBRARY_PATH /usr/lib/firefox/bundled/lib
      else
        echo "Error: the firefox package (version 60 or more) is not installed." >&2
        echo "On CentOS/RHEL 6, Tor Browser requires the firefox package to be installed." >&2
        exit 1
      fi
    fi
  fi
fi

# Check if the system has a more recent version of libstdc++.so.6; if yes, use
# that instead of the bundled version.
"$basedir/abicheck" >/dev/null 2>&1
if [ $? -ne 0 ]; then
  add_LD_LIBRARY_PATH "$basedir/TorBrowser/Tor/libstdc++/"
fi
add_LD_LIBRARY_PATH "$basedir/TorBrowser/Tor/"
export LD_LIBRARY_PATH

exec "$basedir/firefox.real" "$@"
