forked from lynxthecat/adblock-lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabl-install.sh
485 lines (420 loc) · 12 KB
/
abl-install.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/bin/sh
# shellcheck disable=SC3043,SC1090
# silence shellcheck warnings
: "${LIBS_SOURCED}"
ABL_CONFIG_DIR=/etc/adblock-lean
ABL_PID_DIR=/tmp/adblock-lean
ABL_CONFIG_FILE=${ABL_CONFIG_DIR}/config
ABL_SERVICE_PATH=/etc/init.d/adblock-lean
ABL_DIR=/var/run/adblock-lean
ABL_INST_DIR="${ABL_DIR}/remote_abl"
UCL_ERR_FILE="${ABL_DIR}/uclient-fetch_err"
ABL_GH_URL_API=https://api.github.com/repos/lynxthecat/adblock-lean
LC_ALL=C
DEFAULT_IFS='
'
_NL_='
'
IFS="${DEFAULT_IFS}"
_DELIM_="$(printf '\35')"
if [ -z "${MSGS_DEST}" ] && [ -t 0 ]
then
MSGS_DEST=/dev/tty
else
MSGS_DEST=/dev/null
fi
DO_DIALOGS=
[ -z "${luci_skip_dialogs}" ] && [ "${MSGS_DEST}" = "/dev/tty" ] && DO_DIALOGS=1
if sed --version 2>/dev/null | grep -qe '(GNU sed)'
then
SED_CMD="sed"
else
SED_CMD="busybox sed"
fi
# exit with code ${1}
# if function 'abl_luci_exit' is defined, execute it before exit
cleanup_and_exit()
{
trap - INT TERM EXIT
rm -rf "${ABL_DIR}" "${ABL_PID_DIR}"
[ -n "${luci_sourced}" ] && abl_inst_luci_exit "${1}"
exit "${1}"
}
check_util()
{
command -v "${1}" 1>/dev/null
}
# asks the user to pick an option
# 1 - input in the format 'a|b|c'
# output via $REPLY
pick_opt()
{
while :
do
printf %s "$1: " 1>${MSGS_DEST}
read -r REPLY
case "$REPLY" in *[!A-Za-z0-9_]*) printf '\n%s\n\n' "Please enter $1" 1>${MSGS_DEST}; continue; esac
eval "case \"$REPLY\" in
$1) return 0 ;;
*) printf '\n%s\n\n' \"Please enter $1\" 1>${MSGS_DEST}
esac"
done
}
get_md5()
{
md5sum "${1}" | cut -d' ' -f1
}
# checks if string $1 is included in newline-separated list $2
# if $3 is specified, uses the value as list delimiter
# result via return status
is_included() {
local delim="${3:-"${_NL_}"}"
case "$2" in
"$1"|"$1${delim}"*|*"${delim}$1"|*"${delim}$1${delim}"*)
return 0 ;;
*)
return 1
esac
}
try_mv()
{
[ -z "${1}" ] || [ -z "${2}" ] && { reg_failure "try_mv(): bad arguments."; return 1; }
mv -f "${1}" "${2}" || { reg_failure "Failed to move '${1}' to '${2}'."; return 1; }
:
}
# 0 - (optional) '-p'
# 1 - path
try_mkdir()
{
local p=
[ "${1}" = '-p' ] && { p='-p'; shift; }
[ -d "${1}" ] && return 0
mkdir ${p} "${1}" || { reg_failure "Failed to create directory '${1}'."; return 1; }
:
}
# prints each argument into a separate line
print_msg()
{
local m
for m in "${@}"
do
printf '%s\n' "${m}" > "$MSGS_DEST"
done
}
log_msg()
{
local m msgs='' msgs_prefix='' _arg err_l=info
local IFS="${DEFAULT_IFS}"
for _arg in "$@"
do
case "${_arg}" in
"-err") err_l=err msgs_prefix="Error: " ;;
'') msgs="${msgs}dummy${_DELIM_}" ;;
*) msgs="${msgs}${msgs_prefix}${_arg}${_DELIM_}"; [ -n "${msgs_prefix}" ] && msgs_prefix=
esac
done
msgs="${msgs%"${_DELIM_}"}"
IFS="${_DELIM_}"
for m in ${msgs}
do
case "${m}" in
dummy) echo ;;
*)
print_msg "${m}"
logger -t abl-install -p user."${err_l}" "${m}"
esac
done
:
}
reg_failure()
{
log_msg -err "" "${1}"
luci_errors="${luci_errors}${1}${_NL_}"
}
# shellcheck disable=SC2120
# get config format from config or main script file contents
# input via STDIN or ${1}
get_config_format()
{
local conf_form_sed_expr='/^[ \t]*(CONFIG_FORMAT|#[ \t]*config_format)=v/{s/.*=v//;p;:1 n;b1;}'
if [ -n "${1}" ]
then
$SED_CMD -En "${conf_form_sed_expr}" "${1}"
else
$SED_CMD -En "${conf_form_sed_expr}"
fi
}
# 1 - new version
# 2 - path to file
update_version()
{
${SED_CMD} -i "/^\s*#\s*ABL_VERSION=/{s/.*/# ABL_VERSION=${1}/;:1 n;b1;}" "${2}"
}
# Get GitHub ref and tarball url for specified version
# 1 - [latest|snapshot|v<version>|tag=<github_tag>|commit=<commit_hash>]
# Output via variables:
# $2 - github ref, $3 - tarball url, $4 - update channel
get_gh_ref_data()
{
local me=get_gh_ref_data ref_fetch_url jsonfilter_ptrn commit version="${1}"
local gh_ref='' gh_channel=''
unset "${2}" "${3}" "${4}"
case "${version}" in
snapshot)
ref_fetch_url="${ABL_GH_URL_API}/commits/master"
jsonfilter_ptrn='@.sha' # latest commit is first on the list
gh_channel=snapshot ;;
latest)
ref_fetch_url="${ABL_GH_URL_API}/releases"
jsonfilter_ptrn='@[@.prerelease=false]' # ignore pre-releases
gh_channel=release ;;
v[0-9]*)
gh_ref="${version}"
gh_channel=release ;;
tag=*)
gh_ref="${version#tag=}"
gh_channel=tag ;;
commit=*)
commit="${version#commit=}"
gh_ref="${commit%"${commit#???????}"}" # trim commit hash to 7 characters
gh_channel=commit ;;
*) reg_failure "${me}: invalid version '${version}'."; return 1
esac
if [ -n "${ref_fetch_url}" ]
then
# Get ref for latest/snapshot
log_msg "Getting GitHub ref for ${version} version of adblock-lean from GitHub."
gh_ref="$(
uclient-fetch -q "${ref_fetch_url}" -O - 2> "${UCL_ERR_FILE}" |
jsonfilter -e "${jsonfilter_ptrn}" |
{
case "${version}" in
snapshot) head -c7 ;;
latest)
jsonfilter -a -e '@[@.target_commitish="master"].tag_name' | # get latest tag for master
head -n1
esac
cat 1>/dev/null
}
)"
fi
# validate resulting ref
case "${gh_ref}" in
''|*[!a-zA-Z0-9._-]*) reg_failure "${me}: failed to get GitHub ref for version '${version}'."; return 1
esac
eval "${2}"='${gh_ref}' "${3}"='${ABL_GH_URL_API}/tarball/${gh_ref}' "${4}"='${gh_channel}'
: "${gh_channel}" # silence shellcheck warning
:
}
# assigns path to extracted distribution directory to $1
# 1 - var name for output
# 2 - tarball url
# 3 - github ref
fetch_abl_dist()
{
local fetch_tarball_url="${2}" fetch_ref="${3}"
local fetch_dir fetch_rv tarball="${ABL_INST_DIR}/remote_abl.tar.gz"
log_msg "Downloading adblock-lean, version '${fetch_ref}'."
rm -rf "${UCL_ERR_FILE}" "${ABL_INST_DIR}/lynxthecat-adblock-lean-"*
uclient-fetch "${fetch_tarball_url}" -O "${tarball}" 2> "${UCL_ERR_FILE}" &&
grep -q "Download completed" "${UCL_ERR_FILE}" &&
tar -C "${ABL_INST_DIR}" -xzf "${tarball}" &&
fetch_dir="$(find "${ABL_INST_DIR}/" -type d -name "lynxthecat-adblock-lean-*")"
fetch_rv=${?}
[ "${fetch_rv}" != 0 ] && [ -s "${UCL_ERR_FILE}" ] && ! grep -q "Download completed" "${UCL_ERR_FILE}" &&
reg_failure "uclient-fetch errors: '$(cat "${UCL_ERR_FILE}")'."
rm -f "${UCL_ERR_FILE}"
eval "${1}"='${fetch_dir}'
: "${fetch_dir}" # silence shellcheck warning
return ${fetch_rv}
}
# 1 - path to distribution dir
# 2 - version string to write to files
install_abl_files()
{
local file preinst_path new_files curr_files
local dist_dir="${1}" version="${2}"
# read new files list
read -r new_files < "${dist_dir}/new_files" ||
{
reg_failure "Failed to read file '${dist_dir}/new_files'."
return 1
}
# read current files list
if [ -f "${dist_dir}/curr_files" ]
then
read -r curr_files < "${dist_dir}/curr_files" ||
{
reg_failure "Failed to read file '${dist_dir}/curr_files'."
return 1
}
fi
# delete obsolete files
for file in ${curr_files}
do
if [ -f "${file}" ] && ! is_included "${file}" "${new_files}" " "
then
log_msg "Deleting obsolete file ${file}."
rm -f "${file}"
fi
done
for file in ${new_files}
do
case "${file##*/}" in
adblock-lean) preinst_path="${dist_dir}/adblock-lean" ;;
*) preinst_path="${dist_dir}/${file}"
esac
# set new ABL_VERSION
update_version "${version}" "${preinst_path}"
if [ -f "${file}" ] && check_util md5sum && [ "$(get_md5 "${preinst_path}")" = "$(get_md5 "${file}")" ]
then
log_msg "File '${file}' did not change - not updating."
else
log_msg "Copying file '${file##*/}'."
{ [ -d "${file%/*}" ] || try_mkdir -p "${file%/*}"; } &&
cp "${preinst_path}" "${file}" ||
{
reg_failure "Failed to copy file '${file##*/}'."
return 1
}
fi
done
chmod +x "${ABL_SERVICE_PATH}"
:
}
inst_failed()
{
local fail_msg="${1}"
[ -s "${UCL_ERR_FILE}" ] && fail_msg="${fail_msg} uclient-fetch errors: '$(cat "${UCL_ERR_FILE}")'"
[ -n "${fail_msg}" ] && reg_failure "${fail_msg}"
reg_failure "Failed to install adblock-lean."
rm -rf "${ABL_INST_DIR}" "${UCL_ERR_FILE}"
}
failsafe_log()
{
printf '%s\n' "${1}" > "${MSGS_DEST:-/dev/tty}"
logger -t adblock-lean "${1}"
}
unexp_arg()
{
inst_failed "abl-install: unexpected argument '${1}'."
}
if ${ABL_SERVICE_PATH} enabled 2>/dev/null
then
${ABL_SERVICE_PATH} stop
fi
trap 'cleanup_and_exit 1' INT TERM
trap 'cleanup_and_exit ${?}' EXIT
try_mkdir -p "${ABL_DIR}" || exit 1
unset VERSION DIST_DIR REF TARBALL_URL UPD_CHANNEL
while getopts ":s:v:" opt; do
case ${opt} in
s) SIM_PATH=${OPTARG} ;;
v) VERSION=${OPTARG} ;;
*) unexp_arg "-${OPTARG}"; exit 1
esac
done
shift $((OPTIND-1))
[ -z "${*}" ] || { unexp_arg "${*}"; exit 1; }
rm -rf "${ABL_INST_DIR}"
try_mkdir -p "${ABL_INST_DIR}" || { inst_failed; exit 1; }
if [ -n "${SIM_PATH}" ]
then
print_msg "" "Running in simulation mode."
[ -d "${SIM_PATH}" ] || { inst_failed "Directory '${SIM_PATH}' does not exist."; exit 1; }
[ -n "${VERSION}" ] || { inst_failed "Specify new version."; exit 1; }
UPD_CHANNEL=dev REF="${VERSION}"
DIST_DIR="${ABL_INST_DIR}/simulation"
try_mkdir -p "${DIST_DIR}" || { inst_failed; exit 1; }
cp -rT "${SIM_PATH}" "${DIST_DIR}"
else
: "${VERSION:=latest}"
get_gh_ref_data "${VERSION}" REF TARBALL_URL UPD_CHANNEL &&
fetch_abl_dist DIST_DIR "${TARBALL_URL}" "${REF}" || { inst_failed; exit 1; }
fi
(
# unset vars and functions from current version to have a clean slate with the new version
clean_abl_env()
{
unset ABL_LIB_FILES ABL_EXTRA_FILES
unset -f abl_post_update_1 abl_post_update_2 get_config_format load_config
}
unset is_update prev_config_format
export pid_file=/tmp/adblock-lean/adblock-lean.pid # for compatibility with older versions
# register config format in the installed adblock-lean version
if [ -s "${ABL_CONFIG_FILE}" ]
then
prev_config_format="$(get_config_format < "${ABL_CONFIG_FILE}")"
fi
# register files list in the installed adblock-lean version
if [ -s "${ABL_SERVICE_PATH}" ]
then
clean_abl_env
is_update=1
touch "${DIST_DIR}/is_update"
curr_abl_files="${ABL_SERVICE_PATH}"
# shellcheck source=/dev/null
if . "${ABL_SERVICE_PATH}"
then
for file in ${ABL_LIB_FILES} ${ABL_EXTRA_FILES}
do
curr_abl_files="${curr_abl_files} ${file}"
done
printf '%s\n' "${curr_abl_files}" > "${DIST_DIR}/curr_files"
fi
fi
clean_abl_env
# shellcheck source=/dev/null
. "${DIST_DIR}/adblock-lean" || { failsafe_log "Error: Failed to source the downloaded script."; exit 1; }
# if updating, call abl_post_update_1() in new version
[ -n "${is_update}" ] && check_util abl_post_update_1 && abl_post_update_1
# register files included in the new version
new_abl_files="${ABL_SERVICE_PATH}"
for file in ${ABL_LIB_FILES} ${ABL_EXTRA_FILES}
do
new_abl_files="${new_abl_files} ${file}"
done
printf '%s\n' "${new_abl_files}" > "${DIST_DIR}/new_files"
# register config format in the new adblock-lean version
check_util get_config_format && upd_config_format="$(get_config_format < "${DIST_DIR}/adblock-lean")"
if [ -n "${upd_config_format}" ] && [ -n "${prev_config_format}" ] && [ "${upd_config_format}" != "${prev_config_format}" ]
then
failsafe_log "NOTE: config format has changed from v${prev_config_format} to v${upd_config_format}."
# load config and call abl_post_update_2() in new version
if { ! check_util source_libs || source_libs "${DIST_DIR}${ABL_LIB_DIR}" "${DIST_DIR}"; } &&
check_util load_config
then
load_config
check_util abl_post_update_2 && abl_post_update_2
else
failsafe_log "Please run 'service adblock-lean start' to initialize the new config."
fi
fi
:
) 1>/dev/null || { inst_failed "Failed to source the new version of adblock-lean. Update is cancelled."; exit 1; }
print_msg ""
install_abl_files "${DIST_DIR}" "${UPD_CHANNEL}_${REF}" || { inst_failed; exit 1; }
IS_UPDATE=
[ -f "${DIST_DIR}/is_update" ] && IS_UPDATE=1
rm -rf "${ABL_DIR}" "${ABL_PID_DIR}" "${UCL_ERR_FILE}"
log_msg "" "adblock-lean ${REF} has been installed."
if [ -n "${DO_DIALOGS}" ]
then
if [ -n "${IS_UPDATE}" ]
then
print_msg "" "Start adblock-lean now? (y|n)"
pick_opt "y|n"
if [ "$REPLY" = y ]
then
${ABL_SERVICE_PATH} start
fi
else
print_msg "" "Set up adblock-lean now? (y|n)"
pick_opt "y|n"
if [ "$REPLY" = y ]
then
${ABL_SERVICE_PATH} setup
fi
fi
fi
: