#!/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=8.1.4087.53
VIVALDI_VERSION_SHORT="${VIVALDI_VERSION%\.*\.*}"
FFMPEG_VERSIONS="8.1-150-S-20260618 8.1-149-Z-20260709"
FFMPEG_FOUND=NO
unset VIVALDI_FFMPEG_FUTURE_PATH

## Support third party maintainers providing a suitable file
if [ -e "$HERE/libffmpeg.so.$VIVALDI_VERSION_SHORT" ]; then
  export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HERE/libffmpeg.so.$VIVALDI_VERSION_SHORT"
  export VIVALDI_PRELOADS="$HERE/libffmpeg.so.$VIVALDI_VERSION_SHORT"
  FFMPEG_FOUND=YES
else
  for FFMPEG_VERSION_CANDIDATE in $FFMPEG_VERSIONS; do
    if [ "$FFMPEG_FOUND" = NO ] && [ -e "/var/opt/vivaldi/media-codecs-$FFMPEG_VERSION_CANDIDATE/libffmpeg.so" ]; then
      export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}/var/opt/vivaldi/media-codecs-$FFMPEG_VERSION_CANDIDATE/libffmpeg.so"
      export VIVALDI_PRELOADS="/var/opt/vivaldi/media-codecs-$FFMPEG_VERSION_CANDIDATE/libffmpeg.so"
      FFMPEG_FOUND=YES
      break
    # Support third party maintainers providing a suitable file
    elif [ "$FFMPEG_FOUND" = NO ] && [ -e "$HERE/libffmpeg.so.$FFMPEG_VERSION_CANDIDATE" ]; then
      export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HERE/libffmpeg.so.$FFMPEG_VERSION_CANDIDATE"
      export VIVALDI_PRELOADS="$HERE/libffmpeg.so.$FFMPEG_VERSION_CANDIDATE"
      FFMPEG_FOUND=YES
      break
    elif [ "$FFMPEG_FOUND" = NO ] && [ -e "$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_VERSION_CANDIDATE/libffmpeg.so" ]; then
      export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_VERSION_CANDIDATE/libffmpeg.so"
      export VIVALDI_PRELOADS="$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_VERSION_CANDIDATE/libffmpeg.so"
      FFMPEG_FOUND=YES
      break
    fi
  done
fi
if [ "$FFMPEG_FOUND" = NO ] && [ -n "$FFMPEG_VERSIONS" ]; then
  # 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_SHORT" ]; 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-$VIVALDI_VERSION_SHORT/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-$VIVALDI_VERSION_SHORT/libffmpeg.so" ]; then
        export LD_PRELOAD="$LD_PRELOAD${LD_PRELOAD:+:}$HOME/.local/lib/vivaldi/media-codecs-$VIVALDI_VERSION_SHORT/libffmpeg.so"
        export VIVALDI_PRELOADS="$HOME/.local/lib/vivaldi/media-codecs-$VIVALDI_VERSION_SHORT/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-$VIVALDI_VERSION_SHORT/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_SHORT"
    fi
  fi
fi

# Detect distro and distro version and export as $DISTRO_NAME $DISTRO_VERSION_NUMBER
## Check that /etc/os-release or fallback /usr/lib/os-release are present and readable
ID=''
VERSION_ID=''
OS_RELEASE_FILE=''
if [ -r /etc/os-release ]; then
  OS_RELEASE_FILE="/etc/os-release"
elif [ -r /usr/lib/os-release ]; then
  OS_RELEASE_FILE="/usr/lib/os-release"
fi

## Parse os-release line by line because sourcing it directly is problematic on some distros
if [ -n "$OS_RELEASE_FILE" ]; then
  while IFS='=' read -r osreleasekey osreleasevalue; do
    case "$osreleasekey" in
      ''|'#'*)
        continue
        ;;
      ID)
        # Strip any quoting of variables
        ID="${osreleasevalue%\"}"
        ID="${ID#\"}"
        ID="${ID%\'}"
        ID="${ID#\'}"
        ;;
      VERSION_ID)
        VERSION_ID="${osreleasevalue%\"}"
        VERSION_ID="${VERSION_ID#\"}"
        VERSION_ID="${VERSION_ID%\'}"
        VERSION_ID="${VERSION_ID#\'}"
        ;;
    esac
  done < "$OS_RELEASE_FILE"
fi

## In cases where $ID and $VERSION_ID are not set provide sensible defaults
[ -z "${ID:-}" ] && ID=linux
[ -z "${VERSION_ID:-}" ] && VERSION_ID="$(uname -r | tr -cd 'A-Za-z0-9._-' | tr 'A-Z' 'a-z')"

## Export these values with less generic names
export VIVALDI_DISTRO_NAME="$ID"
export VIVALDI_DISTRO_VERSION_NUMBER="$VERSION_ID"

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" | sed -e '/^\s*#/d')"
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 "$@"
