-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm
executable file
·119 lines (96 loc) · 2.29 KB
/
tm
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
#!/usr/bin/env bash
# Simple script for adding torrents to `transmission` via magnet links and
# torrent files.
#
# The `transmission-add` script is an external Python tool that can be used to
# add magnet links, assign labels to torrents, and move torrent files to
# different locations.
set -eou pipefail
PROG=$(basename "$0")
CMD="transmission-daemon"
declare -a DEPS=(tm-add tremc)
function _usage {
cat <<-_EOF
usage: $PROG [options]
options:
-a, add Add torrent by magnet link
-s, start Start daemon
-k, kill Stop daemon
-x, tremc Run tremc
_EOF
exit
}
function _logme {
printf "%s: %s\n" "$PROG" "$*"
}
for dep in "${DEPS[@]}"; do
if ! command -v "$dep" &>/dev/null; then
_logme "'$dep' not found"
exit 1
fi
done
function _notify {
local icon="transmission"
local notify_args
local mesg="<b>$1</b>"
declare -a notify_args=(-r "888" -i "$icon" "${PROG:-"no-name"}")
notify-send "${notify_args[@]}" "$mesg"
}
function is_running {
if ! pgrep -f "$CMD" &>/dev/null; then
return 1
fi
return 0
}
function _add_torrent {
local torrent="$1"
if ! is_running; then
_logme "$CMD not running"
_usage
fi
if [[ "$torrent" =~ ^magnet: ]]; then
_add_magnet "$torrent"
return
fi
if [[ ! -e "$torrent" ]]; then
return
fi
local mesg
if tremc "$torrent"; then
mesg="torrent <i>$(basename "$torrent")</i> added"
else
mesg="torrent not added"
fi
_notify "$mesg"
return
}
function _add_magnet {
local magnet="$1"
local msg="torrent added"
if [[ -z "$magnet" ]]; then
_logme "you must provide a magnet link"
exit 1
fi
tm-add "$magnet" && notify-send "$msg"
_logme "$msg"
}
function _stop {
pkill -f "$CMD"
}
function _start {
setsid -f "$CMD"
}
function main {
local subcommand="${1:-}"
local magnet="${2:-$subcommand}"
case "$subcommand" in
-s | start) _start && echo "$PROG: $CMD started" ;;
-k | kill) _stop && echo "$PROG: stopped" ;;
-a | add) _add_torrent "$magnet" ;;
-r | restart) _stop && _start && _notify "$CMD restarted" ;;
-h | help) shift && _usage ;;
-x | tremc) tremc -X ;;
*) tremc -X ;;
esac
}
main "$@"