#!/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.2.3105.47
case x86_64 in
  amd64|x86_64)
    FFMPEG_SUM_DEB=0b47baed618d40969cf7459eb7e77693c793a9b9368b57f23e6def56be805e22-disabled # Disabled 2023-05-16
    FFMPEG_SUM_SNAP=e1cc9bedad03e1c47cb91fd92fd1a59ce8ff9265133fe6d6e344c7c92a7ade9c
    ;;
  armhf|armv7hl)
    FFMPEG_SUM_DEB=477fdd37c6972659321988605cb01c3a86cfc37cb83542aa09295519bcb15e6b-disabled # Disabled 2023-05-16
    VIVALDI_FFMPEG_AUTO=0 # Temporaily disable this because there are no suitable libs (2023-05-16)
    ;;
  arm64|aarch64)
    FFMPEG_SUM_DEB=dadb1e0d39b4bc61a8992720eaa2016a0b610c3d874a9d6ad8398bcb4c8cca9b-disabled # Disabled 2023-05-16
    VIVALDI_FFMPEG_AUTO=0 # Temporaily disable this because there are no suitable libs (2023-05-16)
    ;;
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

# Set VIVALDI_FFMPEG_FOUND=YES to stop warning about lack of proprietary media (2023-05-26) on arm
if [ 'x86_64' = 'armhf' ] || [ 'x86_64' = 'armv7hl' ] || [ 'x86_64' = 'arm64' ] || [ 'x86_64' = 'aarch64' ]; then
  export VIVALDI_FFMPEG_FOUND=YES
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

# Allow users to override command-line options
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
VIVALDI_USER_FLAGS=""
if [[ -f "$XDG_CONFIG_HOME/vivaldi-$CHROME_VERSION_EXTRA.conf" ]]; then
   VIVALDI_USER_FLAGS="$(cat "$XDG_CONFIG_HOME/vivaldi-$CHROME_VERSION_EXTRA.conf")"
fi

# 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" $VIVALDI_USER_FLAGS "$@"
