-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctorrent-mv
executable file
·69 lines (53 loc) · 1.55 KB
/
ctorrent-mv
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
#!/bin/bash
first=$(ps -eo ppid,pid | awk -v me=$$ '$1 == me { e=$2 } END { print e }')
torrents=$1
move_to=${2:-/R/cinema}
function torr_dir() {
ctorrent -x "$1" | awk '$1 == "Directory:" { sub("Directory: ", "", $0); print ; exit} '
}
function torr_file() {
ctorrent -x "$1" | awk '$1 == "<1>" {
sub("<1> ", "", $0);
sub(/ \[[0-9]+\]$/, "", $0);
print ; exit} '
}
if ! [[ -d $torrents ]]; then
echo \$1 is $1: not a dir
exit 1
fi
move_to_out=/tmp/ctorrent.move-to
find "$move_to" -type d > $move_to_out.d
find "$move_to" -type f > $move_to_out.f
find "$torrents" -name '*.torrent' | while read torr_file; do
dl_dir=`torr_dir "$torr_file"`
dl_file=`torr_file "$torr_file"`
if [[ -n $dl_dir ]]; then
found=$(grep -F "$dl_dir" -m1 $move_to_out.d)
if [[ -d $found ]]; then
from_dir="$(dirname "$torr_file")"
to_dir="$(dirname "$found")"
if [[ $from_dir != $to_dir ]]; then
echo mv -v "$torr_file" "$to_dir"
fi
else
#echo "not dir for: $torr_file"
echo "not dir: <$found> {$dl_dir} ($torr_file)"
fi
elif [[ -n $dl_file ]]; then
dl_file_g=$(echo "$dl_file" | sed 's/[[:punct:]\$\^]/\\&/g')
found=$(grep -e "$dl_file_g$" $move_to_out.f)
if [[ -f $found ]]; then
from_dir="$(dirname "$torr_file")"
to_dir="$(dirname "$found")"
if [[ $from_dir != $to_dir ]]; then
echo mv -v "$torr_file" "$to_dir" '#f'
fi
else
echo "not file: <$found> {$dl_file} ($torr_file)"
fi
else
echo "?? $torr_file"
fi
done
last=$(ps -eo ppid,pid | awk -v me=$$ '$1 == me { e=$2 } END { print e }')
echo F $first L $last