mirror of
https://github.com/arkenfox/user.js.git
synced 2025-05-07 17:33:45 +02:00
Add an initial rewrite of updater.sh script
This commit is contained in:
parent
a2b5e1e7cf
commit
68c5a17f5f
1 changed files with 90 additions and 109 deletions
195
updater.sh
195
updater.sh
|
@ -1,119 +1,100 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
#
|
||||||
|
# ghacks-user.js updater for GNU/Linux and Mac.
|
||||||
|
#
|
||||||
|
# Copyright (C) 2018 Emanuele Petriglia <inbox@emanuelepetriglia.me>.
|
||||||
|
# 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
|
# Default values for flags.
|
||||||
## author: @overdodactyl
|
QUIET="false"
|
||||||
## version: 1.3
|
VERBOSE="false"
|
||||||
|
FORCE_VERSION="false"
|
||||||
|
UPDATED="false"
|
||||||
|
|
||||||
## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in check_for_update() )
|
# Prints a message to the standard error and exit with error code 1.
|
||||||
|
error() {
|
||||||
ghacksjs="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js"
|
echo -e "$@" >&2
|
||||||
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
|
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Prints a message to the standard erorr without terminate execution.
|
||||||
main () {
|
warn() {
|
||||||
## create backup folder if it doesn't exist
|
if [[ "$QUIET" == "false" ]]; then
|
||||||
mkdir -p userjs_backups;
|
echo -e "$@" >&2
|
||||||
|
|
||||||
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."
|
|
||||||
fi
|
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]')"
|
# Prints a message to the standard output.
|
||||||
if [ $update_pref = "-donotupdate" ]; then
|
log() {
|
||||||
main
|
if [[ "$VERBOSE" == "true" && "$QUIET" == "false" ]]; then
|
||||||
else
|
echo -e "$@"
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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
|
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
|
fi
|
||||||
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue