From 68c5a17f5f15fd73b89364d140862c738b331436 Mon Sep 17 00:00:00 2001 From: Emanuele Petriglia Date: Fri, 15 Jun 2018 20:37:20 +0200 Subject: [PATCH 1/9] Add an initial rewrite of updater.sh script --- updater.sh | 199 ++++++++++++++++++++++++----------------------------- 1 file changed, 90 insertions(+), 109 deletions(-) diff --git a/updater.sh b/updater.sh index ddf623a..01a62ab 100755 --- a/updater.sh +++ b/updater.sh @@ -1,119 +1,100 @@ #!/usr/bin/env bash +# +# ghacks-user.js updater for GNU/Linux and Mac. +# +# Copyright (C) 2018 Emanuele Petriglia . +# All right reserved. This file is licensed under the MIT license. +# +# Version: 1.4 +# +# Please read the wiki or run 'updater.sh --help' to get informations about this +# script. -### ghacks-user.js updater for Mac/Linux -## author: @overdodactyl -## version: 1.3 +# Default values for flags. +QUIET="false" +VERBOSE="false" +FORCE_VERSION="false" +UPDATED="false" -## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in check_for_update() ) - -ghacksjs="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js" -updater="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/updater.sh" -update_pref=${1:--ask} - -currdir=$(pwd) - -## get the full path of this script (readlink for Linux, greadlink for Mac with coreutils installed) -sfp=$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null) - -## fallback for Macs without coreutils -if [ -z "$sfp" ]; then sfp=${BASH_SOURCE[0]}; fi - -## change directory to the Firefox profile directory -cd "$(dirname "${sfp}")" - -## Used to check if a new version of updater.sh is available -update_available="no" -check_for_update () { - online_version="$(curl -s ${updater} | sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p')" - path_to_script="$(dirname "${sfp}")/updater.sh" - current_version="$(sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$path_to_script")" - if [[ "$current_version" < "$online_version" ]]; then - update_available="yes" - fi -} - -## Used to backup the current script, and download and execute the latest version of updater.sh -update_script () { - echo -e "This script will be backed up and the latest version of updater.sh will be executed.\n" - mv updater.sh "updater.sh.backup.$(date +"%Y-%m-%d_%H%M")" - curl -O ${updater} && echo -e "\nThe latest updater script has been downloaded\n" - - # make new file executable - chmod +x updater.sh - - # execute new updater script - ./updater.sh -donotupdate - - # exit script +# Prints a message to the standard error and exit with error code 1. +error() { + echo -e "$@" >&2 exit 1 } - -main () { - ## create backup folder if it doesn't exist - mkdir -p userjs_backups; - - echo -e "\nThis script should be run from your Firefox profile directory.\n" - - echo -e "Updating the user.js for Firefox profile:\n$(pwd)\n" - - if [ -e user.js ]; then - echo "Your current user.js file for this profile will be backed up and the latest ghacks version from github will take its place." - echo -e "\nIf currently using the ghacks user.js, please compare versions:" - echo " Available online: $(curl -s ${ghacksjs} | sed -n '4p')" - echo " Currently using: $(sed -n '4p' user.js)" - else - echo "A user.js file does not exist in this profile. If you continue, the latest ghacks version from github will be downloaded." +# Prints a message to the standard erorr without terminate execution. +warn() { + if [[ "$QUIET" == "false" ]]; then + echo -e "$@" >&2 fi - - echo -e "\nIf a user-overrides.js file exists in this profile, it will be appended to the user.js.\n" - - read -p "Continue Y/N? " -n 1 -r - echo -e "\n\n" - - if [[ $REPLY =~ ^[Yy]$ ]]; then - if [ -e user.js ]; then - # backup current user.js - bakfile="userjs_backups/user.js.backup.$(date +"%Y-%m-%d_%H%M")" - mv user.js "${bakfile}" && echo "Your previous user.js file was backed up: ${bakfile}" - fi - - # download latest ghacks user.js - echo "downloading latest ghacks user.js file" - curl -O ${ghacksjs} && echo "ghacks user.js has been downloaded" - - if [ -e user-overrides.js ]; then - echo "user-overrides.js file found" - cat user-overrides.js >> user.js && echo "user-overrides.js has been appended to user.js" - fi - else - echo "Process aborted" - fi - - ## change directory back to the original working directory - cd "${currdir}" } -update_pref="$(echo $update_pref | tr '[A-Z]' '[a-z]')" -if [ $update_pref = "-donotupdate" ]; then - main -else - check_for_update - if [ $update_available = "no" ]; then - main - else - ## there is an update available - if [ $update_pref = "-update" ]; then - ## update without asking - update_script - else - read -p "There is a newer version of updater.sh available. Download and execute? Y/N? " -n 1 -r - echo -e "\n\n" - if [[ $REPLY =~ ^[Yy]$ ]]; then - update_script - else - main - fi - fi +# Prints a message to the standard output. +log() { + if [[ "$VERBOSE" == "true" && "$QUIET" == "false" ]]; then + echo -e "$@" fi -fi +} + +# Updates the installer script. It set "true" the variable UPDATED if this +# script is succesfully updated. +update_installer() { + local TMPFILE="$(mktemp)" + local UPDATER_URL="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/updater.sh" + + log "Downloading latest updater.sh script to $TMPFILE..." + wget --quiet --output-document "$TMPFILE" "$UPDATER_URL" + + if [[ "$?" == "0" ]]; then + log "Updater script succesfully downloaded!" + UPDATED="true" + else + warn "Failed to download the updater script." + fi + + mv "$TMPFILE" "UPDATER.SH" +} + +# Prints to the standard output the help message. +show_help() { + echo "ciao" +} + +# Prints to the standard output the version of this script. +show_version() { + echo "$PROGRAM version 1.4" +} + +# Updates the user.js. +update_userjs() { + # Run the recenlty downloader version of this script. + if [[ "$UPDATED" == "true" ]]; then + source "$PROGRAM" $@ + exit 0 + fi +} + +# Check if a program is installed. If it is not installed prints an error. +check_utily() { + if [[ -z $(command -v "$1") ]]; then + error "$1 is not installed. Please install it before executing this script" + fi +} + +PROGRAM="${0##*/}" + +check_utily "wget" +check_utily "mktemp" + +case "$1" in + --update|-u) shift; update_installer "$@";; + --force-version|-f) shift; FORCE_VERSION="yes";; + --quiet|-q) shift; QUIET="yes";; + --verbose) shift; VERBOSE="true";; + --help|-h) shift; show_help;; + --version|-v) shift; show_version;; +esac + +update_userjs + +exit 0 From 56e15eb77a50c364fcba7c4aced3769a2a845294 Mon Sep 17 00:00:00 2001 From: Emanuele Petriglia Date: Sat, 16 Jun 2018 10:45:36 +0200 Subject: [PATCH 2/9] Convert warning to error if can't update script --- updater.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/updater.sh b/updater.sh index 01a62ab..e55793b 100755 --- a/updater.sh +++ b/updater.sh @@ -36,7 +36,7 @@ log() { fi } -# Updates the installer script. It set "true" the variable UPDATED if this +# Updates the installer script. It sets "true" the variable UPDATED if this # script is succesfully updated. update_installer() { local TMPFILE="$(mktemp)" @@ -49,7 +49,7 @@ update_installer() { log "Updater script succesfully downloaded!" UPDATED="true" else - warn "Failed to download the updater script." + error "Failed to download the updater script." fi mv "$TMPFILE" "UPDATER.SH" From d821c496253b585ad2a9c6a957c2e669b77b19c7 Mon Sep 17 00:00:00 2001 From: Emanuele Petriglia Date: Sat, 16 Jun 2018 10:59:25 +0200 Subject: [PATCH 3/9] Add help and version messages --- updater.sh | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/updater.sh b/updater.sh index e55793b..ce02c57 100755 --- a/updater.sh +++ b/updater.sh @@ -15,6 +15,7 @@ QUIET="false" VERBOSE="false" FORCE_VERSION="false" UPDATED="false" +VERSION="1.4" # Prints a message to the standard error and exit with error code 1. error() { @@ -57,12 +58,31 @@ update_installer() { # Prints to the standard output the help message. show_help() { - echo "ciao" + show_version + cat <<-_EOF +Usage: + $PROGRAM [--update,-u] [--verbose] [--quiet,-q] [--force-version,-f] + [--help,-h] [--version,-v] + +Options: + -u, --update First updates the updater script, then the user.js + --force-version, -f Force to download the user.js according to the + Firefox version + --quiet, -q Print only errors + --verbose Print additional informations + --help Print this message + --version Print script version + +Please note that it is not given the option '--force-version' this script will +download the latest version available, that can be unstable. + +Please report bugs to https://github.com/ghacksuserjs/ghacks-user.js/issues +_EOF } # Prints to the standard output the version of this script. show_version() { - echo "$PROGRAM version 1.4" + echo "$PROGRAM for ghacks-user.js version $VERSION" } # Updates the user.js. From d215dea5b006dbc5c66dc0fd3c998c6a0fd0eafd Mon Sep 17 00:00:00 2001 From: Emanuele Petriglia Date: Sat, 16 Jun 2018 11:51:23 +0200 Subject: [PATCH 4/9] Improve parsing of command line options --- updater.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/updater.sh b/updater.sh index ce02c57..8e7f244 100755 --- a/updater.sh +++ b/updater.sh @@ -5,17 +5,23 @@ # Copyright (C) 2018 Emanuele Petriglia . # All right reserved. This file is licensed under the MIT license. # +# Special requirements (beyond Bash): wget and mktemp. +# # Version: 1.4 # # Please read the wiki or run 'updater.sh --help' to get informations about this # script. +# +# Report bugs to https://github.com/ghacksuserjs/ghacks-user.js/issues + +readonly VERSION="1.4" # Default values for flags. QUIET="false" VERBOSE="false" FORCE_VERSION="false" UPDATED="false" -VERSION="1.4" + # Prints a message to the standard error and exit with error code 1. error() { @@ -73,6 +79,8 @@ Options: --help Print this message --version Print script version +Please run this script from your Firefox profile directory. + Please note that it is not given the option '--force-version' this script will download the latest version available, that can be unstable. @@ -87,11 +95,12 @@ show_version() { # Updates the user.js. update_userjs() { - # Run the recenlty downloader version of this script. + # Run the recently downloader version of this script. if [[ "$UPDATED" == "true" ]]; then source "$PROGRAM" $@ exit 0 fi + echo "aggiornato" } # Check if a program is installed. If it is not installed prints an error. @@ -106,14 +115,41 @@ PROGRAM="${0##*/}" check_utily "wget" check_utily "mktemp" -case "$1" in - --update|-u) shift; update_installer "$@";; - --force-version|-f) shift; FORCE_VERSION="yes";; - --quiet|-q) shift; QUIET="yes";; - --verbose) shift; VERBOSE="true";; - --help|-h) shift; show_help;; - --version|-v) shift; show_version;; -esac +UPDATE_INSTALLER="false" +SHOW_HELP="false" +SHOW_VERSION="false" + +# Parse command line options. +while :; do + case "$1" in + --update|-u) shift; UPDATE_INSTALLER="true";; + --force-version|-f) shift; FORCE_VERSION="true";; + --quiet|-q) shift; QUIET="true";; + --verbose) shift; VERBOSE="true";; + --help|-h) shift; SHOW_HELP="true";; + --version|-v) shift; SHOW_VERSION="true";; + "") break;; # Default case: no more options. + *) error "Unrecognized option '$1'";; + esac +done + +if [[ "$VERBOSE" == "true" && "$QUIET" == "true" ]]; then + error "You can't use '--verbose' and '--quiet' options together" +fi + +if [[ "$SHOW_HELP" == "true" ]]; then + show_help + exit 0 +fi + +if [[ "$SHOW_VERSION" == "true" ]]; then + show_version + exit 0 +fi + +if [[ "$UPDATE_INSTALLER" == "true" ]]; then + update_installer +fi update_userjs From 6b2c30b7ccb3ce06f2466149d5c704515c3f70e2 Mon Sep 17 00:00:00 2001 From: Emanuele Petriglia Date: Sat, 16 Jun 2018 11:54:08 +0200 Subject: [PATCH 5/9] Add backup option --- updater.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/updater.sh b/updater.sh index 8e7f244..f2e1036 100755 --- a/updater.sh +++ b/updater.sh @@ -14,14 +14,14 @@ # # Report bugs to https://github.com/ghacksuserjs/ghacks-user.js/issues -readonly VERSION="1.4" +readonly VERSION="2.0" # Default values for flags. QUIET="false" VERBOSE="false" FORCE_VERSION="false" UPDATED="false" - +MAKE_BACKUP="false" # Prints a message to the standard error and exit with error code 1. error() { @@ -68,12 +68,13 @@ show_help() { cat <<-_EOF Usage: $PROGRAM [--update,-u] [--verbose] [--quiet,-q] [--force-version,-f] - [--help,-h] [--version,-v] + [--help,-h] [--version,-v] [--backup,-b] Options: -u, --update First updates the updater script, then the user.js --force-version, -f Force to download the user.js according to the Firefox version + --backup, -b Make a copy of the old user.js before overwriting it --quiet, -q Print only errors --verbose Print additional informations --help Print this message @@ -128,6 +129,7 @@ while :; do --verbose) shift; VERBOSE="true";; --help|-h) shift; SHOW_HELP="true";; --version|-v) shift; SHOW_VERSION="true";; + --backup|-b) shift; MAKE_BACKUP="true";; "") break;; # Default case: no more options. *) error "Unrecognized option '$1'";; esac From 49e841527012a642c7f17732d3fe4c6f317c3118 Mon Sep 17 00:00:00 2001 From: Emanuele Petriglia Date: Sat, 16 Jun 2018 12:49:39 +0200 Subject: [PATCH 6/9] Allow updater.sh to update user.js --- updater.sh | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 5 deletions(-) diff --git a/updater.sh b/updater.sh index f2e1036..f675eff 100755 --- a/updater.sh +++ b/updater.sh @@ -16,12 +16,15 @@ readonly VERSION="2.0" +# First part of the URL to get updater.sh and user.js. +readonly REPOSITORY="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js" + # Default values for flags. QUIET="false" VERBOSE="false" FORCE_VERSION="false" -UPDATED="false" MAKE_BACKUP="false" +UPDATED="false" # Prints a message to the standard error and exit with error code 1. error() { @@ -59,7 +62,8 @@ update_installer() { error "Failed to download the updater script." fi - mv "$TMPFILE" "UPDATER.SH" + mv "$TMPFILE" "$PROGRAM" + chmod u+x "$PROGRAM" } # Prints to the standard output the help message. @@ -94,14 +98,98 @@ show_version() { echo "$PROGRAM for ghacks-user.js version $VERSION" } +# Runs the newer script with right command line options given to this script. +run_newer_updater() { + log "Starting newer script..." + + # It recreates the command line options. + if [[ "$QUIET" == "true" ]]; then + QUIET="--qiet" + else + QUIET="" + fi + + if [[ "$VERBOSE" == "true" ]]; then + VERBOSE="--verbose" + else + VERBOSE="" + fi + + if [[ "$FORCE_VERSION" == "true" ]]; then + FORCE_VERSION="--force-version" + else + FORCE_VERSION="" + fi + + if [[ "$MAKE_BACKUP" == "true" ]]; then + MAKE_BACKUP="--backup" + else + MAKE_BACKUP="" + fi + + "./$PROGRAM" "$QUIET" "$VERBOSE" "$FORCE_VERSION" "$MAKE"_"$BACKUP" + exit $? +} + # Updates the user.js. update_userjs() { # Run the recently downloader version of this script. if [[ "$UPDATED" == "true" ]]; then - source "$PROGRAM" $@ - exit 0 + run_newer_updater + fi + + local URL="$REPOSITORY/master/user.js" + + # If '--force-version' is given, it will try to download the user.js version + # according to Firefox version installed on the computer. + if [[ "$FORCE_VERSION" == "true" ]]; then + if [[ $(command -v "firefox") ]]; then + local JS_VERS=$(firefox --version | grep -Eo "[[:digit:]]+.[[:digit:]]+") + local URL_VERSION="$REPOSITORY/$JS_VERS/user.js" + + wget --quiet --spider "$URL_VERSION" + if [[ $? != "0" ]]; then + warn "user.js version $JS_VERS is not available" + else + log "user.js $JS_VERS is available" + URL="$URL_VERSION" + fi + else + warn "Firefox binary not found, so download the latest user.js version" + fi + fi + + # Downloades the user.js file on a temporary file. + local DOWNLOADED_USERJS="$(mktemp)" + log "Downloading newer user.js to $DOWNLOADED_USERJS..." + wget --quiet --output-document "$DOWNLOADED_USERJS" "$URL" + + if [[ $? != "0" ]]; then + error "Failed to download the newer user.js. Check internet connection?" + fi + + # If '--backup' options is given, it will make a copy of the old user.js if it + # is present. + if [[ "$MAKE_BACKUP" == "true" ]]; then + if [[ -e "user.js" ]]; then + log "Copying old user.js to user.js.old" + mv "user.js" "user.js.old" + else + warn "Old user.js not found, so no backup" + fi + fi + + # Finally move downloaded user.js as the newer user.js and append + # user-overrides.js if it is present. + log "Move $DOWNLOADED_USERJS as user.js" + mv "$DOWNLOADED_USERJS" "user.js" + + if [[ -e "user-overrides.js" ]]; then + log "Append user-overrides.js to user.js" + cat "user-overrides.js" >> "user.js" + else + log "No user-overrides.js file found" fi - echo "aggiornato" } # Check if a program is installed. If it is not installed prints an error. From 7296d00e84b44c28053d31fa17c251cf0746fc0b Mon Sep 17 00:00:00 2001 From: Emanuele Petriglia Date: Sat, 16 Jun 2018 13:05:47 +0200 Subject: [PATCH 7/9] Fix updater.sh version number --- updater.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updater.sh b/updater.sh index f675eff..ff31bf0 100755 --- a/updater.sh +++ b/updater.sh @@ -7,7 +7,7 @@ # # Special requirements (beyond Bash): wget and mktemp. # -# Version: 1.4 +# Version: 2.0 # # Please read the wiki or run 'updater.sh --help' to get informations about this # script. From 78432634fb16be6a4c88eb801919ab33d1bcc364 Mon Sep 17 00:00:00 2001 From: Emanuele Petriglia Date: Sun, 17 Jun 2018 19:53:52 +0200 Subject: [PATCH 8/9] Allow to parse old options for updater.sh --- updater.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/updater.sh b/updater.sh index ff31bf0..e185976 100755 --- a/updater.sh +++ b/updater.sh @@ -218,8 +218,15 @@ while :; do --help|-h) shift; SHOW_HELP="true";; --version|-v) shift; SHOW_VERSION="true";; --backup|-b) shift; MAKE_BACKUP="true";; + + # Deprecated options. + -donotupdate) shift; warn "'-donotupdate' is a deprecated option";; + -update) shift; warn "'-update' is a deprecated option";; + + # Special cases. "") break;; # Default case: no more options. *) error "Unrecognized option '$1'";; + esac done From 9256e77ac2776489b733faf25027b0eedf870fd9 Mon Sep 17 00:00:00 2001 From: Emanuele Petriglia Date: Sun, 17 Jun 2018 19:54:18 +0200 Subject: [PATCH 9/9] Mark output messagges (log, error or warning) --- updater.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/updater.sh b/updater.sh index e185976..048d2d5 100755 --- a/updater.sh +++ b/updater.sh @@ -28,21 +28,21 @@ UPDATED="false" # Prints a message to the standard error and exit with error code 1. error() { - echo -e "$@" >&2 + echo "[Error] $@" >&2 exit 1 } # Prints a message to the standard erorr without terminate execution. warn() { if [[ "$QUIET" == "false" ]]; then - echo -e "$@" >&2 + echo "[Warning] $@" >&2 fi } # Prints a message to the standard output. log() { if [[ "$VERBOSE" == "true" && "$QUIET" == "false" ]]; then - echo -e "$@" + echo "[Log] $@" fi }