#!/bin/bash

#this script will need some updates once this browser is officially released on ARM64.
#monitor progress on: https://github.com/zen-browser/desktop/pull/3135

version=1.21.12b

sudo rm -rf /opt/zen /opt/zen-browser /tmp/zen-browser-arm64-copr

cd /tmp
wget https://github.com/zen-browser/desktop/releases/download/${version}/zen.linux-aarch64.tar.xz || exit 1

status -n "Extracting... "
sudo tar xf zen.linux-aarch64.tar.xz -C /opt || error "Failed to extract"
status_green Done

rm -f zen.linux-aarch64.tar.xz

sudo mv /opt/zen /opt/zen-browser

git clone --depth 1 https://github.com/ArchitektApx/zen-browser-arm64-copr || exit 1

#fix app_id to get taskbar icon working on wayland
sed -i 's+exec /opt/zen-browser/zen-bin+exec /opt/zen-browser/zen-bin --class zen-browser --name zen-browser+g' zen-browser-arm64-copr/zen-browser || error "failed to edit launcher file"

sudo mv -f zen-browser-arm64-copr/zen-browser /usr/bin/zen-browser || error "Failed to move zen-browser command"
sudo chmod +x /usr/bin/zen-browser

#copy menu launcher
sudo mv -f zen-browser-arm64-copr/zen-browser.desktop /usr/share/applications || error "Failed to move menu launcher file"

#disables update notifications
sudo mkdir -p /opt/zen-browser/distribution
sudo mv -f zen-browser-arm64-copr/policies.json /opt/zen-browser/distribution || error "Failed to move policies.json file"
#no need for the git repo anymore
rm -rf zen-browser-arm64-copr

#copy icons
for i in 16x16 32x32 48x48 64x64 128x128; do
  sudo mkdir -p /usr/share/icons/hicolor/$i/apps/
  sudo cp /opt/zen-browser/browser/chrome/icons/default/default${i/x*}.png /usr/share/icons/hicolor/$i/apps/zen-browser.png || error "Failed to copy $i icon"
done

sudo update-icon-caches /usr/share/icons/*
sudo xdg-icon-resource forceupdate --mode system
true
