#!/bin/	bash

machine=$(uname -m)
if [[  ${machine} == x86_64 ]]; then
	arch=amd64
elif
	[[ ${machine} == aarch64 ]]; then
	arch=arm64
fi

# UBUNTU_CODENAME=bionic
UBUNTU_CODENAME=stable
BRANCH="-dev"
URL=brave-browser-apt${BRANCH}.s3.brave.com


#sudo curl -fsSLo /usr/share/keyrings/brave-browser-archive-keyring.gpg https://brave-browser-apt-release.s3.brave4u7jddbv7cyviptqjc7jusxh72uik7zt6adtckl5f4nwy2v72qd.onion/brave-browser-archive-keyring.gpg

KEY_DIR=/etc/apt/trusted.gpg.d  # if you dont want to have signed-by in the sources.list
# KEY_DIR=/usr/share/keyrings
KEY_DIR=/etc/apt/keyrings

sudo curl -fsSLo ${KEY_DIR}/brave-browser-archive-keyring.gpg https://brave-browser-apt-release.s3.brave.com/brave-browser-archive-keyring.gpg


echo "deb [[signed-by=${KEY_DIR}/brave-browser-archive-keyring.gpg  arch=${arch}] https://${URL}/ $UBUNTU_CODENAME main" | tee /etc/apt/sources.list-available/brave-browser${BRANCH}-${UBUNTU_CODENAME}.list

ln -s /etc/apt/sources.list-available/brave-browser${BRANCH}-${UBUNTU_CODENAME}.list /etc/apt/sources.list.d/brave-browser${BRANCH}-${UBUNTU_CODENAME}.list

apt update

apt install brave-browser${BRANCH}

echo 'kernel.unprivileged_userns_clone=1' > /etc/sysctl.d/00-local-userns.conf
#service procps restart

# Bypass apt-proxy for brave packages
if [ -f /etc/apt/apt.conf.d/02proxy ]; then
	cat /etc/apt/apt.conf.d/02proxy
	if [ ! -z $(grep ${URL}) ]; then
		echo "Acquire::http::Proxy { \"${URL}\" DIRECT; };" >> /etc/apt/apt.conf.d/02proxy
	fi
else
		echo "Acquire::http::Proxy { \"${URL}\" DIRECT; };" >> /etc/apt/apt.conf.d/02proxy
fi
