-
Notifications
You must be signed in to change notification settings - Fork 0
/
shipment.sh
executable file
·121 lines (104 loc) · 3.01 KB
/
shipment.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
NUM_TRIES=1
SLEEP_TIME=10
LOCAL_DESTINATION=~/"Downloads/Shipment"
LOCAL_RUNTIME=~/"dev/proj/shipment/"
LOCAL_WATCH=~/"Downloads/watch"
REMOTE_USER="blahsd"
REMOTE_HOST="star.seedhost.eu"
REMOTE_SOURCE="/home19/blahsd/downloads/completed"
REMOTE_WATCH="/home19/blahsd/downloads/watch"
verbose='true'
wetRun='true'
updated='false'
cleanup='false'
checkDeliveries='false'
onlycheck='false'
help='false'
while getopts 'vwcdho' flag; do
case "${flag}" in
w) wetRun='false' ;; # Debug mode. Saves no modification to the file lists.
v) verbose='true' ;; #
c) cleanup='true' ;;
d) checkDeliveries='true' ;; # Prints out local deliveries
h) help='true' ;;
o) onlycheck='true' ;;
esac
done
function checkForShipment {
#list all the remote files in listStocked
ssh $REMOTE_USER@$REMOTE_HOST ls $REMOTE_SOURCE > $LOCAL_RUNTIME/listStocked
IFS=$'\n' #make newlines the only separator
for f in $(cat $LOCAL_RUNTIME/listStocked) #for each file in listStocked, check if it's in listShipped
do
if grep -Fqx $f $LOCAL_RUNTIME/listShipped
then
:
else
if $verbose
then
echo "Payload found $f. Transferring..."
fi
printf -v sanitizedFilename "%q\n" $f #sanitize input
scp -r $REMOTE_USER@$REMOTE_HOST:$REMOTE_SOURCE/"$sanitizedFilename" $LOCAL_DESTINATION
if $wetRun
then
echo "$f" >> $LOCAL_RUNTIME/listShipped
fi
updated='true'
fi
done
if $updated
then
:
else
echo "No new payload found."
fi
}
if $help
then
echo """
-w wetRun makes no modification to the files lists
-c cleanup cleans up the local delivered payloads
-d deliveries prints the local delivered payloads
-h help prints this help page
-o onlycheck doesn't make downloads
"""
exit
fi
if $checkDeliveries || $onlycheck
then
echo "Checking for locally delivered payloads..."
tree $LOCAL_DESTINATION || ls $LOCAL_DESTINATION
fi
if $cleanup
then
read -p "Clean up local Shipment/? This will delete everything. [y/N]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Cleaning up local Shipment/ ..."
rm -rf $LOCAL_DESTINATION/*
echo "Local Shipment/ cleaned up."
fi
fi
if $onlycheck
then
exit
fi
# /dev/null is a receptacle for unwanted output. It does nothing with it.
scp -r $LOCAL_WATCH/*.torrent $REMOTE_USER@$REMOTE_HOST:$REMOTE_WATCH > /dev/null 2>&1
echo "Local watch/ uploaded to remote."
rm $LOCAL_WATCH/*.torrent > /dev/null 2>&1
echo "Local watch/ cleaned up."
if $updated
then
exit
fi
if $verbose
then
echo "Querying remote server for payload. Checking..."
fi
checkForShipment
#I know, this is dirty. I mean, dirtier than all the rest. But it works.
find $LOCAL_DESTINATION -name "*.flac" -exec ffmpeg -y -i {} -codec:a libmp3lame -q:a 0 -map_metadata 0 -id3v2_version 3 -write_id3v1 1 {}.mp3 \;
find $LOCAL_DESTINATION -name '*.mp3' -exec mv {} ~/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ iTunes.localized \;