-
Notifications
You must be signed in to change notification settings - Fork 17
/
rclone_script.sh
369 lines (309 loc) · 9.07 KB
/
rclone_script.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/bash
# define colors for output
NORMAL=$(tput sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
UNDERLINE=$(tput smul)
# include settings file
config=~/scripts/rclone_script/rclone_script.ini
source ${config}
logLevel=2
# parameters
direction="$1"
system="$2"
emulator="$3"
rom="$4"
command="$5"
####################
# HELPER FUNCTIONS #
####################
function log ()
# Prints messages of different severeties to a logfile
# Each message will look something like this:
# <TIMESTAMP> <SEVERITY> <CALLING_FUNCTION> <MESSAGE>
# needs a set variable $logLevel
# -1 > No logging at all
# 0 > prints ERRORS only
# 1 > prints ERRORS and WARNINGS
# 2 > prints ERRORS, WARNINGS and INFO
# 3 > prints ERRORS, WARNINGS, INFO and DEBUGGING
# needs a set variable $log pointing to a file
# Usage
# log 0 "This is an ERROR Message"
# log 1 "This is a WARNING"
# log 2 "This is just an INFO"
# log 3 "This is a DEBUG message"
{
severity=$1
message=$2
if (( ${severity} <= ${logLevel} ))
then
case ${severity} in
0) level="ERROR" ;;
1) level="WARNING" ;;
2) level="INFO" ;;
3) level="DEBUG" ;;
esac
printf "$(date +%FT%T%:z):\t${level}\t${0##*/}\t${FUNCNAME[1]}\t${message}\n" >> ${logfile}
fi
}
function killOtherNotification ()
{
# get PID of other PNGVIEW process
otherPID=$(pgrep --full pngview)
if [ "${debug}" = "1" ]; then log 3 "Other PIDs: ${otherPID}"; fi
if [ "${otherPID}" != "" ]
then
if [ "${debug}" = "1" ]; then log 3 "Kill other PNGVIEW ${otherPID}"; fi
kill ${otherPID}
fi
}
function showNotification ()
{
# Quit here, if Notifications are not to be shown and they are not forced
if [ "${showNotifications}" == "FALSE" ] && [ "$6" != "forced" ]
then
return
fi
message="$1"
if [ "$2" = "" ]
then
color="yelloW"
else
color="$2"
fi
if [ "$3" = "" ]
then
timeout="10000"
else
timeout="$3"
fi
if [ "$4" = "" ]
then
posx="10"
else
posx="$4"
fi
if [ "$5" = "" ]
then
posy="10"
else
posy="$5"
fi
# create PNG using IMAGEMAGICK
convert -size 1500x32 xc:"rgba(0,0,0,0)" -type truecolormatte -gravity NorthWest \
-pointsize 32 -font FreeMono -style italic \
-fill ${color} -draw "text 0,0 '${message}'" \
PNG32:- > ~/scripts/rclone_script/rclone_script-notification.png
killOtherNotification
# show PNG using PNGVIEW
nohup pngview -b 0 -l 10000 ~/scripts/rclone_script/rclone_script-notification.png -x ${posx} -y ${posy} -t ${timeout} &>/dev/null &
}
function getROMFileName ()
{
rompath="${rom%/*}" # directory containing $rom
romfilename="${rom##*/}" # filename of $rom, including extension
romfilebase="${romfilename%%.*}" # filename of $rom, excluding extension
romfileext="${romfilename#*.}" # extension of $rom
}
function prepareFilter ()
{
filter="${romfilebase//\[/\\[}"
filter="${filter//\]/\\]}"
}
function getTypeOfRemote ()
{
# list all remotes and their type
remotes=$(rclone listremotes -l)
# get line with RETROPIE remote
retval=$(grep -i "^retropie:" <<< ${remotes})
remoteType="${retval#*:}"
remoteType=$(echo ${remoteType} | xargs)
}
function getAvailableConnection ()
# checks if the device is connected to a LAN / WLAN and the Internet
# RETURN
# 0 > device seems to be connected to the Internet
# 1 > device seems to be connected to a LAN / WLAN without internet access
# 2 > device doesn't seem to be connected at all
{
gatewayIP=$(ip r | grep default | cut -d " " -f 3)
if [ "${gatewayIP}" == "" ]
then
log 2 "Gateway could not be detected"
return 2
else
log 2 "Gateway IP: ${gatewayIP}"
fi
ping -q -w 1 -c 1 ${gatewayIP} > /dev/null
if [[ $? -eq 0 ]]
then
log 2 "Gateway PING successful"
else
log 2 "Gateway could not be PINGed"
return 2
fi
ping -q -w 1 -c 1 "8.8.8.8" > /dev/null
if [[ $? -eq 0 ]]
then
log 2 "8.8.8.8 PING successful"
return 0
else
log 2 "8.8.8.8 could not be PINGed"
return 1
fi
}
##################
# SYNC FUNCTIONS #
##################
function downloadSaves ()
{
if [ "${syncOnStartStop}" == "FALSE" ]
then
showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
return
fi
log 2 "Started ${system}/${romfilename} "
log 2 "Downloading saves and states for ${system}/${romfilename} from ${remoteType}..."
showNotification "Downloading saves and states from ${remoteType}..."
getAvailableConnection
availableConnection=$?
if [[ ${availableConnection} -gt ${neededConnection} ]]
then
log 0 "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
case ${neededConnection} in
0) showNotification "Downloading saves and states from ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
1) showNotification "Downloading saves and states from ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
esac
return
fi
# test for remote files
remotefiles=$(rclone lsf retropie:${remotebasedir}/${system} --include "${filter}.*")
retval=$?
if [ "${retval}" = "0" ]
then # no error with RCLONE
if [ "${remotefiles}" = "" ]
then # no remote files found
log 2 "No remote files found"
showNotification "Downloading saves and states from ${remoteType}... No remote files found"
else # remote files found
log 2 "Found remote files"
# download saves and states to corresponding ROM
rclone copy retropie:${remotebasedir}/${system} ~/RetroPie/saves/${system} --include "${filter}.*" --update >> ${logfile}
retval=$?
if [ "${retval}" = "0" ]
then
log 2 "Done"
showNotification "Downloading saves and states from ${remoteType}... Done" "green"
else
log 2 "Saves and states could not be downloaded"
showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
fi
fi
else # error with RCLONE
log 0 "Saves and states could not be downloaded"
showNotification "Downloading saves and states from ${remoteType}... ERROR" "red" "" "" "" "forced"
fi
}
function uploadSaves ()
{
if [ "${syncOnStartStop}" == "FALSE" ]
then
showNotification "!!! Synchronization is currently disabled !!!" "red" "" "" "" "forced"
return
fi
log 2 "Stopped ${system}/${romfilename} "
log 2 "Uploading saves and states for ${system}/${romfilename} to ${remoteType}..."
showNotification "Uploading saves and states to ${remoteType}..."
getAvailableConnection
availableConnection=$?
if [[ ${availableConnection} -gt ${neededConnection} ]]
then
log 0 "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
case ${neededConnection} in
0) showNotification "Uploading saves and states to ${remoteType}... No Internet connection available" "red" "" "" "" "forced" ;;
1) showNotification "Uploading saves and states to ${remoteType}... No LAN / WLAN connection available" "red" "" "" "" "forced" ;;
esac
return
fi
localfiles=$(find ~/RetroPie/saves/${system} -type f -iname "${filter}.*")
if [ "${localfiles}" = "" ]
then # no local files found
log 2 "No local saves and states found"
showNotification "Uploading saves and states to ${remoteType}... No local files found"
else # local files found
# upload saves and states to corresponding ROM
rclone copy ~/RetroPie/saves/${system} retropie:${remotebasedir}/${system} --include "${filter}.*" --update >> ${logfile}
retval=$?
if [ "${retval}" = "0" ]
then
log 2 "Done"
showNotification "Uploading saves and states to ${remoteType}... Done" "green"
else
log 2 "saves and states could not be uploaded"
showNotification "Uploading saves and states to ${remoteType}... ERROR" "red" "" "" "" "forced"
fi
fi
}
function deleteFileFromRemote ()
# deletes a file from the remote
# INPUT
# $1 > relative filepath incl. name and extension to the local savepath
# RETURN
# 0 > file deteted successfully
# 1 > connection not available
# 2 > file could not be deleted
{
fileToDelete="$1"
log 2 "File to delete: retropie:${remotebasedir}/${fileToDelete}"
getAvailableConnection
availableConnection=$?
if [[ ${availableConnection} -gt ${neededConnection} ]]
then
log 0 "Needed Connection not available. Needed ${neededConnection}, available ${availableConnection}"
return 1
fi
rclone delete "retropie:${remotebasedir}/${fileToDelete}" 2>&1 >> ${logfile}
if [[ $? -eq 0 ]]
then
log 2 "File deleted successfully"
return 0
else
log 0 "File could not be deleted. Error Code $?"
return 1
fi
}
########
# MAIN #
########
#if [ "${debug}" = "1" ]; then debug; fi
log 3 "direction: ${direction}"
log 3 "system: ${system}"
log 3 "emulator: ${emulator}"
log 3 "rom: ${rom}"
log 3 "command: ${command}"
log 3 "remotebasedir: ${remotebasedir}"
log 3 "rompath: ${rompath}"
log 3 "romfilename: ${romfilename}"
log 3 "romfilebase: ${romfilebase}"
log 3 "romfileext: ${romfileext}"
if [ "${direction}" == "up" ] && [ "${system}" != "kodi" ]
then
getROMFileName
prepareFilter
getTypeOfRemote
uploadSaves
fi
if [ "${direction}" == "down" ] && [ "${system}" != "kodi" ]
then
getROMFileName
prepareFilter
getTypeOfRemote
downloadSaves
fi
if [ "${direction}" == "delete" ]
then
deleteFileFromRemote "${2}"
fi