#!/bin/bash

version=132.0-1

#install all dependencies found in the librewolf amd64 deb
depends=()
if package_available libasound2t64; then
  depends+=('libasound2t64')
else
  depends+=('libasound2')
fi
install_packages "${depends[@]}" lsb-release libatk1.0-0 libc6 libcairo-gobject2 libcairo2 libdbus-1-3 libdbus-glib-1-2 libfontconfig1 libfreetype6 libgcc-s1 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libharfbuzz0b libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb-shm0 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxtst6 || exit 1

sudo rm -rf /tmp/librewolf /opt/librewolf
wget -O /tmp/librewolf.tar.bz2 "https://gitlab.com/api/v4/projects/44042130/packages/generic/librewolf/${version}/librewolf-${version}-linux-arm64-package.tar.bz2" || exit 1
mkdir -p /tmp/librewolf
status "Extracting: /tmp/librewolf.tar.bz2"
tar --strip-components=1 -xf /tmp/librewolf.tar.bz2 -C /tmp/librewolf || error "Failed to extract librewolf tarfile!"

sudo mv -f /tmp/librewolf /opt || error "Failed to move extracted librewolf folder to /opt!"

#copy icons
sudo cp -n /opt/librewolf/browser/chrome/icons/default/default16.png /usr/share/icons/hicolor/16x16/apps/librewolf.png
sudo cp -n /opt/librewolf/browser/chrome/icons/default/default32.png /usr/share/icons/hicolor/32x32/apps/librewolf.png
sudo cp -n /opt/librewolf/browser/chrome/icons/default/default48.png /usr/share/icons/hicolor/48x48/apps/librewolf.png
sudo cp -n /opt/librewolf/browser/chrome/icons/default/default64.png /usr/share/icons/hicolor/64x64/apps/librewolf.png
sudo cp -n /opt/librewolf/browser/chrome/icons/default/default128.png /usr/share/icons/hicolor/128x128/apps/librewolf.png

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

cat <<"EOF" | sudo tee /usr/share/applications/librewolf.desktop >/dev/null
[Desktop Entry]
Type=Application
Name=LibreWolf
GenericName=Web Browser
# Gnome and KDE 3 uses Comment.
Comment=Web Browser
StartupNotify=true
Terminal=false
Type=Application
MimeType=application/json;application/pdf;application/rdf+xml;application/rss+xml;application/xhtml+xml;application/xhtml_xml;application/xml;image/gif;image/jpeg;image/png;image/webp;text/html;text/xml;x-scheme-handler/http;x-scheme-handler/https;
Comment=LibreWolf Browser
Categories=Network;WebBrowser;Security;
StartupWMClass=librewolf-default
Exec=/opt/librewolf/librewolf %u
Icon=librewolf

Actions=new-window;new-private-window;safe-mode;preferences;

[Desktop Action new-window]
Name=New Window
Exec=/opt/librewolf/librewolf
[Desktop Action new-private-window]
Name=New Private Window
Exec=/opt/librewolf/librewolf --private-window %u
[Desktop Action safe-mode]
Name=Start in Safe Mode
Exec=/opt/librewolf/librewolf --safe-mode
[Desktop Action preferences]
Name=Show Preferences
Exec=/opt/librewolf/librewolf --preferences
EOF
