-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete.sh
263 lines (229 loc) · 6.25 KB
/
complete.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
#!/bin/bash
app="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
#source "$app/functions.sh"
#set -x
#
#Run external program on torrent finished
#Supported parameters (case sensitive):
# %N: Torrent name
# %L: Category
# %G: Tags (separated by comma)
# %F: Content path (same as root path for multifile torrent)
# %R: Root path (first torrent subdirectory path) [will be '' if only a single file torrent]
# %D: Save path
# %C: Number of files
# %Z: Torrent size (bytes)
# %T: Current tracker
# %I: Info hash v1
# %J: Info hash v2
# %K: Torrent ID
#Tip: Encapsulate parameter with quotation marks to avoid text being cut off at whitespace (e.g., "%N")
#
###########
#
# Variables
#
###########
# Logs
DEST="/downloads/complete"
LOG_DATE=$(date +"%Y-%m-%d")
LOG_DIR='/scripts'
LOG_EXT='.log'
LOG_FILE="$LOG_DIR/$LOG_DATE$LOG_EXT"
LOG_DAYS_TO_KEEP=3 # NOTE: It's technically N+1
# All possible parameters that can be passed from qBittorrent
TOR_NAME=""
CATEGORY=""
TAGS=""
CONTENT_PATH=""
ROOT_PATH=""
SAVE_PATH=""
NUM_FILES=0
TOR_SIZE=0
CURR_TRACKER=""
HASH_V1=""
HASH_V2=""
TORR_ID=0
# Parse the flags/parameters that were passed from qBittorren
while getopts ":n:l:g:f:r:d:c:z:t:i:j:k:" flag; do
case "${flag}" in
n)
TOR_NAME="$OPTARG"
;;
l)
CATEGORY="$OPTARG"
;;
g)
TAGS="$OPTARG"
;;
f)
CONTENT_PATH="$OPTARG"
;;
r)
ROOT_PATH="$OPTARG"
;;
d)
SAVE_PATH="$OPTARG"
;;
c)
NUM_FILES="$OPTARG"
;;
z)
TOR_SIZE="$OPTARG"
;;
t)
CURR_TRACKER="$OPTARG"
;;
i)
HASH_V1="$OPTARG"
;;
j)
HASH_V2="$OPTARG"
;;
k)
TORR_ID="$OPTARG"
;;
*)
echo "Usage: [script_file] [-f \"%F\"] [-d \"%D\"]..."
exit 1
;;
esac
done
###########
#
# Functions
#
###########
function PrepLogging
{
# The following will spit out all processes to the log
exec 3>&1 1>>"$LOG_FILE" 2>&1
trap "echo 'ERROR: An error occurred during execution, check log $LOG_FILE for details.' >&3" ERR
trap '{ set +x; } 2>/dev/null; echo -n "[$(date -Is)] "; set -x' DEBUG
}
function PrepFolders
{
echo "Prepping folder '${1}'..."
# Make if it doesn't exist
if [[ ! -e "${1}" ]]; then
mkdir "${1}"
SetPerms "${1}"
# Else, verify it's actually a directory
elif [[ ! -d "${1}" ]]; then
echo "'$1' already exists but is not a directory."
exit 2
fi
echo "Folder prep complete."
}
function PrepLogFile
{
echo "Prepping log file '$LOG_FILE'..."
# Ensure log file exists
if [[ ! -e "$LOG_FILE" ]]; then
touch "$LOG_FILE"
echo "Created '$LOG_FILE'."
fi
# Ensure log file is writable
if [ ! -w "$LOG_FILE" ] ; then
exit 1
fi
echo "Log file ready."
}
function PurgeLogFiles
{
echo "Purging old log file(s)..."
if [[ -z "$LOG_DAYS_TO_KEEP" ]]; then
echo "INFO: 'LOG_DAYS_TO_KEEP' variable not set. No clean-up performed."
else
if [[ "$LOG_DAYS_TO_KEEP" -gt "0" ]]; then
# Remove old log files
# NOTE: Some instances may not work with the 'daystart' parameter
#find "$LOG_DIR" -maxdepth 1 -daystart -type f -iname "*$LOG_EXT" -mtime +$LOG_DAYS_TO_KEEP -exec rm -v '{}' +
find "$LOG_DIR" -maxdepth 1 -type f -iname "*$LOG_EXT" -mtime +$LOG_DAYS_TO_KEEP -exec rm -v '{}' +
echo "Purge complete."
else
echo "WARNING: 'LOG_DAYS_TO_KEEP' must be greater than 0 to perform any work."
fi
fi
}
function DebugToLog
{
# Echo the variables passed in via the app
echo "Torrent: '$TOR_NAME'"
echo " Category : $CATEGORY"
echo " Content Path: $CONTENT_PATH"
echo " Curr Tracker: $CURR_TRACKER"
echo " Identifier : $TORR_ID"
echo " Info Hash v1: $HASH_V1"
echo " Info Hash v2: $HASH_V2"
echo " Number Files: $NUM_FILES"
echo " Root Path : $ROOT_PATH"
echo " Size : $TOR_SIZE"
echo " Save Path : $SAVE_PATH"
echo " Tags : $TAGS"
}
function CopyFiles
{
echo "Copying torrent..."
local DEST_PATH="$DEST"
# Add 'Category' to path, if passed in
if [[ "${CATEGORY}" != "" ]]; then
DEST_PATH="${DEST}/${CATEGORY}"
fi
PrepFolders "$DEST_PATH"
# Copy the folder/file(s)
if [[ "$ROOT_PATH" != "" ]]; then
cp -R "$ROOT_PATH" "$DEST_PATH"
FINAL_DEST="$DEST_PATH/${ROOT_PATH##*/}"
else
# This should be single file TORRENTS
cp "$CONTENT_PATH" "$DEST_PATH"
FINAL_DEST="$DEST_PATH/${CONTENT_PATH##*/}"
fi
echo "Copying complete."
}
function SetPerms
{
chown -R abc:abc "${1}"
chmod -R 777 "${1}"
}
#######
#
# Setup
#
#######
PrepLogging
PrepFolders "$LOG_DIR"
PrepLogFile
PurgeLogFiles
# (UN)Comment as desired
DebugToLog
######
#
# Work
#
######
# Copy files to COMPLETE folder
CopyFiles
# Update permissions
SetPerms "$FINAL_DEST"
echo -e 'Done.\n\n'
#
# 08-12-2023 v01.00 - Initial version
# 08-12-2023 v01.50 - Added handling of 'Category'
# Run external program on torrent finished:
# '/bin/bash /scripts/complete.sh %Z %C "%N" "%D" "%F" "%L" "%R"'
# 08-12-2023 v01.70 - Added logic to delete old log files
# 08-12-2023 v01.75 - Added output during delete old log files
# 08-12-2023 v01.80 - Variables in log clean-up added
# 08-13-2023 v01.82 - Removed unnecessary redirects and cleaed-up LOG logic
# 08-13-2023 v02.00 - Use and parse FLAGS to handle null/empty parameters
# (order of parameter won't matter)
# Run external program on torrent finished:
# '/bin/bash /scripts/complete.sh -n "%N" -l "%L" -f "%F" -r "%R" -c "%D" -c %C -z %Z -i "%I" -j "%J" -k "%K"'
# 08-26-2023 v01.85 - Logging to functions & fixed purge logic
# 08-26-2023 v01.86 - Fixed & tested logic in 'RotateLogFiles'
# 08-26-2023 v01.90 - Logic flow corrections
# 08-27-2023 v01.95 - Refactored & created 'FinalDestinationValidation' function
# 08-27-2023 v01.96 - Refactored to 'PrepFolders' & better logging
#