-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmission_download-completed.sh
executable file
·87 lines (80 loc) · 2.38 KB
/
transmission_download-completed.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
#!/bin/bash
#
# Script that does things after torrent download is completed.
# https://github.com/transmission/transmission/blob/main/docs/Scripts.md
#
# TODO:
# - Convert DEBUG/TEST to use `--debug` and `--test` (getopts)
#
ISOFILES_DIR="$HOME/Downloads/ISOs"
LOG_FILE="$HOME/Downloads/torrents/download-completed.log"
if [[ "$1" == "TEST" ]]; then
TORRENTS_DIR='/home/mclang/Downloads/torrents/completed/'
# TORRENT_NAME='kali-linux-2024.2-installer-amd64.iso'
# TORRENT_NAME='Fedora-Sway-Live-x86_64-39'
TORRENT_NAME='tails-amd64-6.0-img'
else
TORRENTS_DIR="$TR_TORRENT_DIR"
TORRENT_NAME="$TR_TORRENT_NAME"
fi
if [[ -d "$TORRENTS_DIR/$TORRENT_NAME" ]]; then
TORRENT_TYPE="DIR"
elif [[ -e "$TORRENTS_DIR/$TORRENT_NAME" ]]; then
TORRENT_TYPE="${TORRENT_NAME##*.}"
else
TORRENT_TYPE="n/a"
fi
if [[ "$1" == "TEST" || "$1" == "DEBUG" ]]; then
cat <<- EOT >> "$LOG_FILE"
$(date +'%F %T')
Target ISO dir: '$ISOFILES_DIR'
Torrent name: '$TORRENT_NAME'
Torrent type: '$TORRENT_TYPE'
Torrent path: '$TORRENTS_DIR/$TORRENT_NAME'
$(env | grep 'TR_')
EOT
fi
# NOTE: These need to be set AFTER the above definitions, otherwise `e`
# with `pipefail` might hide errors in the sub shell commands!
set -euo pipefail
# Validity checks:
ERRORS=()
if [[ -z "$TORRENTS_DIR" ]]; then
ERRORS+=("Variable 'TORRENTS_DIR' is empty!")
elif [[ ! -d "$TORRENTS_DIR" ]]; then
ERRORS+=("Torrents dir '$TORRENTS_DIR' does not exist!")
fi
if [[ ! -d "$ISOFILES_DIR" ]]; then
ERRORS+=("Target ISO dir '$ISOFILES_DIR' does not exist!")
fi
if [[ ! -e "$TORRENTS_DIR/$TORRENT_NAME" ]]; then
ERRORS+=("Torrent '$TORRENTS_DIR/$TORRENT_NAME' does not exist!")
fi
{
if (( ${#ERRORS[@]} > 0 )); then
echo "$(date +'%F %T') :: UNRECOVABLE ERRORS:"
for err in "${ERRORS[@]}"; do
echo "- $err"
done
echo ""
exit 1
fi
} | tee -a "$LOG_FILE"
# Do things as needed according to lower case file extension:
{
echo "$(date +'%F %T') :: Downloading '$TORRENT_NAME' completed"
case "${TORRENT_TYPE,,}" in
'dir')
echo "==> Torrent is a directory - linking IMG/ISO files using 'find'"
find "$TORRENTS_DIR/$TORRENT_NAME" \( -iname '*.img' -o -iname '*.iso' \) -exec ln "{}" "$ISOFILES_DIR/" \;
;;
'img') ;&
'iso')
echo "==> Hard-linking into '$ISOFILES_DIR/' directory"
ln "$TORRENTS_DIR/$TORRENT_NAME" "$ISOFILES_DIR/"
;;
*)
echo "==> No post download logic specified"
;;
esac
} >> "$LOG_FILE" 2>&1