#!/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.6.2867.58
case arm64 in
  amd64|x86_64)
    FFMPEG_SUM_DEB=61e498d3770eb4d4fd1f37f3eafce82433d8de0f51229c53bdfb3e7eb0e6a3ca
    FFMPEG_SUM_SNAP=964b03f3ee2a5d1ab4fe27b30ff91653d49b26e1a35ecbd9026683e4e30abcfe # Older Chromium (105) but seems to work
    ;;
  armhf|armv7hl)
    FFMPEG_SUM_DEB=81e780042d8df20939e6784749a79e33c249bf6f187abb81ed1756817cf7b391
    ;;
  arm64|aarch64)
    FFMPEG_SUM_DEB=21d7f98cbafb696fd7f4cba313f087e15da075e807c6b04ac821eda335594ab3
    ;;
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 [ 'arm64' = 'amd64' ] || [ 'arm64' = '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 [ 'arm64' = 'amd64' ] || [ 'arm64' = '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" "$@"
