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

# export HERE to make it available for Vivaldi for setting up the relay proxy.
export HERE="$(dirname "$CHROME_WRAPPER")"

# Proprietary media check
VIVALDI_VERSION=6.9.3447.37
FFMPEG_VERSION=115541
unset VIVALDI_FFMPEG_FUTURE_PATH
if [ -e "/var/opt/vivaldi/media-codecs-$FFMPEG_VERSION/libffmpeg.so" ]; then
  export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}/var/opt/vivaldi/media-codecs-$FFMPEG_VERSION/libffmpeg.so"
  # Allow ways 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%\.*\.*}"
elif [ -e "$HERE/libffmpeg.so.$FFMPEG_VERSION" ]; then
  export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HERE/libffmpeg.so.$FFMPEG_VERSION"
elif [ -e "$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_VERSION/libffmpeg.so" ]; then
  export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_VERSION/libffmpeg.so"
  # Stop warning about lack of proprietary media (2023-05-26) on arm
elif [ 'amd64' = 'armhf' ] || [ 'amd64' = 'armv7hl' ] || [ 'amd64' = 'arm64' ] || [ 'amd64' = 'aarch64' ]; then
  :
else
  # Fix up Proprietary media if not present
  if [ "${VIVALDI_FFMPEG_AUTO:-1}" = "1" ]; then
    if [ -e "${XDG_CONFIG_HOME:-$HOME/.config}/vivaldi/update-ffmpeg-checked-${VIVALDI_VERSION%\.*\.*}" ]; then
      echo "'Proprietary media' support is not installed. Attempting to fix this for the next restart." >&2
      export VIVALDI_FFMPEG_FUTURE_PATH="$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_VERSION/libffmpeg.so"
      nohup sh -c "sleep 10; \"$HERE/update-ffmpeg\" --user" > /dev/null 2>&1 &
    else
      rm -f "${XDG_CONFIG_HOME:-$HOME/.config}/vivaldi/update-ffmpeg-checked-"*
      echo "'Proprietary media' support is not installed. Attempting to fix this now." >&2
      timeout 3s "$HERE/update-ffmpeg" --user
      if [ -e "$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_VERSION/libffmpeg.so" ]; then
        export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_VERSION/libffmpeg.so"
      else
        # Try again in the background so that it is fixed for next restart
        export VIVALDI_FFMPEG_FUTURE_PATH="$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_VERSION/libffmpeg.so"
        nohup sh -c "sleep 7; \"$HERE/update-ffmpeg\" --user" > /dev/null 2>&1 &
      fi
      mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/vivaldi"
      touch "${XDG_CONFIG_HOME:-$HOME/.config}/vivaldi/update-ffmpeg-checked-${VIVALDI_VERSION%\.*\.*}"
    fi
  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" "$@"
