#!/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")"

# Proprietary media check
VIVALDI_VERSION='2.11.1811.33'
CODECS_VERSION='79.0.3945.79'
if [ -e "/var/opt/vivaldi/media-codecs-$CODECS_VERSION/libffmpeg.so" ]; then
  if [ -n "$LD_PRELOAD" ]; then
    export LD_PRELOAD="$LD_PRELOAD:/var/opt/vivaldi/media-codecs-$CODECS_VERSION/libffmpeg.so"
  else
    export LD_PRELOAD="/var/opt/vivaldi/media-codecs-$CODECS_VERSION/libffmpeg.so"
  fi
  export VIVALDI_FFMPEG_FOUND=YES
  # Allow a way for third party maintainers to provide a suitable file
elif [ -e "$HERE/libffmpeg.so.${VIVALDI_VERSION%\.*\.*}" ]; then
  if [ -n "$LD_PRELOAD" ]; then
    export LD_PRELOAD="$LD_PRELOAD:$HERE/libffmpeg.so.${VIVALDI_VERSION%\.*\.*}"
  else
    export LD_PRELOAD="$HERE/libffmpeg.so.${VIVALDI_VERSION%\.*\.*}"
  fi
  export VIVALDI_FFMPEG_FOUND=YES
elif [ -e "$HOME/.local/lib/vivaldi/media-codecs-$CODECS_VERSION/libffmpeg.so" ]; then
  if [ -n "$LD_PRELOAD" ]; then
    export LD_PRELOAD="$LD_PRELOAD:$HOME/.local/lib/vivaldi/media-codecs-$CODECS_VERSION/libffmpeg.so"
  else
    export LD_PRELOAD="$HOME/.local/lib/vivaldi/media-codecs-$CODECS_VERSION/libffmpeg.so"
  fi
  export VIVALDI_FFMPEG_FOUND=YES
else
  echo "'Proprietary media' support is not installed. Fix this by running:" >&2
  echo "    $HERE/update-ffmpeg" >&2
  export VIVALDI_FFMPEG_FOUND=NO
fi

# Widevine CDM check
if [ ! -e "$HERE/WidevineCdm" ]; then
  echo "The Widevine CDM is not installed. Fix this by running:" >&2
  echo "    $HERE/update-widevine" >&2
fi

# Delete old cached libffmpeg.so files. Remove this line after Vivaldi 2.9
rm -f "$HOME/.local/lib/vivaldi"/libffmpeg.so*

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)

# Note: exec -a below is a bashism.
exec -a "$0" "$HERE/vivaldi-bin" "$@"
