Skip to content

Instantly share code, notes, and snippets.

@ffflorian
Last active February 10, 2019 21:23
Show Gist options
  • Save ffflorian/d4bcc008c2dde5a85f8f6587590f7281 to your computer and use it in GitHub Desktop.
Save ffflorian/d4bcc008c2dde5a85f8f6587590f7281 to your computer and use it in GitHub Desktop.
Update (or install) the latest Firefox
#!/usr/bin/env bash
# Update (or install) the latest Firefox
# Optimized for Debian + GNOME
# ffflorian 2017
set -e
SCRIPT_NAME="${0##*/}"
INSTALL_DIR="/opt/firefox"
EXEC_BIN="${INSTALL_DIR}/firefox"
TEMP_DIR="$(mktemp -d)"
ARCHIVE_DIR="${TEMP_DIR}/linux-x86_64/en-US"
SHASUMS_FILE="${TEMP_DIR}/SHA256SUMS"
ICON_FILE="${INSTALL_DIR}/browser/chrome/icons/default/default128.png"
DESKTOP_FILE="/usr/share/applications/firefox.desktop"
ARCHIVE_URL="https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=en-US"
USE_CURL="yes"
FORCE="no"
_cleanup() {
rm -rf "${TEMP_DIR}"
}
trap _cleanup EXIT
_compare_version() {
printf "%03d%03d%03d%03d" $(echo "${1}" | tr '.' ' ')
}
_print_usage() {
cat <<EOF
Usage: ${SCRIPT_NAME} [option]
Options:
--force (-f) Reinstall Firefox if it is already installed.
Commands:
--help (-h) Display this help message
EOF
}
while :
do
case "${1}" in
-f|--force )
FORCE="force"
shift
;;
-h|--help )
_print_usage
exit 0
;;
* )
break
;;
esac
done
mkdir -p "${ARCHIVE_DIR}"
if [ -d "${INSTALL_DIR}" ]; then
if [ -r "${EXEC_BIN}" ]; then
CURRENT_VERSION="$("${EXEC_BIN}" --version | sed -n 's/Mozilla Firefox \(.*\)$/\1/p')"
else
read -r -p "The current Firefox installation seems to be broken. Would you like to reinstall the latest version? [y/N] " RESPONSE
case "${RESPONSE}" in
[nN][oO]|[nN]|"" ) exit 0 ;;
esac
FORCE="force"
fi
else
read -r -p "Firefox is not installed yet. Would you like to install it? [y/N] " RESPONSE
case "${RESPONSE}" in
[nN][oO]|[nN]|"" ) exit 0 ;;
esac
FORCE="force"
fi
if ! command -v "curl" > /dev/null; then
echo "This script needs curl to run."
exit 1
# read -r -p "Info: could not find curl. wget can be used instead but the \
# Firefox installer hashsum won't be checked then. Would you like to continue? [y/N] " RESPONSE
# case "${RESPONSE}" in
# [nN][oO]|[nN]|"" ) exit 0 ;;
# esac
# USE_CURL="no"
# if ! command -v "wget" > /dev/null; then
# echo "Error: could not find curl or wget."
# exit 1
# fi
fi
if [ "${USE_CURL}" = "yes" ]; then
REDIRECT_URL="$(curl -w "%{url_effective}\\n" -I -L -s -S "${ARCHIVE_URL}" -o /dev/null)"
ARCHIVE_FILENAME="$(sed -n 's/.*\/en-US\/\(.*\)$/\1/p' <<< "${REDIRECT_URL}")"
ARCHIVE_FILE="${ARCHIVE_DIR}/${ARCHIVE_FILENAME}"
BASE_URL="$(sed -n 's/^\(.*\/releases\/[^\/]*\)\/.*/\1/p' <<< "${REDIRECT_URL}")"
SHASUMS_URL="${BASE_URL}/SHA256SUMS"
LATEST_VERSION="$(sed -n 's/^.*\/releases\/\([^\/]*\)/\1/p' <<< "${BASE_URL}")"
if [ "${FORCE}" != "force" ]; then
if [ "$(_compare_version "${LATEST_VERSION}")" -le "$(_compare_version "${CURRENT_VERSION}")" ]; then
echo "No update needed, ${CURRENT_VERSION} is the latest Firefox version available."
echo "Run this script with --force to reinstall this Firefox version."
exit 0
fi
fi
echo "Downloading Firefox ${LATEST_VERSION} from ${REDIRECT_URL} ... "
curl -L "${ARCHIVE_URL}" -o "${ARCHIVE_FILE}"
curl -L "${SHASUMS_URL}" -o "${SHASUMS_FILE}"
(
echo "Checking SHA256 hashsum ..."
cd "${TEMP_DIR}" || exit 1
sha256sum --quiet --ignore-missing -c "${SHASUMS_FILE}"
)
else
echo "This script needs curl to run."
exit 1
#echo "Downloading Firefox from ${ARCHIVE_URL} ... "
#wget -O "${ARCHIVE_FILE}" "${ARCHIVE_URL}"
fi
printf "OK\\n\\n"
if [ ! -r "${ARCHIVE_FILE}" ]; then
echo "Error: could not find downloaded file \"${ARCHIVE_FILE}\"".
exit 1
fi
echo "Asking for sudo rights to prepare extraction ... "
sudo mkdir -p "${INSTALL_DIR}"
printf "Extracting Firefox %s to %s ... " "${LATEST_VERSION}" "${INSTALL_DIR}"
sudo tar -xjf "${ARCHIVE_FILE}" --strip-components 1 -C "${INSTALL_DIR}"
printf "OK\\n\\n"
printf "Creating soft link at /usr/bin ... "
sudo ln -fs "${EXEC_BIN}" "/usr/bin/"
printf "OK\\n\\n"
printf "Setting start menu entry ... "
sudo tee "${DESKTOP_FILE}" > /dev/null <<EOF
[Desktop Entry]
Name=Firefox
Comment=Browse the World Wide Web
GenericName=Web Browser
X-GNOME-FullName=Firefox Web Browser
Exec=${EXEC_BIN} %u
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=${ICON_FILE}
Categories=Network;WebBrowser;
MimeType=text/html;text/xml;application/xhtml+xml;application/xml;application/vnd.mozilla.xul+xml;application/rss+xml;application/rdf+xml;image/gif;image/jpeg;image/png;x-scheme-handler/http;x-scheme-handler/https;
StartupWMClass=Firefox
StartupNotify=true
EOF
if [ -r "${ICON_FILE}" ]; then
printf "OK\\n\\n"
else
printf "\\Warning: could not find application icon \"%s\".\\n\\n" "${ICON_FILE}"
fi
echo "All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment