#!/bin/bash

version=13.0.9

#if ~/tor-browser_en-US folder exists, prepare to import its user-data
if [ -d ~/tor-browser_en-US/Browser/TorBrowser/Data/Browser ];then
  mv -f ~/tor-browser_en-US ~/tor-browser.temp
fi

#if ~/tor-browser folder exists, prepare to import its user-data
if [ -d ~/tor-browser/Browser/TorBrowser/Data/Browser ];then
  mv -f ~/tor-browser ~/tor-browser.temp
fi

rm -f ~/tor.tar.xz
wget -O ~/tor.tar.xz https://sourceforge.net/projects/tor-browser-ports/files/${version}/tor-browser-linux-arm64-${version}.tar.xz/download || error "Failed to download!"
tar -xf ~/tor.tar.xz || error "Failed to extract!"
rm -f ~/tor.tar.xz

#if ~/tor-browser.temp folder exists, import its user-data
if [ -d ~/tor-browser.temp ];then
  rm -rf ~/tor-browser/Browser/TorBrowser/Data/Browser
  mv -f ~/tor-browser.temp/Browser/TorBrowser/Data/Browser ~/tor-browser/Browser/TorBrowser/Data && rm -rf ~/tor-browser.temp
fi

#copy icon for wayfire
mkdir -p ~/.local/share/icons
cp -f $HOME/tor-browser/Browser/browser/chrome/icons/default/default128.png ~/.local/share/icons/tor-browser.png

echo "[Desktop Entry]
Type=Application
Name=Tor Browser
GenericName=Web Browser
Comment=Tor Browser is +1 for privacy and −1 for mass surveillance
Categories=Network;WebBrowser;Security;
Exec=$HOME/tor-browser/Browser/start-tor-browser --class 'tor-browser' --name 'tor-browser'
X-TorBrowser-ExecShell=./Browser/start-tor-browser --detach
Icon=tor-browser
StartupWMClass=tor-browser" > ~/.local/share/applications/tor.desktop
