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

cd $HOME

rm -rf ~/chromium-v84-widevine &>/dev/null

git clone https://github.com/Botspot/chromium-v84-widevine || error 'Failed to clone example.com repository!'

echo "[Desktop Entry]
Version=1.0
Name=Chromium Widevine
GenericName=Web Browser
Comment=Play Netflix & Spotify on Chromium v84
Exec=chromium-browser %U --user-agent="\""Mozilla/5.0 (X11; CrOS armv7l 12371.89.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"\"" %U
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=$(dirname "$0")/icon-64.png
Categories=Network;WebBrowser;
StartupNotify=true" > ~/.local/share/applications/widevine-chromium.desktop

sudo cp -a ~/chromium-v84-widevine/WidevineCdm /opt

rm -rf ~/chromium-v84-widevine

mkdir -p ~/.config/chromium-browser/WidevineCdm
echo "{"\""Path"\"":"\""/opt/WidevineCdm"\""}" 1> ~/.config/chromium-browser/WidevineCdm/latest-component-updated-widevine-cdm

if [ ! -z "$(ps aux | grep /usr/lib/chromium-browser | grep -v grep)" ];then
  echo -e "\e[97mFor Chromium Widevine to work, you need to exit all instances of Chromium.\e[39m"
  sleep 1
  echo -en "\e[97m\nWaiting until Chromium is closed\e[39m"
  while [ ! -z "$(ps aux | grep /usr/lib/chromium-browser | grep -v grep)" ];do
    sleep 1
    echo -n '.'
  done
fi
