-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrents-from-raspi
executable file
·65 lines (49 loc) · 1.66 KB
/
torrents-from-raspi
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
#!/usr/bin/bash
LOCAL_TORRENTS_DIR="/data/torrents/"
RASPI_TORRENTS="/mnt/alarmpi/1.5tb/raspi/torrents/*$1*"
TEMP_DIRECTORY_NAME="temp"
MV_CMD_NAMES=('MV' 'RSYNC')
echo Connecting to Samba shares
samba-connect
# case-insensitive bash-wildcard-expansion
shopt -s nocaseglob
find $RASPI_TORRENTS -maxdepth 0 ! -name "$TEMP_DIRECTORY_NAME" -exec basename {} \;
echo ""
# get number of matching files, excluding temp (if it matches)
FILE_COUNT=$(ls -ld $RASPI_TORRENTS | wc -l)
# shellcheck disable=SC2086
# (we intend to expand the glob here)
if echo $RASPI_TORRENTS | grep "$TEMP_DIRECTORY_NAME" -q ; then
((FILE_COUNT--))
fi
echo "Found $FILE_COUNT files that match and could be transferred..."
for torrent in $RASPI_TORRENTS;
do
file=$(basename "$torrent")
if [ "$file" == "$TEMP_DIRECTORY_NAME" ]; then
continue
fi
# determine whether or not to use rsync:
# - use mv if the torrent is a directory and has subdirectories
# - use rsync if the torrent has no subdirectories (or is no directory)
DIRECTORY_COUNT=$(find "$torrent" -type d | wc -l)
[ "$DIRECTORY_COUNT" -gt 1 ]
mv_cmd_index=$?
echo "Use ${MV_CMD_NAMES[$mv_cmd_index]} to move \`$file\`?"
read -r answer
answer=${answer,,}
if [ "$answer" == "y" ] || [ "$answer" == "yes" ]; then
echo "Moving $torrent to $LOCAL_TORRENTS_DIR..."
if [ $mv_cmd_index -eq 0 ]; then
mv "$torrent" $LOCAL_TORRENTS_DIR
else
rsync -avzz --progress --remove-source-files "$torrent" $LOCAL_TORRENTS_DIR
[ "$DIRECTORY_COUNT" -eq 1 ] && rmdir "$torrent"
fi
echo "... done!"
fi
done
echo ""
echo "... done processing files!"
echo Disconnecting from Samba shares
samba-disconnect