Skip to content

Instantly share code, notes, and snippets.

@mzpqnxow
Created December 5, 2021 18:09
Show Gist options
  • Save mzpqnxow/05dfc712d7e9e1a84b4b42e93e3379d2 to your computer and use it in GitHub Desktop.
Save mzpqnxow/05dfc712d7e9e1a84b4b42e93e3379d2 to your computer and use it in GitHub Desktop.
Install/Update Mozilla release of Firefox on Debian and other Linux distributions, can be run from cron
#!/bin/bash
#
# Install/Update Mozilla Firefox conveniently and repeatably, allowing version selection
# The ESR Firefox that ships with Debian 10/11 (and probably Ubuntu, etc) doesn't support
# containers, so I found myself installing the Mozilla distribution of Firefox. That had
# me a little concerned about keeping it up to date, so I run this out of cron. Maybe it's
# useful for someone ...
#
# This is intended for use with XFCE but I think most window managers use the .desktop style
# metadata files for launchers/menus/etc. Check /usr/share/applications/ on your system to
# confirm
#
# - Download the latest Mozilla Firefox stable release for Linux x86_64
# - Extract it to a directory of other Firefox installations
# - Symlink to the new directory
# - Create/update an application launcher file so the latest version appears in menus
# if you're into that sort of thing
#
# You can copy the mozilla-firefox.desktop file it generated to ~/Desktop if you want a Desktop
# icon/launcher, otherwise you'll see it in menus only
#
# Managing Versions
# -----------------
# I prefer the following convention and use it with other software packages, e.g. golang
# There is a single directory (in this case, ~/firefox) where each version is kept
# A symlink is always available, pointing to the latest version
#
# Visual representation:
#
# ~/firefox
# firefox-94.0.0/
# firefox-94.0.1/
# firefox-94.0.2/
# latest -> firefox-94.0.2
#
# This way you don't need to update the .desktop file or anything else referencing the
# path of the latest installed version. You can also easily upgrade/downgrade by modifying
# the symlink
#
# To downgrade to firefox-94.0.0, simply use `ln -sf firefox-94.0.0 ~/firefox/latest` and
# restart Firefox
#
# NOTE: If you manually adjust the symlink, your menu launcher will still work, but the description
# will have an incorrect date. You can either manually fix this, or remove the date from this
# script entirely. In general though you should avoid downgrading anyway ...
#
# Each time this runs, it will output trace information into $TRACE_OUTFILE, overwriting logs from
# previous runs, to avoid filling your disk up ...
#
#
# https://github.com/mzpqnxow
#
set -eu
TODAY="$(date +%Y-%m-%d)"
TARBALL_URL='https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=en-US'
FF_ROOT=~/firefox
FF_LATEST="$FF_ROOT/latest"
FF_DIST="$FF_ROOT/dist"
FF_WORKDIR="$FF_ROOT/tmp"
# Global installation, all users' menus
# APPS_DIRECTORY=/usr/share/applications
# Per-user installation, will appear in your menu
APPS_DIRECTORY=~/.local/share/applications
# Can be anything ending with .desktop
APPS_FILENAME=firefox-latest.desktop
APPS_FULLPATH="$APPS_DIRECTORY/$APPS_FILENAME"
# Must end with a semi-colon and be semi-colon delimited, I think
MIME_TYPES="text/html;text/xml;application/xhtml+xml;application/xml;application/vnd.mozilla.xul+xml;"
MIME_TYPES+="application/rss+xml;application/rdf+xml;image/gif;image/jpeg;image/png;"
MIME_TYPES+="x-scheme-handler/http;x-scheme-handler/https;"
TRACE_OUTFILE="$FF_ROOT/ff_update.tracelog"
# Nice tracing function, applies trace logging globally
# Disable by commenting out the call to start_trace(), or manually
# use set +x wherever you want to turn it off
configure_trace() {
echo "Script tracing to file is ENABLED"
if [ -n "$TRACE_OUTFILE" ]; then
if [[ $(( ${BASH_VERSINFO[0]}*10 + ${BASH_VERSINFO[0]} )) -ge 41 ]]; then
# Automatically get a file descriptor number if Bash >= 4.1
exec {fd}>"$TRACE_OUTFILE"
BASH_XTRACEFD=${fd}
else
# Pick an arbitrary but high file descriptor number if an older version of Bash
# In theory, only 0/1/2 should be open, but in practice that's rarely the case
# because of FDs that may be inherited depending on how your script was started
# and based on what else may be using descriptors in the script
exec 500>"$TRACE_OUTFILE"
BASH_XTRACEFD=500
fi
fi
}
start_trace() {
set -x
}
main() {
local -r ff_exe="$FF_LATEST/firefox"
local -r ff_icon="$FF_LATEST/browser/chrome/icons/default/default64.png"
mkdir -p "$FF_DIST" "$FF_WORKDIR"
cd $FF_DIST
wget --trust-server-names -c "$TARBALL_URL"
# wget --no-use-server-timestamps --trust-server-names -c "https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=en-US"
local -r ff_distfile="$(ls -rt -w1 | tail -1)"
local -r ff_distname="$(basename "$ff_distfile" .tar.bz2)"
local -r ff_version="$(echo "$ff_distname" | grep -Po '(?<=firefox-)(.*)')"
tar -C "$FF_WORKDIR" -xvjf "$ff_distfile"
echo "Switching to Firefox, version=$ff_version"
FF_REALDIR="firefox-$ff_version"
FF_FULL_REALDIR="$FF_ROOT/$FF_REALDIR"
if [ -d "$FF_FULL_REALDIR" ]; then
# Will not overwride, but will still continue on to generate .desktop file
echo "Version already exists, will not overwrite ..."
else
mv "$FF_WORKDIR/firefox" "$FF_ROOT/$FF_REALDIR"
fi
cd "$FF_ROOT"
echo "Updating symlink to point to version=$ff_version"
rm -f firefox
ln -sf "$FF_REALDIR" latest
echo "Creating desktop files ..."
if [ ! -d "$APPS_DIRECTORY" ]; then
echo "Applications directory $APPS_DIRECTORY does not exist; creating it ..."
mkdir "$APPS_DIRECTORY"
fi
echo "Creating launcher file $APPS_FULLPATH ..."
# Could use a template for this, but meh
cat > "$APPS_FULLPATH" << EOF
[Desktop Entry]
Name=Firefox $ff_version ($TODAY)
Comment=Browse the World Wide Web
GenericName=Web Browser
X-GNOME-FullName=Firefox $ff_version ($TODAY)
Exec=$ff_exe %u
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=$ff_icon
Categories=Network;WebBrowser;
MimeType=$MIME_TYPES
StartupWMClass=Firefox
StartupNotify=true"
EOF
}
configure_trace
start_trace
main "$@"
echo "DONE, please use xfdesktop --reload or equivalent to refresh menu if necessary"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment