updater.sh: more useful comments + minor refactoring

This commit is contained in:
fluorescent_haze 2022-06-02 12:57:27 +00:00
parent bb0acf1c76
commit 1b1ec00c1d

View file

@ -1,6 +1,8 @@
#!/bin/sh -fe #!/bin/sh -fe
# arkenfox user.js updater for UNIX-like systems # arkenfox user.js updater for UNIX-like systems
#
# By default, the directory in which it is run is assumed to be the firefox
# profile directory.
die() { die() {
printf "${red}!! %s${nc}\n" "$*" printf "${red}!! %s${nc}\n" "$*"
@ -53,11 +55,10 @@ yel='\033[33m'
cya='\033[36m' cya='\033[36m'
nc='\033[m' nc='\033[m'
# more variables # default values for variables
esr=0 esr=0
prompt=1 prompt=1
overrides=1 overrides=1
cmd_get=
#################### ####################
@ -70,7 +71,7 @@ if command -v curl >/dev/null; then
elif command -v wget >/dev/null; then elif command -v wget >/dev/null; then
cmd_get='wget -qO' cmd_get='wget -qO'
else else
die This script needs curl or wget, aborting. die This script needs curl or wget, aborting
fi fi
# Parse commandline options # Parse commandline options
@ -87,22 +88,27 @@ while getopts :hefn opt; do
done done
shift "$((OPTIND - 1))" shift "$((OPTIND - 1))"
# If no argument is given, assume the script's locaction
# as the target firefox profile directory.
if [ "$1" ]; then if [ "$1" ]; then
if [ -d "$1" ]; then if [ -d "$1" ]; then
dir="$1" dir="$1"
else else
die "'$1': no such directory" die "'$1': no such directory"
fi fi
else
dir="$(echo "$0" | sed -E 's/\/[^\/]*$//')"
fi fi
# Prompt user for confirmation # Prompt user for confirmation
pmt_yn 'Update user.js and append custom configuration from user-overrides.js?' \ pmt_yn 'Update user.js and append custom configuration from user-overrides.js?' \
|| exit 0 || exit 0
[ "$dir" ] || dir="$(echo "$0" | sed -E 's/\/[^\/]*$//')" cd "$dir" || die "Couldn't change directory to '$dir', aborting"
cd "$dir" || die "Couldn't change directory to '$dir', aborting."
# Assume that a valid firefox profile directory has a prefs.js file # Assume that a valid firefox profile directory has a prefs.js file.
# If it doesn't, prompt the user for confirmation.
if ! [ -f "prefs.js" ]; then if ! [ -f "prefs.js" ]; then
# Prompt if the user wants to continue, even if it doesn't look like a # Prompt if the user wants to continue, even if it doesn't look like a
# firefox profile directory. # firefox profile directory.
@ -112,20 +118,20 @@ fi
# Create backup of user.js # Create backup of user.js
if [ -f user.js ]; then if [ -f user.js ]; then
# use ISO 8601 date format, instead of making up our own. # Use the ISO 8601 date format instead of making up our own.
bak="userjs_backups/user.js.$(date "+%Y-%m-%dT%H:%M%z")" bak="userjs_backups/user.js.$(date "+%Y-%m-%dT%H:%M%z")"
log Creating a backup of user.js in "$bak" log Creating a backup of user.js in "$bak"
mkdir -p userjs_backups 2>/dev/null mkdir -p userjs_backups
cp -f user.js "$bak" 2>/dev/null || cp -f user.js "$bak" 2>/dev/null \
die "Couldn't create a backup of user.js, aborting." || die "Couldn't create a backup of user.js, aborting."
fi fi
# Download user.js # Download user.js
log Fetching new version of user.js log Fetching new version of user.js
$cmd_get user.js \ $cmd_get user.js \
https://raw.githubusercontent.com/arkenfox/user.js/master/user.js \ https://raw.githubusercontent.com/arkenfox/user.js/master/user.js \
|| die "Couldn't download user.js, aborting." || die "Couldn't download user.js, aborting."
# ESR preferences # ESR preferences
if [ "$esr" = 1 ]; then if [ "$esr" = 1 ]; then