-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtru
More file actions
executable file
·85 lines (79 loc) · 2.51 KB
/
Copy pathtru
File metadata and controls
executable file
·85 lines (79 loc) · 2.51 KB
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
#!/bin/sh
# vi: lbr noet sw=2 ts=2 tw=79 wrap
# SPDX-FileCopyrightText: 2020-2026 David Rabkin
# SPDX-License-Identifier: 0BSD
#
# Removes a torrent and its content, then adds it again to increase the share
# ratio; the name stands for Transmission Remote Updater.
#
# The script uses local variables which are not POSIX but supported by most
# shells. See:
# https://stackoverflow.com/q/18597697
# shellcheck disable=SC1091,SC2034,SC2039,SC3043
readonly \
BASE_APP_VERSION=0.9.20260711 \
BASE_MIN_VERSION=0.9.20260707
. base.sh
# Validates arguments and runs the remove-then-add cycle.
main() {
validate "$@"
process
}
# Removes the existing torrent if present, then re-adds it from the file.
process() {
local tid
tid=$(tid) || die
if [ -n "$tid" ]; then
xmit --torrent "$tid" --remove-and-delete
log "$NME $tid is removed from $SER."
fi
xmit --add "$TOR"
tid=$(tid) || die
log "$NME $tid is added to $SER."
}
# Looks up the torrent ID by the torrent name.
# Word splitting required for AUT:
# shellcheck disable=SC2086
tid() {
local out
out="$(transmission-remote "$SER" $AUT --list 2>&1)" ||
die "Unable to communicate with $SER: $out"
out="$(printf %s "$out" | grep "$NME")" || {
log "There is no $NME in $SER."
return 0
}
[ -n "$out" ] || die "Unexpected empty match for $NME on $SER."
out="$(printf %s "$out" | awk '{print $1}')" || die Wrong format: "$out"
[ -n "$out" ] || die Unable to extract the torrent ID: "$out"
printf %s "$out"
}
# Validates arguments and sets SER, AUT, TOR, and NME. A lone ':' as the second
# argument means no authentication data. Splits host:port into nc parameters,
# then validates the torrent file given as the third argument.
# Word splitting required for prm:
# shellcheck disable=SC2086
validate() {
cmd_exists awk nc tr transmission-remote transmission-show || die
[ "$#" -eq 3 ] || die tru: try tru [host:port user:pass torrent-file]
[ "$2" != : ] && AUT="--auth $2" || AUT=''
readonly \
AUT \
SER="$1" \
TOR="$3"
local out prm
prm="$(printf %s "$SER" | tr : \ )"
nc -z $prm >/dev/null 2>&1 || die "$prm" is not available.
isreadable "$TOR" || die Unable to read "$TOR".
out="$(transmission-show "$TOR")" || die Unable to read "$TOR": "$out"
NME="$(printf %s "$out" | grep Name: | head -1 | cut -c 7-)"
readonly NME
[ -n "$NME" ] || die Unable to extract name from "$TOR": "$out"
}
# Wraps transmission-remote with cmd_run.
# Word splitting required for AUT:
# shellcheck disable=SC2086
xmit() {
cmd_run transmission-remote "$SER" $AUT "$@"
}
# Entry point.
main "$@"