#!/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=5.3.2679.70
case amd64 in
  amd64|x86_64)
    FFMPEG_SUM_DEB=407c13b7c319c6c02b8776160002d5ac30b53fa3aaedcf288e55bd75804fcc11
    FFMPEG_SUM_SNAP=af22e598ffc8d4e0dd4526d6a28cf918a7f56ef65be416f698bcabb2952f8879
    ;;
  armhf|armv7hl)
    FFMPEG_SUM_DEB=750bdf337bc6be62350ae8b5aba26378aa59c1274222d319153debaeffb09810
    ;;
  arm64|aarch64)
    FFMPEG_SUM_DEB=ee1c752f9e19248256791d8aa2bf5bb86c87ba66acc0d121ce874bae0f1ecdeb
    ;;
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)

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