#!/bin/bash
#
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Let the wrapped binary know that it has been run through the wrapper.
export CHROME_WRAPPER="`readlink -f "$0"`"

HERE="`dirname "$CHROME_WRAPPER"`"

# We include some xdg utilities next to the binary, and we want to prefer them
# over the system versions when we know the system versions are very old. We
# detect whether the system xdg utilities are sufficiently new to be likely to
# work for us by looking for xdg-settings. If we find it, we leave $PATH alone,
# so that the system xdg utilities (including any distro patches) will be used.
if ! which xdg-settings &> /dev/null; then
  # Old xdg utilities. Prepend $HERE to $PATH to use ours instead.
  export PATH="$HERE:$PATH"
else
  # Use system xdg utilities. But first create mimeapps.list if it doesn't
  # exist; some systems have bugs in xdg-mime that make it fail without it.
  xdg_app_dir="${XDG_DATA_HOME:-$HOME/.local/share/applications}"
  mkdir -p "$xdg_app_dir"
  [ -f "$xdg_app_dir/mimeapps.list" ] || touch "$xdg_app_dir/mimeapps.list"
fi

# Find libffmpeg.so with additional codecs.
CHR_CODECS_CL="/usr/share/doc/chromium-codecs-ffmpeg-extra/changelog.Debian.gz"
if [ -r "$CHR_CODECS_CL" ]; then
  if [ -r "/usr/lib/chromium-browser/libs/libffmpeg.so" ]; then
    LIBFFMPEG_PATH="/usr/lib/chromium-browser/libs"
  elif [ -r "/usr/lib/chromium-browser/libffmpeg.so" ]; then
    LIBFFMPEG_PATH="/usr/lib/chromium-browser"
  fi
  if [[ -n "$LIBFFMPEG_PATH" ]]; then
    CHR_CODECS_VER=`gzip -cd "$CHR_CODECS_CL" |
      sed -n '1s/^chromium-browser (\([0-9]\+\.\)\{2\}\([0-9]\+\)\.[0-9]\+-.*/\2/p'`
  fi
elif [ -r "/etc/os-release" ]; then
  # Check distro, to allow distro specific tests to locate a suitable libffmpeg.so
  . /etc/os-release
  # os-release does not mandate ID_LIKE so set it to something (if empty), to
  # avoid having an empty variable.
  ID_LIKE="${ID_LIKE:-unknown}"
  set_lib_dir_suf()
  {
    case "`uname -m`" in
      x86_64) LIB_DIR_SUFFIX="64" ;;
           *) LIB_DIR_SUFFIX="" ;;
    esac
  }
  # OpenSUSE, SLES and derivatives will all have an ID_LIKE that includes "suse"
  if [[ "$ID_LIKE" =~ "suse" ]]; then
    set_lib_dir_suf
    # chromium-ffmpeg recently changed the path for libffmpeg.so, so we need to
    # check both locations
    if [ -r "/usr/lib$LIB_DIR_SUFFIX/chromium/libffmpeg.so" ]; then
      LIBFFMPEG_PATH=/usr/lib$LIB_DIR_SUFFIX/chromium
    elif [ -r "/usr/lib$LIB_DIR_SUFFIX/chromium/lib/libffmpeg.so" ]; then
      LIBFFMPEG_PATH=/usr/lib$LIB_DIR_SUFFIX/chromium/lib
    fi
    if [[ -n "$LIBFFMPEG_PATH" ]]; then
      # Query RPM database and set the version if chromium-ffmpeg is installed.
      CHR_CODECS_VER=`rpm -qf "$LIBFFMPEG_PATH/libffmpeg.so" |
        sed -n 's/^chromium-ffmpeg-\([0-9]\+\.\)\{2\}\([0-9]\+\)\.[0-9]\+-.*/\2/p'`
    fi
  fi
fi
if [[ -n "$CHR_CODECS_VER" ]] && [ "$CHR_CODECS_VER" -ge "2564" ]; then
  # Seperate libffmpeg.so from the rest of the libs, as we do not want to prefer
  # the others to our own.
  VIVALDI_LOCAL_LIBS="$HOME/.local/lib/vivaldi"
  if [ ! -r "$VIVALDI_LOCAL_LIBS/libffmpeg.so" ]; then
    mkdir -p "$VIVALDI_LOCAL_LIBS"
    ln -fs "$LIBFFMPEG_PATH/libffmpeg.so" "$VIVALDI_LOCAL_LIBS/libffmpeg.so"
  fi
  FFMPEG_LD_LIBRARY_PATH=":$VIVALDI_LOCAL_LIBS"
fi

# Set LD_LIBRARY_PATH to prefer our libs, excluding $HERE/lib/libffmpeg.so if
# an alternative ffmpeg was found.
if [[ -n "$LD_LIBRARY_PATH" ]]; then
  LD_LIBRARY_PATH="$HERE$FFMPEG_LD_LIBRARY_PATH:$HERE/lib:$LD_LIBRARY_PATH"
else
  LD_LIBRARY_PATH="$HERE$FFMPEG_LD_LIBRARY_PATH:$HERE/lib"
fi
export LD_LIBRARY_PATH

export CHROME_VERSION_EXTRA="stable"

# We don't want bug-buddy intercepting our crashes. http://crbug.com/24120
export GNOME_DISABLE_CRASH_DIALOG=SET_BY_GOOGLE_CHROME

# Sanitize std{in,out,err} because they'll be shared with untrusted child
# processes (http://crbug.com/376567).
exec < /dev/null
exec > >(exec cat)
exec 2> >(exec cat >&2)

# Make sure that the profile directory specified in the environment, if any,
# overrides the default.
if [[ -n "$CHROME_USER_DATA_DIR" ]]; then
  # Note: exec -a below is a bashism.
  exec -a "$0" "$HERE/vivaldi-bin"  \
    --user-data-dir="$CHROME_USER_DATA_DIR" "$@"
else
  exec -a "$0" "$HERE/vivaldi"-bin  "$@"
fi
