#!/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=6.4.3160.44
case amd64 in
  amd64|x86_64)
    FFMPEG_SUM_DEB=13410bc08209e177b3dc5c598ab5b36a6d85a44b892667ce41c7265c2cf78c16
    FFMPEG_SUM_SNAP=1d4bf08f5f0591f7238d0824e005f1939a9ab5c9360b0ee0a227ad3b4926b21a
    ;;
  armhf|armv7hl)
    FFMPEG_SUM_DEB=9220857c54b50b74802a9a75db0dcb9a3f7dde53016d9383027938695a2ececb
    ;;
  arm64|aarch64)
    FFMPEG_SUM_DEB=9a6911272c1de68e983854c7358d644f535b9e7912f4446b5c7f6bba9693e67b
    ;;
esac
if [ -e "/var/opt/vivaldi/media-codecs-$FFMPEG_SUM_DEB/libffmpeg.so" ]; then
  export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}/var/opt/vivaldi/media-codecs-$FFMPEG_SUM_DEB/libffmpeg.so"
  export VIVALDI_FFMPEG_FOUND=YES
elif [ 'amd64' = 'amd64' ] || [ 'amd64' = 'x86_64' ] && [ -e "/var/opt/vivaldi/media-codecs-$FFMPEG_SUM_SNAP/libffmpeg.so" ]; then
  export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}/var/opt/vivaldi/media-codecs-$FFMPEG_SUM_SNAP/libffmpeg.so"
  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
  export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HERE/libffmpeg.so.${VIVALDI_VERSION%\.*\.*}"
  export VIVALDI_FFMPEG_FOUND=YES
elif [ -e "$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_SUM_DEB/libffmpeg.so" ]; then
  export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_SUM_DEB/libffmpeg.so"
  export VIVALDI_FFMPEG_FOUND=YES
elif [ 'amd64' = 'amd64' ] || [ 'amd64' = 'x86_64' ] && [ -e "$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_SUM_SNAP/libffmpeg.so" ]; then
  export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_SUM_SNAP/libffmpeg.so"
  export VIVALDI_FFMPEG_FOUND=YES
else
  export VIVALDI_FFMPEG_FOUND=NO
  # Fix up Proprietary media if not present
  if [ "${VIVALDI_FFMPEG_AUTO:-1}" = "1" ]; then
    echo "'Proprietary media' support is not installed. Attempting to fix this for the next restart." >&2
    nohup "$HERE/update-ffmpeg" --user > /dev/null 2>&1 &
  fi
fi

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)

# Absolute edit -- accomodates usage by root user
if [[ $EUID -ne 0 ]]; then
	exec -a "$0" "$HERE/vivaldi-bin" "$@"
else
	exec -a "$0" "$HERE/vivaldi-bin" --no-sandbox "$@"
fi


