#!/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
}

if [ ! -z "$(ps aux | grep chromium | grep -v 'grep' )" ];then
  echo -n "Please close all Chromium windows first."
  while [ ! -z "$(ps aux | grep chromium | grep -v 'grep' )" ];do
    echo -n '.'
    sleep 1
  done
fi

rm -rf ~/chromium78 &>/dev/null
git clone https://github.com/Botspot/chromium78 || error "Failed to clone repository!"

#extract chromium-browser binary
cd ~/chromium78/usr/lib/chromium-browser
rm -f ~/chromium78/usr/lib/chromium-browser/chromium-browser &>/dev/null
unzip ~/chromium78/usr/lib/chromium-browser/chromium-browser.zip || error "Failed to extract chromium-browser.zip!"
rm ~/chromium78/usr/lib/chromium-browser/chromium-browser.zip &>/dev/null

cd ~

echo "Copying files now..."
sudo cp -af ~/chromium78/. / || error "Failed to copy files in!"

echo "Fixing Chromium profile..."
rm ~/'.config/chromium/Default/Web Data' &>/dev/null

echo 'Removing $HOME/chromium78'
rm -rf ~/chromium78

exit 0
