-
Notifications
You must be signed in to change notification settings - Fork 0
/
tru
executable file
·91 lines (80 loc) · 2.73 KB
/
tru
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
#!/bin/sh
# vi:et lbr noet sw=2 ts=2 tw=79 wrap
# Copyright 2020-2024 David Rabkin
# tru stands for Transmission Remote Updater. The script removes a torrent with
# content and than adds it again. It is useful to automatically increase a
# ratio.
# The script uses local variables which are not POSIX but supported by most
# shells. See:
# https://stackoverflow.com/q/18597697
# shellcheck disable=SC3043 # Uses local variables.
# shellcheck disable=SC1091,SC2034 # File not following, appears unused.
readonly \
BASE_APP_VERSION=0.9.20240813 \
BASE_MIN_VERSION=0.9.20240104
. base.sh
# Looks for torrent ID by torrent name extracted from torrent file name.
# Continues if the ID is not existed.
main() {
validation "$@"
local tid
tid=$(tid) || die
if [ -n "$tid" ]; then
{
# shellcheck disable=SC2086 # Double quote to prevent globbing.
transmission-remote "$SER" $AUT --torrent "$tid" --remove-and-delete \
2>&1 1>&3 3>&- | tologe
} \
3>&1 1>&2 | tolog
log "$NME $tid is removed from $SER."
fi
# Adds torrent file name to torrent server.
{
# shellcheck disable=SC2086 # Double quote to prevent globbing.
transmission-remote "$SER" $AUT --add "$TOR" \
2>&1 1>&3 3>&- | tologe
} \
3>&1 1>&2 | tolog
# Verifies that a torrent was added.
tid=$(tid) || die
log "$NME $tid is added to $SER."
}
# Looks for torrent ID by torrent file name.
tid() {
local out
# shellcheck disable=SC2086 # Double quote to prevent globbing.
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 "Something wrong with grep $NME on $SER."
out="$(printf %s "$out" | awk '{print $1}')" || die Wrong format: "$out"
[ -n "$out" ] || die Something wrong with awk: "$out"
printf %s "$out"
}
validation() {
validate_cmd awk nc tr transmission-remote transmission-show
[ "$#" -eq 3 ] || die tru: try tru [host:port user:pass torrent-file]
# Single ':' means there is no authentication data.
[ "$2" != : ] && AUT="--auth $2" || AUT=''
readonly \
AUT \
SER="$1" \
TOR="$3"
# Prepares host and port to be nc command parameters. Validates the first
# parameter host:port.
local out prm
prm="$(printf %s "$SER" | tr : \ )"
# shellcheck disable=SC2086 # Double quote to prevent globbing.
nc -z $prm >/dev/null 2>&1 || die "$prm" is not available.
# Validates third parameter full torrent file name.
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"
}
# Starting point.
main "$@"