-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvsp
executable file
·269 lines (250 loc) · 10.7 KB
/
vsp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env bash
# vivaldi sync prefs - vsp
# https://github.com/kurtlieber/vivaldi-customizations
# this is the profile that will be used as the "gold image" for your other profiles.
# it **must** match exactly the directory in which the profile is stored.
# 99% of the time, it will either be "Default" or "Profile <num>"
#
# you can override this on the command line with '-M /path/to/master/profile/dir'
master_profile="Default"
# don't change this unless you've installed vivaldi's config directory in a non-standard location.
#vivaldi_config_dir="${HOME}/.config/vivaldi"
vivaldi_config_dir="${HOME}/temp"
jq=$(which jq)
# in case you want/need to hard-code path to jq
# jq = "/path/to/jq"
###############################################################################
# you shouldn't need to change anything below this line
###############################################################################
usage() {
local f="$(basename ${0})"
echo "${f} will update vivaldi-specific preferences, which are not currently supported by Vivaldi sync."
echo ""
echo "***close out of vivaldi before running this script or it may (will) overwrite your changes***"
echo ""
echo "Currently supports:"
echo ""
echo " - navigation toolbar"
echo " - panels toolbar"
echo " - custom web panels in the panel toolbar"
echo ""
echo "Usage: ${f} [-s] [-v] [-M <value>]"
echo ""
echo "Options:"
echo " -s Run silently, with no confirmation"
echo " -M <value> Use this vivaldi profile as the master profile (overrides \$master_profile in script)"
echo " -v Be verbose"
echo ""
echo "${f} will pull the key value for the selected Preferences from the master profile,"
echo "as defined by -M <path> or by \$master_profile in the script itself."
echo ""
echo "The master profile defaults to ~/.config/vivaldi/Default"
echo ""
echo "Dependencies: jq - the command-line JSON processor"
exit 1
}
verbose() {
if [ -n "$verbose" ]; then
echo "$1"
fi
}
debug() {
if [ -n "$debug" ]; then
echo "$1"
fi
}
jq_write() {
local f="$1"
local s="$2"
# it kills me that I have to write the jq query code twice, but I can't figure out how
# to write the last line based on a variable
if [ -n "$f" ] && [ -n "$s" ] && [ -z "$export_file_location" ] && [ -z "$import_file_location" ]; then
debug "DEBUG INFO FROM jq_write function where -e is NOT set"
#debug "jq = ${jq}"
#debug "export_file_location = ${export_file_location}"
#debug "new_nav_toolbar = ${nav_toolbar}"
#debug "panels = ${panels}"
#debug "new_panel_toolbar = ${panels_toolbar}"
debug "f = ${f}"
debug "s = ${s}"
${jq} \
--argjson new_nav_toolbar "${nav_toolbar}" \
--argjson new_panel "${panels}" \
--argjson new_panel_toolbar "${panels_toolbar}" \
'.vivaldi.toolbars.navigation = $new_nav_toolbar |
.vivaldi.panels = $new_panel |
.vivaldi.toolbars.panel = $new_panel_toolbar' \
"${f}" > ${s} #jq can't edit files in place, so we have to create a temp file.
# both import and export don't have a starting file to modify, so we need to use the -n flag with jq to get it to work
elif [ -n "$s" ]; then
debug "DEBUG INFO FROM jq_write function where -e is set"
#debug "jq = ${jq}"
#debug "export_file_location = ${export_file_location}"
#debug "import_file_location = ${import_file_location}"
#debug "new_nav_toolbar = ${nav_toolbar}"
#debug "panels = ${panels}"
#debug "new_panel_toolbar = ${panels_toolbar}"
#debug "s = ${s}"
${jq} -n \
--argjson new_nav_toolbar "${nav_toolbar}" \
--argjson new_panel "${panels}" \
--argjson new_panel_toolbar "${panels_toolbar}" \
'.vivaldi.toolbars.navigation = $new_nav_toolbar |
.vivaldi.panels = $new_panel |
.vivaldi.toolbars.panel = $new_panel_toolbar' \
> ${s}
else
echo "something bad happened in the jq_write function"
verbose "\$f = ${f}" # debugging
verbose "\$s = ${s}" # debugging
fi
}
error() {
local m="$1"
local c="$2"
echo "Error: ${1}"
exit $2
}
pref_file_location="${vivaldi_config_dir%/}/${master_profile}"
while getopts ":sM:ve:i:d" opt; do
case "$opt" in
s)
silent=1
;;
M)
master_dir=1
pref_file_location="$(echo "${OPTARG}" | sed 's/^"\(.*\)"$/\1/')" # in case the dir path is enclosed in quotes. might be a better way to handle this - if so, tell me
pref_file_location="$(eval echo ${pref_file_location})" #in case someone uses ~/ for their home dir. again, might be a better way. I'm all ears
if [ ! -d "${pref_file_location}" ]; then
error "-M is either missing or not a directory. Check and run again." 1
fi
if [ ! -f "${pref_file_location}/Preferences" ]; then
error "-M does not appear to be a valid Vivaldi profile directory. Preferences file not found. Please check and run again." 1
fi
debug "pref_file_location set to ${pref_file_location} via -M flag"
;;
v)
verbose=1
;;
e)
if [ -z "${OPTARG}" ]; then
echo "OPTARG = ${OPTARG}"
error "Missing argument to -e. path expected" 1
else
export_file_location="$(echo "${OPTARG}" | sed 's/^"\(.*\)"$/\1/')"
export_file_location="$(eval echo ${export_file_location})"
fi
;;
i)
if [ -z "${OPTARG}" ]; then
error "Missing argument to -i. path expected" 1
else
import_file_location="$(echo "${OPTARG}" | sed 's/^"\(.*\)"$/\1/')"
import_file_location="$(eval echo ${import_file_location})"
fi
;;
d)
debug=1
;;
\?)
usage
;;
esac
done
if [ ! -x "${jq}" ]; then
error "Missing jq dependency. Please ensure it is installed and available in your \$PATH" 1
fi
if [ ! -z ${import_file_location} ] && [ ! -z ${export_file_location} ]; then
debug "import_file_location = ${import_file_location}"
debug "export_file_location = ${export_file_location}"
error "Can't import and export at the same time. Pick one and re-run." 1
fi
if pgrep -f "vivaldi" > /dev/null; then
echo "Detected vivaldi running. If you're only running your master profile, this is fine."
echo "However, if you're running any of the profiles you're seeking to modify, Vivaldi will"
echo "overwrite those changes. Please exit out of those profiles and run the script again."
read -rp "Would you like to continue?(Y/n): " confirm
confirm="${confirm,,}"
if [ "$confirm" != "y" ] && [ "$confirm" != "yes" ] && [ "$confirm" != "" ]; then
error "Operation canceled." 1
fi
fi
if [ -n "${import_file_location}" ]; then
if [ ! -f "${import_file_location}" ]; then
error "${import_file_location} is either missing or you do not have access." 1
else
master_file="${import_file_location}"
debug "master_file set via import_file_location section to: ${master_file}"
fi
else
if [ ! -d "${pref_file_location}" ]; then
debug "master_profile = ${master_profile}"
error "Missing or unknown profile directory. Please check \$master_profile in the script and try again." 1
fi
if [ ! -f "${pref_file_location}/Preferences" ]; then
debug "pref_file_location = ${pref_file_location}"
error "Missing Preferences file in ${pref_file_location}." 1
else
master_file="${pref_file_location}/Preferences"
debug "master_file just set to ${master_file}"
fi
if [ -z "$silent" ] && [ -z "$master_dir" ]; then
read -rp "You're about to pull Preferences from the ${profile_name} profile. Is that correct? (Y/n): " confirm
confirm="${confirm,,}"
if [ "$confirm" != "y" ] && [ "$confirm" != "yes" ] && [ "$confirm" != "" ]; then
error "Operation canceled." 1
fi
fi
fi
profile_name=$(${jq} -r '.profile.name' "${master_file}")
nav_toolbar=$(${jq} -r '.vivaldi.toolbars.navigation' "${master_file}")
panels=$(${jq} -r '.vivaldi.panels' "${master_file}")
panels_toolbar=$(${jq} -r '.vivaldi.toolbars.panel' "${master_file}")
debug "-M set to ${pref_file_location}"
debug "Profile name = ${profile_name}"
#debug "Nav toolbar = ${nav_toolbar}"
debug "master_file = ${master_file}"
if [ -n "$export_file_location" ]; then
pref_file=""
if [ ! -d "$(dirname "$export_file_location")" ]; then
echo "Error: The directory $(dirname "$export_file_location") does not exist."
exit 1
fi
if [ -e "$export_file_location" ]; then
if [ -z "$silent" ]; then
read -rp "The file already exists. Do you want to overwrite it? (Y/n): " overwrite
overwrite="${overwrite,,}"
else
overwrite="y" # if you run it silently, it will overwrite silently.
fi
if [ "$overwrite" != "y" ] && [ "$overwrite" != "yes" ] && [ "$overwrite" != "" ]; then
verbose "File not saved."
exit 0
fi
fi
if [ ! -w "$(dirname "$export_file_location")" ]; then
error "Error: You do not have write permissions to the directory $(dirname "$export_file_location").\n Pick an alternate location and try again." 1
fi
jq_write "${pref_file}" "${export_file_location}"
else
for dir in "$vivaldi_config_dir"/Profile* "$vivaldi_config_dir/Default"; do
if [ "${dir}" != "${pref_file_location}" ]; then
if [ -f "${dir}/Preferences" ]; then
pref_file="${dir}/Preferences"
pref_file_bak="${dir}/Preferences.bak"
save_file="/tmp/NewPreferences"
verbose "Creating backup of ${pref_file} at ${pref_file_bak}"
cp "${pref_file}" "${pref_file_bak}" #make a backup, just in case
jq_write "${pref_file}" "${save_file}"
verbose "Updating ${pref_file}"
debug "pref_file = ${pref_file}"
debug "pref_file_bak = ${pref_file_bak}"
debug "save_file = ${save_file}"
if [ -f "${save_file}" ] && [ -f "${pref_file_bak}" ]; then
verbose "Installing new Preferences file. Old one can be restored from backup at ${pref_file_bak} if needed"
mv "${save_file}" "${pref_file}"
fi
fi
fi
done
fi