#!/usr/bin/env bash
#shellcheck disable=SC2155,SC2034
#shellcheck source=/dev/null

#  /usr/local/bin/brave-browser
#  Description: Tweaks to improve speed of browser
#
#  Created: 2020/01/11
#  Altered: 2024/06/14
#
#  Copyright (c) 2023-2024, Vilmar Catafesta <vcatafesta@gmail.com>
#                2022-2023, Bruno Gonçalves <www.biglinux.com.br>
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
#  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
#  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
#  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Normal config in mesa and with h264ify with amber mesa
if [ ! -e "$HOME/.config/BraveSoftware" ]; then
	if [ -e "/usr/lib/libEGL_amber.so" ]; then
		cp -R /usr/share/biglinux/browser/BraveSoftwareAmber "$HOME/.config/BraveSoftware"
	else
		cp -R /usr/share/biglinux/browser/BraveSoftware "$HOME/.config/BraveSoftware"
	fi
	
	# Change default url in community version
	if grep -q 'ID=bigcommunity' /etc/os-release; then
		sed -i 's|www.biglinux.com.br|communitybig.org|g' ~/.config/BraveSoftware/Brave-Browser/Default/Preferences
		if [[ -z "$1" ]]; then
			startUrl=communitybig.org
		fi
	else
		if [[ -z "$1" ]]; then
			startUrl=www.biglinux.com.br
		fi
	fi
fi

# # Fix to intel gpu
if lspci | grep -q 'UHD Graphics 605'; then
	export LIBVA_DRIVER_NAME=i965
fi

# # Read optimization and vainfo
# if [ -f ~/.config/browser-optimize ]; then
#     OPTIMIZATION="$(<~/.config/browser-optimize)"
# fi
# VAINFO="$(vainfo 2>&1)"

if [[ "$XDG_SESSION_TYPE" = "wayland" ]]; then
   enableWayland='--ozone-platform=wayland'
   waylandTweaks=',WebRTCPipeWireCapturer'
fi

# if [ -z "$OPTIMIZATION" ]; then
#     if [ "$(systemd-detect-virt)" = "none" ] && [[ $VAINFO != *"nvidia"* ]] && [[ $VAINFO != *"nouveau"* ]] && [[ $VAINFO == *"VAProfileVP9"* ]]; then
#         OPTIMIZATION="performance"
#     else
#         OPTIMIZATION="optimize"
#     fi
# fi

# if [ "$OPTIMIZATION" = "performance" ]
# then
#     PARAMETERS="--enable-accelerated-2d-canvas --enable-accelerated-mjpeg-decode --enable-accelerated-video --enable-features=VaapiVideoDecodeLinuxGL,UseDecoderStreamForWebRTC,ChromeWideEchoCancellation,AudioRendererAlgorithmParameters,MemoryPressureBasedSourceBufferGC,GlobalMediaControlsPictureInPicture,VaapiVideoDecoder,VaapiVideoEncoder,ParallelDownloading,PdfUseSkiaRenderer,PdfXfaForms,Quic${waylandTweaks} --enable-gpu-rasterization --enable-quic --ignore-gpu-blocklist --enable-parallel-downloading --enable-zero-copy --enable-accelerated-video-decode"
#
# elif [ "$OPTIMIZATION" = "optimize" ]
# then
#     PARAMETERS="--enable-accelerated-2d-canvas --enable-accelerated-video --enable-features=VaapiVideoDecodeLinuxGL,UseDecoderStreamForWebRTC,ChromeWideEchoCancellation,AudioRendererAlgorithmParameters,MemoryPressureBasedSourceBufferGC,GlobalMediaControlsPictureInPicture,VaapiVideoDecoder,VaapiVideoEncoder,ParallelDownloading,PdfUseSkiaRenderer,PdfXfaForms,Quic${waylandTweaks} --enable-gpu-rasterization --enable-quic --enable-parallel-downloading"
# else
#     PARAMETERS=""
# fi

if glxinfo | grep -i -q -e "vmware" -e "llvmpipe"; then
	PARAMETERS="--disable-gpu"
fi

# Less disk write cache
if [ -n "$LD_PRELOAD" ]; then
	export LD_PRELOAD="/usr/lib/libeatmydata.so $LD_PRELOAD"
else
	export LD_PRELOAD="/usr/lib/libeatmydata.so"
fi

# Read filename and change /usr/local/bin to /usr/bin
exec ${0//usr\/local\/bin/usr\/bin} $enableWayland $CLASS --enable-quic --enable-features=TearOffWebAppTabOpensWebAppWindow,PdfOcr,PdfSearchify,PdfTags,AccessiblePDFForm,PdfXfaForms,FluentScrollbar,GlobalMediaControlsUpdatedUI,ParallelDownloading --disable-features=WebRtcAllowInputVolumeAdjustment,AllowQt,GlobalShortcutsPortal $PARAMETERS $startUrl ${1+"$@"}
