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

. /etc/default/vivaldi

# We include some xdg utilities next to the binary, and we want to prefer them
# over the system versions when we know the system versions are very old. We
# detect whether the system xdg utilities are sufficiently new to be likely to
# work for us by looking for xdg-settings. If we find it, we leave $PATH alone,
# so that the system xdg utilities (including any distro patches) will be used.
if ! which xdg-settings &> /dev/null; then
  # Old xdg utilities. Prepend $HERE to $PATH to use ours instead.
  export PATH="$HERE:$PATH"
else
  # Use system xdg utilities. But first create mimeapps.list if it doesn't
  # exist; some systems have bugs in xdg-mime that make it fail without it.
  xdg_app_dir="${XDG_DATA_HOME:-$HOME/.local/share/applications}"
  mkdir -p "$xdg_app_dir"
  [ -f "$xdg_app_dir/mimeapps.list" ] || touch "$xdg_app_dir/mimeapps.list"
fi

# Find libffmpeg.so with additional codecs.
FFMPEG_LIB="/usr/lib64/chromium-ffmpeg-extra/libffmpeg.so"
if [[ -n "$FFMPEG_LIB" && -e "$FFMPEG_LIB" ]]; then
  FFMPEG_VERSION="$(grep -aom1 'FFmpeg version N-[0-9]\+-' "$FFMPEG_LIB" | cut -f2 -d-)"
  if [[ -n "$FFMPEG_VERSION" && "$FFMPEG_VERSION" -ge "91124" ]]; then
    FFMPEG_LD_LIBRARY_PATH=":${FFMPEG_LIB%/*}"
  fi
fi

# Set LD_LIBRARY_PATH to prefer our libs, excluding $HERE/lib/libffmpeg.so if
# an alternative ffmpeg was found.
LD_LIBRARY_PATH="$HERE$FFMPEG_LD_LIBRARY_PATH:$HERE/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH

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)

PEPPER_PATH="/usr/lib64/chromium/PepperFlash/libpepflashplayer.so"
if [[ -n "$PEPPER_PATH" && -f "$PEPPER_PATH" ]]; then
  FLASH_PATH="$PEPPER_PATH"
  FLASH_VERSION="$(grep -ao -m1 "LNX [,0-9]\+" $PEPPER_PATH | sed 's/[^0-9]\+//;s/,/./g')"
fi

# The setuid permission set on sandbox
VIVALDI_SANDBOX_FILE="$HERE/vivaldi-sandbox"
VIVALDI_FLAGS="${VIVALDI_USER_FLAGS:-$VIVALDI_FLAGS}"
if which xdg-su &> /dev/null; then
  if [[ "$VIVALDI_FLAGS" =~ 'disable-setuid-sandbox' || "$@" =~ 'disable-setuid-sandbox' ]]; then
    if [[ -u "$VIVALDI_SANDBOX_FILE" ]]; then
    ┆ echo "Removing the setuid permission on sandbox"
    ┆ xdg-su -c "chmod u-s $VIVALDI_SANDBOX_FILE" &> /dev/null || exit $?
    fi
  elif [[ ! -u "$VIVALDI_SANDBOX_FILE" ]]; then
    echo "Setting the setuid permission on sandbox"
    xdg-su -c "chmod u+s $VIVALDI_SANDBOX_FILE" &> /dev/null || exit $?
  fi
fi

VIVALDI_FLAGS="${VIVALDI_FLAGS//--disable-setuid-sandbox/}"
if [[ ! -u "$VIVALDI_SANDBOX_FILE" ]]; then
  VIVALDI_FLAGS="$VIVALDI_FLAGS --disable-setuid-sandbox"
fi

export VIVALDI_FLAGS="$VIVALDI_FLAGS"
export VIVALDI_DATA_DIR="${VIVALDI_USER_DATA_DIR:-$VIVALDI_DATA_DIR}"

if [[ -n "$VIVALDI_DATA_DIR" ]]; then
  exec -a "$0" "$HERE/vivaldi-bin"  \
    --user-data-dir="$VIVALDI_DATA_DIR" \
    --ppapi-flash-path=$FLASH_PATH --ppapi-flash-version=$FLASH_VERSION $VIVALDI_FLAGS \
    "$@"
else
  exec -a "$0" "$HERE/vivaldi"-bin  \
    --ppapi-flash-path=$FLASH_PATH --ppapi-flash-version=$FLASH_VERSION $VIVALDI_FLAGS \
    "$@"
fi
