# Fetch the lastest version of tor-browser-ports using https://sourceforge.net/projects/<projectname>/best_release.json and jq
PROJECT_NAME="tor-browser-ports"
#SF_MIRROR="https://versaweb.dl.sourceforge.net"
SF_MIRROR="https://sourceforge.net"
# RELEASE_JSON_URL="${SF_MIRROR}/projects/${PROJECT_NAME}/best_release.json"
# VERSION="13.0.3"
ARCH="arm64"
DL_DIR="/var/tmp"
WGET_ARGS="--directory-prefix=${DL_DIR} --content-disposition"

# Get the URL for the armhf version
JSON="$(curl -s "https://sourceforge.net/projects/${PROJECT_NAME}/best_release.json")"
LINUX_FILENAME="$(echo "${JSON}" | jq -r '.platform_releases.linux.filename')"  # /13.0.3/tor-browser-linux-armhf-13.0.3.tar.xz
echo "LINUX_FILENAME: ${LINUX_FILENAME}"

VERSION="$(echo "${LINUX_FILENAME}" | awk -F'/' '{print $2}')"
echo "VERSION: ${VERSION}"

FILENAME="$(echo "${LINUX_FILENAME}" | awk -F'/' '{print $3}')"
echo "FILENAME_1: ${FILENAME}"

FILENAME="${FILENAME//armhf/${ARCH}}"
echo "FILENAME_2: ${FILENAME}"


# URL=$(echo "${JSON}" | jq -r '.platform_releases.linux.url')"  # http://downloads.sourceforge.net/project/tor-browser-ports/13.0.3/tor-browser-linux-armhf-13.0.3.tar.xz?ts=gAAAAABlWy5eHDl8CF3KA6Ja87f36RamtBO2hapKpnviOEUOXnkmbbBIthBZFx_TErKR9W1pMpMyOEteXy_XpEYcruyZBv6YgA%3D%3D

TAR_URL="${SF_MIRROR}/projects/${PROJECT_NAME}/files/${VERSION}/${FILENAME}/download"
ASC_URL="${SF_MIRROR}/projects/${PROJECT_NAME}/files/${VERSION}/${FILENAME}.asc/download"

# Replace "armhf" with "arm64" in the URL
# TAR_URL="https://versaweb.dl.sourceforge.net/project/tor-browser-ports/${VERSION}/tor-browser-linux-${ARCH}-${VERSION}.tar.xz"
# ASC_URL="https://versaweb.dl.sourceforge.net/project/tor-browser-ports/${VERSION}/tor-browser-linux-${ARCH}-${VERSION}.tar.xz.asc"

# TAR_URL="${URL//armhf/${ARCH}}"
# ASC_URL="${TAR_URL//tar.xz/tar.xz.asc}"

wget -c "${WGET_ARGS}" "${TAR_URL}" -O "${FILENAME}"
wget -c "${WGET_ARGS}" "${ASC_URL}" -O "${FILENAME}".asc

# check downloaded tar using asc file, if valid unpack the tar

DEST="/home/mobian"

# An if statement that uses gpg verify to check the downloaded tar.gz and if valid unpack the tar.gz to DEST
gpg --auto-key-locate nodefault,wkd --locate-keys torbrowser@torproject.org


conf_print_public_key() {
	cat <<-'EOF'
		-----BEGIN PGP PUBLIC KEY BLOCK-----

		mDMEYLy7zxYJKwYBBAHaRw8BAQdA5PXKfnbxV/lViTGAgW34jh2jVahatLnFgKpU
		i1aZVqW0HkhlaWtraSBMaW5kaG9sbSA8aG9saW5AaWtpLmZpPoiaBBMWCgBCFiEE
		JPFBo7mItsNQuTdYavFdHkX9zskFAmC8u88CGwEFCQWjmoAFCwkIBwIDIgIBBhUK
		CQgLAgQWAgMBAh4HAheAAAoJEGrxXR5F/c7Jp7gBAL8vm5jf36L+SL2+MsiX6pYi
		uVvEMs0Fqns5UmH+eO9CAP9sTv4e1XUSG3kcNPxDHNhb390EALIKJOafuzIUFOkt
		AbgzBGC8vBMWCSsGAQQB2kcPAQEHQHkjBFnRo4dXVzwJ0PTECXGk2no5CMTXwqWc
		XNmrDlfFiPUEGBYKACYWIQQk8UGjuYi2w1C5N1hq8V0eRf3OyQUCYLy8EwIbAgUJ
		AeEzgACBCRBq8V0eRf3OyXYgBBkWCgAdFiEEnQ+3JDWrYv30FZVUWJDtuAD3xT0F
		AmC8vBMACgkQWJDtuAD3xT1KHQEAp6gkZRQYzLLCEnFDronvnGgPRMgfnzs3eCFW
		x843EJoA/RWbZQeuv2tgh7pUYJk5Kzoi6PCklnw2DWuhJST9QPYMxbUBAJeTn5+q
		fMa1fGLF6rcr1yvf2cO0u5ow45Tft0+jxjMNAQDlp0YaC4vF0dJF8QyWMAwgNbms
		OfPns7bfiACf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TYZgVFNfqs/9b66X7QBT5k2wCidCA==
		=KkEP
		-----END PGP PUBLIC KEY BLOCK-----
	EOF
}

