#!/bin/bash
#
# Copyright 2011 The Chromium Authors
# 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"`"

APPNAME=yandex-browser
[ ! -f "/etc/$APPNAME/default" ] ||
  . "/etc/$APPNAME/default"

# 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 ! command -v 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

# Prefer user defined YANDEX_BROWSER_USER_FLAGS (fron env) over system
# default YANDEX_BROWSER_FLAGS (from /etc/$APPNAME/default)
if [ -n "${YANDEX_BROWSER_USER_FLAGS-}" ]; then
	YANDEX_BROWSER_ARGS=( $YANDEX_BROWSER_USER_FLAGS )
elif [ -n "${YANDEX_BROWSER_FLAGS-}" ]; then
	YANDEX_BROWSER_ARGS=( $YANDEX_BROWSER_FLAGS )
fi

# Enable native Wayland support on alt linux and astra linux if the env var
# YANDEX_DISABLE_WAYLAND isn't set and the current session is running on a
# Wayland display server.
if [ "" == "alt_linux" ] ||
   [ "" == "astra_linux" ]; then
  if [ -z "${YANDEX_DISABLE_WAYLAND-}" ] &&
     [ "$XDG_SESSION_TYPE" = "wayland" ]; then
    YANDEX_BROWSER_ARGS+=("--ozone-platform=wayland")
    if [ "" == "astra_linux" ]; then
      YANDEX_BROWSER_ARGS+=("--enable-wayland-ime")
    fi
  fi
fi


# Proxy settings
if [ -n "${no_proxy-}" ]; then
  YANDEX_BROWSER_ARGS+=("--proxy-bypass-list=$no_proxy")
fi

if [ -z "${auto_proxy-}" ]; then
	for use_proxy in "${https_proxy-}" "${http_proxy-}" "${all_proxy-}"; do
		if [ -n "$use_proxy" ]; then
			YANDEX_BROWSER_ARGS+=("--proxy-server=$use_proxy")
			break
		fi
	done
	[ -z "${ftp_proxy-}" ] ||
		echo >&2 "warning: environment variable \`ftp_proxy' not supported"
else
	YANDEX_BROWSER_ARGS+=("--proxy-auto-detect")
fi

FFMPEG_PATH=`$HERE/find_ffmpeg`
if [ $? -eq 0 ]; then
  export FOUND_FFMPEG=1
  export THE_BEST_FFMPEG_LIBRARY=$FFMPEG_PATH
else
  echo find_ffmpeg failed, using the integrated library.
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/yandex_browser" "${YANDEX_BROWSER_ARGS[@]}" "$@"
