Created
December 16, 2022 23:36
-
-
Save SydLambert/e8d3db1e82baf985fe3140b9112ca47e to your computer and use it in GitHub Desktop.
Simple script to manually install Firefox to the /opt/ directory on GNU/Linux. Also handles updating. Primarily here for my own reference, use it if you'd like, I made it quickly so there are definitely things to improve. Requires wget. I recommend purging other Firefox installs beforehand. This script doesn't touch your profile directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Syd Lambert 2022, sydlambert.com | |
usage () { | |
cut -c 5- <<EOF | |
Usage: | |
$(basename $0) command [options] | |
A simple script for manual installation of Firefox to the /opt/ directory. | |
Commands: | |
install Install Firefox | |
status See installation status, local & remote versions | |
uninstall Uninstall Firefox | |
upgrade Upgrade Firefox to latest version (if available) | |
Options: | |
-f, --force Force & accept all prompts | |
EOF | |
} | |
# Get the latest Firefox version from mozilla.org, e.g. 108.0.1 | |
version-remote () { | |
curl -sfI 'https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=en-US' | grep -Po '(?<=firefox-)[\d.]+(?=\.)' | |
} | |
# Get the current latest installed Firefox version (if present), e.g. 108.0.1 | |
version-local () { | |
readlink /opt/firefox | grep -Po '(?<=firefox-)[\d.]+' | |
} | |
# Determine if Firefox is already installed in /opt/ | |
is-installed () { | |
if [ -z "$(version-local)" ]; then | |
false | |
else | |
true | |
fi | |
} | |
# Remove temporary files | |
cleanup () { | |
rm -rf /tmp/firefox-manager | |
} | |
# Prompt the user for a yes/no confirmation | |
yesno () { | |
read -p "$1 [Y/n] " | |
case $(echo "$REPLY" | tr '[A-Z]' '[a-z]') in | |
y|yes) true ;; | |
*) false ;; | |
esac | |
} | |
# Install Firefox to the /opt/ directory. This will download the latest version | |
# from mozilla.org, extract it, then place it in /opt/. Firefox installations | |
# are versioned, new versions will coexist with old versions. This function | |
# sets up symlinks to relevant locations. The end result will look something | |
# like this after an install and an upgrade, for example: | |
# | |
# Old version: /opt/firefox-108.0/ | |
# Latest version: /opt/firefox-108.0.1/ | |
# Symlink to current version: /opt/firefox/ -> /firefox-108.0.1/ | |
# Symlink to binary: /usr/local/bin/firefox -> /opt/firefox/firefox | |
# Desktop file: /usr/local/share/applications/firefox.desktop | |
ff-install () { | |
echo 'Downloading...' | |
mkdir -p /tmp/firefox-manager | |
wget 'https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-GB' --trust-server-names -P /tmp/firefox-manager | |
downloaded_version=$(find /tmp/firefox-manager/ -name 'firefox*' -type f | grep -Po '(?<=firefox-)[\d.]+(?=\.)') | |
[ ! -f "/tmp/firefox-manager/firefox-$downloaded_version.tar.bz2" ] && { echo "Download failed."; cleanup; exit 2; } | |
[ -d "/opt/firefox-$downloaded_version" ] && { echo "An installation of this version already exists."; cleanup; exit 2; } | |
echo 'Extracting...' | |
mkdir "/opt/firefox-$downloaded_version" | |
tar -xjvf "/tmp/firefox-manager/firefox-$downloaded_version.tar.bz2" -C "/opt/firefox-$downloaded_version" --strip-components=1 | |
[ ! -d "/opt/firefox-$downloaded_version" ] && { echo "Extraction failed."; cleanup; exit 2; } | |
echo 'Installing...' | |
ln -fsn /opt/firefox-$downloaded_version /opt/firefox | |
ln -fs /opt/firefox/firefox /usr/local/bin/firefox | |
if [ ! -f /usr/local/share/applications/firefox.desktop ]; then | |
mkdir -p /usr/local/share/applications | |
wget https://raw.githubusercontent.com/mozilla/sumo-kb/main/install-firefox-linux/firefox.desktop -P /usr/local/share/applications | |
fi | |
cleanup | |
echo 'Done.' | |
} | |
# See current installation status and latest version | |
ff-status () { | |
if [ is-installed ]; then | |
echo "Status: Installed" | |
echo "Version: $(version-local)" | |
else | |
echo "Status: Firefox is not installed" | |
echo "Version: N/A" | |
fi | |
echo "Latest version: $(version-remote)" | |
} | |
# Remove this manual install from the system (including desktop file) | |
ff-uninstall () { | |
rm /usr/local/bin/firefox | |
rm /opt/firefox | |
rm -rf /opt/firefox-$(version-local) | |
rm /usr/local/share/applications/firefox.desktop | |
} | |
[ -z "$1" ] && { usage; exit 1; } | |
subcommand=$1; shift | |
opts=$(getopt -o f -l force -n "$(basename $0)" -- "$@" ) | |
eval set --$opts | |
while true; do | |
case "$1" in | |
-f|--force) opt_force="true"; shift ;; | |
--) shift; break ;; | |
*) usage; exit 1 ;; | |
esac | |
done | |
case "$subcommand" in | |
install) | |
is-installed && { echo Firefox is already installed. ; exit 1; } | |
[ "$(whoami)" != "root" ] && { echo "Please run this command as root." && exit 1; } | |
if [ "$opt_force" != "true" ]; then | |
yesno "Are you sure you want to install Firefox?" || exit 1 | |
fi | |
ff-install | |
;; | |
uninstall) | |
[ ! is-installed ] && { echo Firefox is not installed. ; exit 1; } | |
[ "$(whoami)" != "root" ] && { echo "Please run this command as root." && exit 1; } | |
if [ "$opt_force" != "true" ]; then | |
yesno "Are you sure you want to uninstall Firefox?" || exit 1 | |
fi | |
ff-uninstall | |
;; | |
upgrade) | |
[ ! is-installed ] && { echo Firefox is not installed. ; exit 1; } | |
this_version_local="$(version-local)" | |
this_version_remote="$(version-remote)" | |
echo "Local version: $this_version_local" | |
echo "Latest available version: $this_version_remote" | |
[ "$this_version_local" = "$this_version_remote" ] && { echo "Already up-to-date."; exit 0; } | |
[ "$(whoami)" != "root" ] && { echo "Please run this command as root." && exit 1; } | |
if [ "$opt_force" != "true" ]; then | |
{ yesno "Would you like to upgrade?" || [ $opt_force ]; } || exit 1 | |
fi | |
ff-install | |
;; | |
status) | |
ff-status | |
;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment