#!/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='4.3.2439.65'
case 'x86_64' in
  amd64|x86_64)
    FFMPEG_SUM_DEB='a812e2cfcc048a3cf96ab5b310bf522767dc454aea567af4b1d9b6c2737d1840'
    FFMPEG_SUM_SNAP='6fb7f1da8c410352e8bb1b45395908b53b8e8dcbc6e9126d8435529eb3963b40'
    ;;
  armhf)
    FFMPEG_SUM_DEB='7db095933802d0ee97fca022f177f3e0ba06cf84e5166cd76c84ddfc9a066c23'
    ;;
  arm64)
    FFMPEG_SUM_DEB='03a677225359cad8f798644550554522233fb3c6ebd306b6752ad067add23231'
    ;;
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 [ 'x86_64' = 'amd64' ] || [ 'x86_64' = '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 [ 'x86_64' = 'amd64' ] || [ 'x86_64' = '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" "$@"
