summarylogtreecommitdiffstats
path: root/rtorrentctl
blob: 6d4b680966869138cf56bfbf18b9fdb4799932cc (plain)
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
#!/bin/bash

set -e

# systemd integration for rtorrent
#
# Starts and stops instances of rtorrent inside screen sessions.
# Utilized by rtorrent@.service systemd service.
# This process does not fork after starting rtorrent/screen.
#
# Instances are rtorrent.rc files named ${INSTANCE}${RC_EXT} and
# directive files named ${INSTANCE}${DIRECTIVE_EXT}. Directive
# files are sourced BASH fragments that set known elements in an
# associated array named 'CONFIG'. Instances live in ${CONFIG_DIR}.
#
# Supported Directives:
# user=... Sets the run-as user for the rtorrent and screen programs
# dir=...  Sets the working directory of the rtorrent and screen programs
#          Defaults to the current working directory
#
# Example Directive File:
# CONFIG+=([user]="username")
# CONFIG+=([dir]="/torrents")
#
# Return codes:
# 1 - Working directory doesn't exist
# 2 - Invalid action
# 3 - Missing instance argument
# 4 - Incorrect number of arguments
# 5 - Instance rtorrent.rc file missing
# 6 - Instance directive file missing

# Directory containing .rtorrent.rc files for each rtorrent instance
CONFIG_DIR="/etc/rtorrent.d"
# Suffix of instance rtorrent.rc files
RC_EXT=".rtorrent.rc"
# Suffic of instance directive files
DIRECTIVE_EXT=".config"
# Suffix added to instance name and used as the screen session name
SESSION_SUFFIX="-rtorrent"

# Prints the script usage
function usage() {
  echo "Usage: rtorrentctl {start|stop} instance" >&2
  echo "'instance' names prefix of ${RC_EXT} and ${DIRECTIVE_EXT} in ${CONFIG_DIR}" >&2
}

# Produces the rtorrent.rc file path from the instance name 
function rcFile() {
  echo "${CONFIG_DIR}/${1}${RC_EXT}"
}

# Produces the directive file path from the instance name
function configFile() {
  echo "${CONFIG_DIR}/${1}${DIRECTIVE_EXT}"
}

# Sets the working directory to the default if not specified
function defaultWorkingDir() {
  if [[ "${CONFIG[dir]}" == "" ]]
  then
    CONFIG[dir]="$(pwd)"
  fi
}

# Produces the screen session name for an instance
function sessionName() {
  echo "${1}${SESSION_SUFFIX}"
}

# Starts an instance
function start() {
  dir="${CONFIG[dir]}"
  user="${CONFIG[user]}"
  sessionName="${CONFIG[sessionName]}"
  rcFile="${CONFIG[rcFile]}"

  # Change to the desired working directory
  cd "${dir}"

  # Exec screen as user
  # "-u ..."        User to exec screen as
  # "-D -m"         Start detached and don't fork process
  # "-fn"           Disable software flow control (CTRL+Q,CTRL+S)
  # "-S ..."        Session name
  # "-n"            Tell rtorrent to not read ~/.rtorrent.rc
  # "-o import=..." Tell rtorrent to read and use this file as configuration
  sudo -u "${user}" screen -D -m -fn -S "${sessionName}"  rtorrent -n -o import="${rcFile}"
}

# Stops an instance
function stop() {
  instance="${CONFIG[instance]}"
  user="${CONFIG[user]}"
  sessionName="${CONFIG[sessionName]}"

  # Signal screen to send "quit" keystroke (CTRL+Q) to rtorrent
  # "-u ..." User to send signal as
  # "-S ..." Session name
  # "-p ..." Screen number within session
  # "-X xon" Send 'xon' command, which means send 'CTRL+Q'
  sudo -u "${user}" screen -S "${sessionName}" -p 0 -X xon
}

if [[ "${1}" != "start" ]] && [[ "${1}" != "stop" ]]
then
  echo "Valid actions are 'start' and 'stop'" >&2
  usage
  exit 2
fi

if [[ "${2}" == "" ]] 
then
  echo "Must specify an instance" >&2
  usage
  exit 3
fi

if [[ $# -ne 2 ]]
then
  echo "Wrong number of arguments" >&2
  usage
  exit 4
fi

ACTION="${1}"
INSTANCE="${2}"

RCFILE=$(rcFile "${INSTANCE}")
if [[ ! -e "${RCFILE}" ]]
then
  echo "Instance rtorrent.rc doesn't exist. Check for ${RCFILE}" >&2
  exit 5
fi

CONFIGFILE=$(configFile "${INSTANCE}")
if [[ ! -e "${CONFIGFILE}" ]]
then
  echo "Instance configuration directive file doesn't exist. Check for ${CONFIGFILE}" >&2
  exit 6
fi

# Load the configuration directives from the config file
declare -A CONFIG
CONFIG[instance]="${INSTANCE}"
CONFIG[action]="${ACTION}"
CONFIG[sessionName]="$(sessionName "${INSTANCE}")"
CONFIG[rcFile]="${RCFILE}"
source ${CONFIGFILE}

# Set the default directory if none was set
defaultWorkingDir

if [[ "${CONFIG[user]}" == "" ]]
then
  echo "User directive missing from ${CONFIGFILE}" >&2
  exit 7
fi

if [[ "${ACTION}" == "start" ]]
then

  start

elif [[ "${ACTION}" == "stop" ]]
then

  stop

fi