forked from arkenfox/user.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.sh
executable file
·155 lines (130 loc) · 3.57 KB
/
updater.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh -fe
# 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() {
printf "${red}!! %s${nc}\n" "$*"
exit 1
}
log() {
printf "${cya}-> ${nc}%s\n" "$*"
}
war_yn() {
[ "$prompt" -eq 0 ] && return 0
printf "${yel}%s${nc} [Y/n] " "$*"
read_yn
return $?
}
pmt_yn() {
[ "$prompt" -eq 0 ] && return 0
printf "${cya}%s${nc} [Y/n] " "$*"
read_yn
return $?
}
read_yn() {
read -r tmp
case "$tmp" in
[Nn]*) return 1 ;;
*) return 0 ;;
esac
}
usage() {
cat << EOF
usage: updater.sh [options] [<profile>]
Update the user.js file and append custom configuration.
options:
-e Activate ESR preferences
-f Don't prompt for confirmation
-h Show this help message
-n Don't append user overrides
EOF
}
# Define color escape sequences
red='\033[31m'
yel='\033[33m'
cya='\033[36m'
nc='\033[m'
# default values for variables
esr=0
prompt=1
overrides=1
####################
# Main #
####################
# set command for downloading user.js
if command -v curl >/dev/null; then
cmd_get='curl -Lso'
elif command -v wget >/dev/null; then
cmd_get='wget -qO'
else
die This script needs curl or wget, aborting
fi
# Parse commandline options
while getopts :hefn opt; do
case "$opt" in
e) esr=1 ;;
f) prompt=0 ;;
h) usage; exit 0 ;;
n) overrides=0 ;;
:) die "Option -$OPTARG requires argument." ;;
?) die "Invalid option -$OPTARG" ;;
*) exit ;;
esac
done
shift "$((OPTIND - 1))"
# If no argument is given, assume the script's locaction
# as the target firefox profile directory.
if [ "$1" ]; then
if [ -d "$1" ]; then
dir="$1"
else
die "'$1': no such directory"
fi
else
dir="$(echo "$0" | sed -E 's/\/[^\/]*$//')"
fi
# Prompt user for confirmation
pmt_yn 'Update user.js and append custom configuration from user-overrides.js?' \
|| exit 0
cd "$dir" || die "Couldn't change directory to '$dir', aborting"
# 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
# Prompt if the user wants to continue, even if it doesn't look like a
# firefox profile directory.
war_yn "'$PWD' doesn't look like a firefox profile directory, continue anyway?" \
|| exit 0
fi
# Create backup of user.js
if [ -f user.js ]; then
# Use the ISO 8601 date format instead of making up our own.
bak="userjs_backups/user.js.$(date "+%Y-%m-%dT%H:%M%z")"
log Creating a backup of user.js in "$bak"
mkdir -p userjs_backups
cp -f user.js "$bak" 2>/dev/null \
|| die "Couldn't create a backup of user.js, aborting."
fi
# Download user.js
log Fetching new version of user.js
$cmd_get user.js \
https://raw.githubusercontent.com/arkenfox/user.js/master/user.js \
|| die "Couldn't download user.js, aborting."
# ESR preferences
if [ "$esr" = 1 ]; then
log Applying ESR preferences
# shellcheck disable=SC2015
sed '/\/\* ESR91\.x still uses all the following prefs/s/^\/\*/\/\//' user.js > _ \
&& mv _ user.js || die "Couldn't apply ESR preferences"
fi
# Apply overrides
if [ "$overrides" -eq 1 ]; then
if [ -f user-overrides.js ]; then
log Applying overrides in user-overrides.js
echo >> user.js
cat user-overrides.js >> user.js
else
log user-overrides.js not found, skipping overriding user preferences...
fi
fi
log Updating user.js finished successfully!