conf_print_public_key | gpg --import
gpg --lsign-key 24F141A3B988B6C350B937586AF15D1E45FDCEC9
gpg --no-default-keyring -a \
	--export 24F141A3B988B6C350B937586AF15D1E45FDCEC9 | gpg --no-default-keyring \
	--keyring ~/.gnupg/trustedkeys.gpg \
	--import -

gpg --update-trustdb

if [ -f "${DL_DIR}"/tor-browser-linux-${ARCH}-"${VERSION}".tar.xz.asc ]; then
	if gpg --verify ${DL_DIR}/tor-browser-linux-"${ARCH}"-"${VERSION}".tar.xz.asc "${DL_DIR}"/tor-browser-linux-"${ARCH}"-"${VERSION}".tar.xz; then
		echo "Valid GPG signature"
		tar -C "${DEST}" -xf "${DL_DIR}"/tor-browser-linux-"${ARCH}"-"${VERSION}".tar.xz
	else
		echo "Invalid GPG signature; not unpacking."
	fi
fi


# Create the desktop file.
# DESKTOP_FILE="https://raw.githubusercontent.com/RightToPrivacy/Tor-Browser-Pinephone/main/start-tor-browser.desktop"
APP_DEST="/usr/share/applications/"
# APP_DEST="~/.local/share/applications/"
# sudo wget -c -O ${APP_DEST}/mobian-torbrowser.desktop $DESKTOP_FILE

cat <<-EOF | sudo tee ${APP_DEST}/mobian-torbrowser.desktop
	#!/usr/bin/env /home/mobian/tor-browser/Browser/execdesktop
	#
	# *Slightly modified to work on Mobian*
	#
	# This file is a self-modifying .desktop file that can be run from the shell.
	# It preserves arguments and environment for the start-tor-browser script.
	#
	# Run './start-tor-browser.desktop --help' to display the full set of options.
	#
	# When invoked from the shell, this file must always be in a Tor Browser root
	# directory. When run from the file manager or desktop GUI, it is relocatable.
	#
	# After first invocation, it will update itself with the absolute path to the
	# current TBB location, to support relocation of this .desktop file for GUI
	# invocation. You can also add Tor Browser to your desktop's application menu
	# by running './start-tor-browser.desktop --register-app'
	#
	# If you use --register-app, and then relocate your TBB directory, Tor Browser
	# will no longer launch from your desktop's app launcher/dock. However, if you
	# re-run --register-app from inside that new directory, the script
	# will correct the absolute paths and re-register itself.
	#
	# This file will also still function if the path changes when TBB is used as a
	# portable app, so long as it is run directly from that new directory, either
	# via the shell or via the file manager.

	[Desktop Entry]
	Type=Application
	Name=Tor Browser Setup
	GenericName=Web Browser
	Comment=Tor Browser is +1 for privacy and −1 for mass surveillance
	Categories=Network;WebBrowser;Security;
	Exec=/bin/bash ${DEST}/tor-browser_en-US/Browser/start-tor-browser --detach
	X-TorBrowser-ExecShell=/home/mobian/tor-browser/Browser/start-tor-browser --detach
	Icon=web-browser
	StartupWMClass=Tor Browser
EOF

sudo chmod +x /usr/share/applications/mobian-torbrowser.desktop
