#!/bin/bash

sudo rm -f /usr/share/keyrings/brave-browser-archive-keyring.gpg
sudo rm -f /etc/apt/sources.list.d/brave-browser-release.list
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/brave-browser-archive-keyring.gpg] https://brave-browser-apt-release.s3.brave.com/ stable main" | sudo tee /etc/apt/sources.list.d/brave-browser-release.list
sudo curl -fsSLo /usr/share/keyrings/brave-browser-archive-keyring.gpg https://brave-browser-apt-release.s3.brave.com/brave-browser-archive-keyring.gpg

(install_packages brave-browser)
if [ $? != 0 ]; then
  if ! anything_installed_from_repo "https://brave-browser-apt-release.s3.brave.com/" ; then
    # nothing installed from repo, this check is to prevent removing repos which other pi-apps scripts or the user have used successfully
    # safe to remove
    sudo rm -f /etc/apt/sources.list.d/brave-browser-release.list /usr/share/keyrings/brave-browser-archive-keyring.gpg
    error "Failed to install brave-browser. brave-browser repository has been removed."
  else
    error "Failed to install brave-browser."
  fi
fi
