#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

# Add repository source to apt sources.list
echo "Adding ubuntu repository..."
echo "deb http://ports.ubuntu.com/ubuntu-ports bionic-updates main" | sudo tee /etc/apt/sources.list.d/firefox-ubuntu.list || error "Failed to add repository to sources.list!"

# Add ubuntu keyring
echo "Signing ubuntu repository..."
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32 || (sudo rm -f /etc/apt/sources.list.d/firefox-ubuntu.list ; error "Failed to add key")

# Add preferences so the ubuntu repositories don't become your "default" repositories
echo "Preventing Ubuntu repository from causing apt problems..."
sudo wget -O /etc/apt/preferences.d/99bionic-updates https://raw.githubusercontent.com/chunky-milk/pi-bashscripts-files/main/Files/99bionic-updates || (sudo rm -f /etc/apt/sources.list.d/firefox-ubuntu.list ; error "wget failed to download 99bionic-updates file!")

sudo apt update || error "Failed to run 'sudo apt update'!"
sudo apt install -t bionic-updates firefox -y || error "Failed to install firefox!"
