forked from Jandalf81/rclone_script
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rclone_script-install.sh
1179 lines (969 loc) · 32.4 KB
/
rclone_script-install.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# define colors for output
NORMAL="\Zn"
BLACK="\Z0"
RED="\Z1"
GREEN="\Z2"
YELLOW="\Z3\Zb"
BLUE="\Z4"
MAGENTA="\Z5"
CYAN="\Z6"
WHITE="\Z7"
BOLD="\Zb"
REVERSE="\Zr"
UNDERLINE="\Zu"
# global variables
url="https://raw.githubusercontent.com/Dannflower/rclone_script"
branch="master"
# configuration variables
remotebasedir=""
shownotifications=""
backtitle="RCLONE_SCRIPT installer (https://github.com/Jandalf81/rclone_script)"
logfile=~/scripts/rclone_script/rclone_script-install.log
##################
# WELCOME DIALOG #
##################
dialog \
--backtitle "${backtitle}" \
--title "Welcome" \
--colors \
--no-collapse \
--cr-wrap \
--yesno \
"\nThis script will configure RetroPie so that your savefiles and statefiles will be ${YELLOW}synchronized with a remote destination${NORMAL}. Several packages and scripts will be installed, see\n\n https://github.com/Jandalf81/rclone_script/blob/master/ReadMe.md\n\nfor a rundown. In short, any time you ${GREEN}start${NORMAL} or ${RED}stop${NORMAL} a ROM the savefiles and savestates for that ROM will be ${GREEN}down-${NORMAL} and ${RED}uploaded${NORMAL} ${GREEN}from${NORMAL} and ${RED}to${NORMAL} a remote destination. To do so, RetroPie will be configured to put all savefiles and statefiles in distinct directories, seperated from the ROMS directories. This installer will guide you through the necessary steps. If you wish to see what exactly is done at each step, open a second console and execute\n ${YELLOW}tail -f ~/scripts/rclone_script/rclone_script-install.log${NORMAL}\n\nIf you already have some savefiles in the ROMS directory, you will need to ${YELLOW}move them manually${NORMAL} after installation. You can use the new network share\n ${YELLOW}\\\\$(hostname)\\saves${NORMAL}\nfor this.\n\nAre you sure you wish to continue?" \
26 90 2>&1 > /dev/tty \
|| exit
####################
# DIALOG FUNCTIONS #
####################
# Warn the user if they are using the BETA branch
function dialogBetaWarning ()
{
dialog \
--backtitle "${backtitle}" \
--title "Beta Warning" \
--colors \
--no-collapse \
--cr-wrap \
--yesno \
"\n${RED}${UNDERLINE}WARNING!${NORMAL}\n\nYou are about to install a beta version!\nAre you ${RED}REALLY${NORMAL} sure you want to continue?" \
10 50 2>&1 > /dev/tty \
|| exit
}
# Build progress from array $STEPS()
# INPUT
# $steps()
# OUTPUT
# $progress
function buildProgress ()
{
progress=""
for ((i=0; i<=${#steps[*]}; i++))
do
progress="${progress}${steps[i]}\n"
done
}
# Show Progress dialog
# INPUT
# 1 > Percentage to show in dialog
# $backtitle
# $progress
function dialogShowProgress ()
{
local percent="$1"
buildProgress
clear
echo "${percent}" | dialog \
--stdout \
--colors \
--no-collapse \
--cr-wrap \
--backtitle "${backtitle}" \
--title "Installer" \
--gauge "${progress}" 36 90 0 \
2>&1 > /dev/tty
sleep 1
}
# Show summary dialog
function dialogShowSummary ()
{
# list all remotes and their type
remotes=$(rclone listremotes --long)
# get line with RETROPIE remote
retval=$(grep -i "^retropie:" <<< ${remotes})
remoteType="${retval#*:}"
remoteType=$(echo ${remoteType} | xargs)
dialog \
--backtitle "${backtitle}" \
--title "Summary" \
--colors \
--no-collapse \
--cr-wrap \
--yesno \
"\n${GREEN}All done!${NORMAL}\n\nFrom now on, all your saves and states will be synchronized each time you start or stop a ROM.\n\nAll systems will put their saves and states in\n Local: \"${YELLOW}~/RetroPie/saves/<SYSTEM>${NORMAL}\"\n Remote: \"${YELLOW}retropie:${remotebasedir}/<SYSTEM>${NORMAL}\" (${remoteType})\nIf you already have some saves in the ROM directories, you need to move them there manually now! You can use the new network share\n ${YELLOW}\\\\$(hostname)\\saves${NORMAL}\nfor this. Afterward, you should ${red}reboot${NORMAL} your RetroPie. Then, you should start a full sync via\n ${YELLOW}RetroPie / RCLONE_SCRIPT menu / 1 Full sync${NORMAL}\n\nStart\n ${YELLOW}RetroPie / RCLONE_SCRIPT menu / 9 uninstall${NORMAL}\nto revert all changes and remove this script.\n\nTo finish the installer you should reboot your RetroPie now.\n\n${RED}Reboot RetroPie now?${NORMAL}" \
28 90 2>&1 > /dev/tty
case $? in
0) sudo shutdown -r now ;;
esac
}
##################
# STEP FUNCTIONS #
##################
# Initialize array $STEPS()
# OUTPUT
# $steps()
function initSteps ()
{
steps[1]="1. RCLONE"
steps[2]=" 1a. Test for RCLONE binary [ waiting... ]"
steps[3]=" 1b. Download RCLONE binary [ waiting... ]"
steps[4]=" 1c. Test RCLONE remote [ waiting... ]"
steps[5]=" 1d. Create RCLONE remote [ waiting... ]"
steps[6]="2. PNGVIEW"
steps[7]=" 2a. Test for PNGVIEW binary [ waiting... ]"
steps[8]=" 2b. Download PNGVIEW source [ waiting... ]"
steps[9]=" 2c. Compile PNGVIEW [ waiting... ]"
steps[10]="3. IMAGEMAGICK"
steps[11]=" 3a. Test for IMAGEMAGICK [ waiting... ]"
steps[12]=" 3b. apt-get install IMAGEMAGICK [ waiting... ]"
steps[13]="4. RCLONE_SCRIPT"
steps[14]=" 4a. Download RCLONE_SCRIPT files [ waiting... ]"
steps[15]=" 4b. Create RCLONE_SCRIPT menu item [ waiting... ]"
steps[16]=" 4c. Configure RCLONE_SCRIPT [ waiting... ]"
steps[17]="5. RUNCOMMAND"
steps[18]=" 5a. Add call to RUNCOMMAND-ONSTART [ waiting... ]"
steps[19]=" 5b. Add call to RUNCOMMAND-ONEND [ waiting... ]"
steps[20]="6. Local SAVEFILE directory"
steps[21]=" 6a. Check local base directory [ waiting... ]"
steps[22]=" 6b. Check local <SYSTEM> directories [ waiting... ]"
steps[23]="7. Remote SAVEFILE directory"
steps[24]=" 7a. Check remote base directory [ waiting... ]"
steps[25]=" 7b. Check remote <SYSTEM> directories [ waiting... ]"
steps[26]="8. Configure RETROARCH"
steps[27]=" 8a. Set local SAVEFILE directories [ waiting... ]"
steps[28]="9. Finalizing"
steps[29]=" 9a. Save configuration [ waiting... ]"
}
# Update item of $STEPS() and show updated progress dialog
# INPUT
# 1 > Number of step to update
# 2 > New status for step
# 3 > Percentage to show in progress dialog
# $steps()
# OUTPUT
# $steps()
function updateStep ()
{
local step="$1"
local newStatus="$2"
local percent="$3"
local oldline
local newline
# translate and colorize $NEWSTATUS
case "${newStatus}" in
"waiting") newStatus="[ ${NORMAL}WAITING...${NORMAL} ]" ;;
"in progress") newStatus="[ ${NORMAL}IN PROGRESS${NORMAL} ]" ;;
"done") newStatus="[ ${GREEN}DONE${NORMAL} ]" ;;
"found") newStatus="[ ${GREEN}FOUND${NORMAL} ]" ;;
"not found") newStatus="[ ${RED}NOT FOUND${NORMAL} ]" ;;
"created") newStatus="[ ${GREEN}CREATED${NORMAL} ]" ;;
"failed") newStatus="[ ${RED}FAILED${NORMAL} ]" ;;
"skipped") newStatus="[ ${YELLOW}SKIPPED${NORMAL} ]" ;;
*) newStatus="[ ${RED}UNDEFINED${NORMAL} ]" ;;
esac
# search $STEP in $STEPS
for ((i=0; i<${#steps[*]}; i++))
do
if [[ ${steps[i]} =~ .*$step.* ]]
then
# update $STEP with $NEWSTATUS
oldline="${steps[i]}"
oldline="${oldline%%[*}"
newline="${oldline}${newStatus}"
steps[i]="${newline}"
break
fi
done
# show progress dialog
dialogShowProgress ${percent}
}
#######################
# INSTALLER FUNCTIONS #
#######################
# Installer
function installer ()
{
initSteps
dialogShowProgress 0
1RCLONE
2PNGVIEW
3IMAGEMAGICK
4RCLONE_SCRIPT
5RUNCOMMAND
6LocalSAVEFILEDirectory
7RemoteSAVEFILEDirectory
8ConfigureRETROARCH
9Finalize
dialogShowSummary
}
function 1RCLONE ()
{
# 1a. Testing for RCLONE binary
updateStep "1a" "in progress" 0
1aTestRCLONE
if [[ $? -eq 0 ]]
then
updateStep "1a" "found" 5
updateStep "1b" "skipped" 10
else
updateStep "1a" "not found" 5
# 1b. Getting RCLONE binary
updateStep "1b" "in progress" 5
1bInstallRCLONE
if [[ $? -eq 0 ]]
then
updateStep "1b" "done" 10
else
updateStep "1b" "failed" 5
exit
fi
fi
# 1c. Testing RCLONE configuration
updateStep "1c" "in progress" 10
1cTestRCLONEremote
if [[ $? -eq 0 ]]
then
updateStep "1c" "found" 15
updateStep "1d" "skipped" 20
else
updateStep "1c" "not found" 15
# 1d. Create RCLONE remote
updateStep "1d" "in progress" 15
1dCreateRCLONEremote
updateStep "1d" "done" 20
fi
}
# Checks if RCLONE is installed
# RETURN
# 0 > RCLONE is installed
# 1 > RCLONE is not installed
function 1aTestRCLONE ()
{
printf "$(date +%FT%T%:z):\t1aTestRCLONE\tSTART\n" >> "${logfile}"
if [ -f /usr/bin/rclone ]
then
printf "$(date +%FT%T%:z):\t1aTestRCLONE\tFOUND\n" >> "${logfile}"
return 0
else
printf "$(date +%FT%T%:z):\t1aTestRCLONE\tNOT FOUND\n" >> "${logfile}"
return 1
fi
}
# Installs RCLONE by download
# RETURN
# 0 > RCLONE has been installed
# 1 > Error while installing RCLONE
function 1bInstallRCLONE ()
{
printf "$(date +%FT%T%:z):\t1bInstallRCLONE\tSTART\n" >> "${logfile}"
# TODO get RCLONE for 64bit
{ # try
# get binary
wget -P ~ https://downloads.rclone.org/rclone-current-linux-arm.zip --append-output="${logfile}" &&
unzip ~/rclone-current-linux-arm.zip -d ~ >> "${logfile}" &&
cd ~/rclone-v* &&
# move binary
sudo mv rclone /usr/bin >> "${logfile}" &&
sudo chown root:root /usr/bin/rclone >> "${logfile}" &&
sudo chmod 755 /usr/bin/rclone >> "${logfile}" &&
cd ~ &&
# remove temp files
rm ~/rclone-current-linux-arm.zip >> "${logfile}" &&
rm -r ~/rclone-v* >> "${logfile}" &&
printf "$(date +%FT%T%:z):\t1bInstallRCLONE\tDONE\n" >> "${logfile}" &&
return 0
} || { #catch
printf "$(date +%FT%T%:z):\t1bInstallRCLONE\tERROR\n" >> "${logfile}" &&
# remove temp files
rm ~/rclone-current-linux-arm.zip >> "${logfile}" &&
rm -r ~/rclone-v* >> "${logfile}" &&
return 1
}
}
# Checks if there's a RCLONE remote called RETROPIE
# RETURN
# 0 > remote RETROPIE has been found
# 1 > no remote RETROPIE found
function 1cTestRCLONEremote ()
{
printf "$(date +%FT%T%:z):\t1cTestRCLONEremote\tSTART\n" >> "${logfile}"
local remotes=$(rclone listremotes)
local retval=$(grep -i "^retropie:" <<< ${remotes})
if [ "${retval}" == "retropie:" ]
then
printf "$(date +%FT%T%:z):\t1cTestRCLONEremote\tFOUND\n" >> "${logfile}"
return 0
else
printf "$(date +%FT%T%:z):\t1cTestRCLONEremote\tNOT FOUND\n" >> "${logfile}"
return 1
fi
}
# Tells the user to create a new RCLONE remote called RETROPIE
# RETURN
# 0 > remote RETROPIE has been created (no other OUTPUT possible)
function 1dCreateRCLONEremote ()
{
printf "$(date +%FT%T%:z):\t1dCreateRCLONEremote\tSTART\n" >> "${logfile}"
dialog \
--stdout \
--colors \
--no-collapse \
--cr-wrap \
--backtitle "${backtitle}" \
--title "Installer" \
--msgbox "\nPlease create a new remote within RCLONE now. Name that remote ${RED}retropie${NORMAL}. Please consult the RCLONE documentation for further information:\n https://www.rclone.org\n\nOpening RCLONE CONFIG now..." 20 50 \
2>&1 > /dev/tty
clear
rclone config
1cTestRCLONEremote
if [[ $? -eq 1 ]]
then
dialog \
--stdout \
--colors \
--no-collapse \
--cr-wrap \
--backtitle "${backtitle}" \
--title "Installer" \
--msgbox "\nNo remote ${RED}retropie${NORMAL} found.\nPlease try again." 20 50 \
2>&1 > /dev/tty
1dCreateRCLONEremote
else
printf "$(date +%FT%T%:z):\t1dCreateRCLONEremote\tFOUND\n" >> "${logfile}"
return 0
fi
}
function 2PNGVIEW ()
{
# 2a. Testing for PNGVIEW binary
updateStep "2a" "in progress" 20
2aTestPNGVIEW
if [[ $? -eq 0 ]]
then
updateStep "2a" "found" 25
updateStep "2b" "skipped" 30
updateStep "2c" "skipped" 35
else
updateStep "2a" "not found" 25
# 2b. Getting PNGVIEW source
updateStep "2b" "in progress" 25
2bGetPNGVIEWsource
if [[ $? -eq 0 ]]
then
updateStep "2b" "done" 30
# 2c. Compiling PNGVIEW
updateStep "2c" "in progress" 30
2cCompilePNGVIEW
if [[ $? -eq 0 ]]
then
updateStep "2c" "done" 35
else
updateStep "2c" "failed" 30
exit
fi
else
updateStep "2b" "failed" 25
exit
fi
fi
}
# Checks if PNGVIEW is installed
# RETURN
# 0 > PNGVIEW is installed
# 1 > PNGVIEW is not installed
function 2aTestPNGVIEW ()
{
printf "$(date +%FT%T%:z):\t2aTestPNGVIEW\tSTART\n" >> "${logfile}"
if [ -f /usr/bin/pngview ]
then
printf "$(date +%FT%T%:z):\t2aTestPNGVIEW\tFOUND\n" >> "${logfile}"
return 0
else
printf "$(date +%FT%T%:z):\t2aTestPNGVIEW\tNOT FOUND\n" >> "${logfile}"
return 1
fi
}
# Gets PNGVIEW source
# RETURN
# 0 > source downloaded and unzipped
# 1 > no source downloaded, removed temp files
function 2bGetPNGVIEWsource ()
{
printf "$(date +%FT%T%:z):\t2bGetPNGVIEWsource\tSTART\n" >> "${logfile}"
{ #try
wget -P ~ https://github.com/AndrewFromMelbourne/raspidmx/archive/master.zip --append-output="${logfile}" &&
unzip ~/master.zip -d ~ >> "${logfile}" &&
printf "$(date +%FT%T%:z):\t2bGetPNGVIEWsource\tDONE\n" >> "${logfile}" &&
return 0
} || { #catch
printf "$(date +%FT%T%:z):\t2bGetPNGVIEWsource\tERROR\n" >> "${logfile}" &&
rm ~/master.zip >> "${logfile}" &&
sudo rm -r ~/raspidmx-master >> "${logfile}" &&
return 1
}
}
# Compiles PNGVIEW source, moves binaries
# RETURN
# 0 > compiled without errors, moved binaries, removed temp files
# 1 > errors while compiling, removed temp files
function 2cCompilePNGVIEW ()
{
printf "$(date +%FT%T%:z):\t2cCompilePNGVIEW\tSTART\n" >> "${logfile}"
{ #try
# compile
# cd ~/raspidmx-master &&
make --directory=~/raspidmx-master >> "${logfile}" &&
# move binary files
sudo mv ~/raspidmx-master/pngview/pngview /usr/bin >> "${logfile}" &&
sudo mv ~/raspidmx-master/lib/libraspidmx.so.1 /usr/lib >> "${logfile}" &&
sudo chown root:root /usr/bin/pngview >> "${logfile}" &&
sudo chmod 755 /usr/bin/pngview >> "${logfile}" &&
# remove temp files
rm ~/master.zip >> "${logfile}" &&
sudo rm -r ~/raspidmx-master >> "${logfile}" &&
printf "$(date +%FT%T%:z):\t2cCompilePNGVIEW\tDONE\n" >> "${logfile}" &&
return 0
} || { #catch
printf "$(date +%FT%T%:z):\t2cCompilePNGVIEW\tERROR\n" >> "${logfile}" &&
# remove temp files
rm ~/master.zip >> "${logfile}" &&
sudo rm -r ~/raspidmx-master >> "${logfile}" &&
return 1
}
}
function 3IMAGEMAGICK ()
{
# 3a. Testing for IMAGEMAGICK
updateStep "3a" "in progress" 35
3aTestIMAGEMAGICK
if [[ $? -eq 0 ]]
then
updateStep "3a" "found" 40
updateStep "3b" "skipped" 45
else
updateStep "3a" "not found" 40
# 3b. Getting IMAGEMAGICK
updateStep "3b" "in progress" 40
3bInstallIMAGEMAGICK
if [[ $? -eq 0 ]]
then
updateStep "3b" "done" 45
else
updateStep "3b" "failed" 40
fi
fi
}
# Checks is IMAGEMAGICK is installed
# RETURN
# 0 > IMAGEMAGICK is installed
# 1 > IMAGEMAGICK is not installed
function 3aTestIMAGEMAGICK ()
{
printf "$(date +%FT%T%:z):\t3aTestIMAGEMAGICK\tSTART\n" >> "${logfile}"
if [ -f /usr/bin/convert ]
then
printf "$(date +%FT%T%:z):\t3aTestIMAGEMAGICK\tFOUND\n" >> "${logfile}"
return 0
else
printf "$(date +%FT%T%:z):\t3aTestIMAGEMAGICK\tNOT FOUND\n" >> "${logfile}"
return 1
fi
}
# Installs IMAGEMAGICK via APT-GET
# RETURN
# 0 > IMAGEMAGICK has been installed
# 1 > Error while installing IMAGEMAGICK
function 3bInstallIMAGEMAGICK ()
{
printf "$(date +%FT%T%:z):\t3bInstallIMAGEMAGICK\tSTART\n" >> "${logfile}"
sudo apt-get update >> "${logfile}"
sudo apt-get --yes install imagemagick >> "${logfile}"
if [[ $? -eq 0 ]]
then
printf "$(date +%FT%T%:z):\t3bInstallIMAGEMAGICK\tDONE\n" >> "${logfile}"
return 0
else
printf "$(date +%FT%T%:z):\t3bInstallIMAGEMAGICK\tERROR\n" >> "${logfile}"
return 1
fi
}
function 4RCLONE_SCRIPT ()
{
# 4a. Getting RCLONE_SCRIPT
updateStep "4a" "in progress" 45
4aGetRCLONE_SCRIPT
if [[ $? -eq 0 ]]
then
updateStep "4a" "done" 50
else
updateStep "4a" "failed" 45
exit
fi
# 4b. Creating RCLONE_SCRIPT menu item
updateStep "4b" "in progress" 50
4bCreateRCLONE_SCRIPTMenuItem
if [[ $? -eq 0 ]]
then
updateStep "4b" "done" 55
else
updateStep "4b" "failed" 50
exit
fi
# 4c. Configure RCLONE_SCRIPT
updateStep "4c" "in progress" 55
4cConfigureRCLONE_SCRIPT
updateStep "4c" "done" 60
}
# Gets RCLONE_SCRIPT
# RETURN
# 0 > downloaded successfully
# 1 > errors while downloading
function 4aGetRCLONE_SCRIPT ()
{
printf "$(date +%FT%T%:z):\t4aGetRCLONE_SCRIPT\tSTART\n" >> "${logfile}"
# create directory if necessary
if [ ! -d ~/scripts/rclone_script ]
then
mkdir ~/scripts/rclone_script >> "${logfile}"
fi
{ #try
# get script files
wget -N -P ~/scripts/rclone_script ${url}/${branch}/rclone_script.sh --append-output="${logfile}" &&
wget -N -P ~/scripts/rclone_script ${url}/${branch}/rclone_script-menu.sh --append-output="${logfile}" &&
wget -N -P ~/scripts/rclone_script ${url}/${branch}/rclone_script-uninstall.sh --append-output="${logfile}" &&
wget -N -P ~/scripts/rclone_script ${url}/${branch}/emulator_settings.xml --append-output="${logfile}" &&
# change mod
chmod +x ~/scripts/rclone_script/rclone_script.sh >> "${logfile}" &&
chmod +x ~/scripts/rclone_script/rclone_script-menu.sh >> "${logfile}" &&
chmod +x ~/scripts/rclone_script/rclone_script-uninstall.sh >> "${logfile}" &&
printf "$(date +%FT%T%:z):\t4aGetRCLONE_SCRIPT\tDONE\n" >> "${logfile}" &&
return 0
} || { # catch
printf "$(date +%FT%T%:z):\t4aGetRCLONE_SCRIPT\tERROR\n" >> "${logfile}" &&
return 1
}
}
# Creates a menu item for RCLONE_SCRIPT in RetroPie menu
# RETURN
# 0 > menu item has been found or created
# 1 > error while creating menu item
function 4bCreateRCLONE_SCRIPTMenuItem ()
{
printf "$(date +%FT%T%:z):\t4bCreateRCLONE_SCRIPTMenuItem\tSTART\n" >> "${logfile}"
# create redirect script
printf "#!/bin/bash\n~/scripts/rclone_script/rclone_script-menu.sh" > ~/RetroPie/retropiemenu/rclone_script-redirect.sh
chmod +x ~/RetroPie/retropiemenu/rclone_script-redirect.sh
# check if menu item exists
if [[ $(xmlstarlet sel -t -v "count(/gameList/game[path='./rclone_script-redirect.sh'])" ~/.emulationstation/gamelists/retropie/gamelist.xml) -eq 0 ]]
then
printf "$(date +%FT%T%:z):\t4bCreateRCLONE_SCRIPTMenuItem\tNOT FOUND\n" >> "${logfile}"
xmlstarlet ed \
--inplace \
--subnode "/gameList" --type elem -n game -v "" \
--subnode "/gameList/game[last()]" --type elem -n path -v "./rclone_script-redirect.sh" \
--subnode "/gameList/game[last()]" --type elem -n name -v "RCLONE_SCRIPT menu" \
--subnode "/gameList/game[last()]" --type elem -n desc -v "Launches a menu allowing you to start a full sync, configure RCLONE_SCRIPT or even uninstall it" \
~/.emulationstation/gamelists/retropie/gamelist.xml
if [[ $? -eq 0 ]]
then
printf "$(date +%FT%T%:z):\t4bCreateRCLONE_SCRIPTMenuItem\tCREATED\n" >> "${logfile}"
return 0
else
printf "$(date +%FT%T%:z):\t4bCreateRCLONE_SCRIPTMenuItem\tERROR\n" >> "${logfile}"
return 1
fi
else
printf "$(date +%FT%T%:z):\t4bCreateRCLONE_SCRIPTMenuItem\tFOUND\n" >> "${logfile}"
return 0
fi
}
# Gets user input to configure RCLONE_SCRIPT
function 4cConfigureRCLONE_SCRIPT ()
{
printf "$(date +%FT%T%:z):\t4cConfigureRCLONE_SCRIPT\tSTART\n" >> "${logfile}"
remotebasedir=$(dialog \
--stdout \
--colors \
--no-collapse \
--cr-wrap \
--no-cancel \
--backtitle "${backtitle}" \
--title "Remote base directory" \
--inputbox "\nPlease name the directory which will be used as your ${YELLOW}remote base directory${NORMAL}. If necessary, this directory will be created.\n\nExamples:\n* RetroArch\n* mySaves/RetroArch\n\n" 18 40 "RetroArch"
)
dialog \
--stdout \
--colors \
--no-collapse \
--cr-wrap \
--backtitle "${backtitle}" \
--title "Notifications" \
--yesno "\nDo you wish to see ${YELLOW}notifications${NORMAL} whenever RCLONE_SCRIPT is synchronizing?" 18 40
case $? in
0) shownotifications="TRUE" ;;
1) shownotifications="FALSE" ;;
*) shownotifications="FALSE" ;;
esac
choice=$(dialog \
--stdout \
--colors \
--no-collapse \
--cr-wrap \
--backtitle "${backtitle}" \
--title "Needed connection" \
--ok-label "Select" \
--no-cancel \
--menu "\nPlease select which type of connection will be needed for your configured remote" 20 50 5 \
0 "Internet access" \
1 "LAN / WLAN connection only"
)
neededConnection=${choice}
printf "$(date +%FT%T%:z):\t4cConfigureRCLONE_SCRIPT\tDONE\n" >> "${logfile}"
}
function 5RUNCOMMAND ()
{
# 5a. RUNCOMMAND-ONSTART
updateStep "5a" "in progress" 60
5aRUNCOMMAND-ONSTART
case $? in
0) updateStep "5a" "found" 65 ;;
1) updateStep "5a" "created" 65 ;;
esac
# 5b. RUNCOMMAND-ONEND
updateStep "5b" "in progress" 65
5aRUNCOMMAND-ONEND
case $? in
0) updateStep "5b" "found" 70 ;;
1) updateStep "5b" "created" 70 ;;
esac
}
# Checks call of RCLONE_SCRIPT by RUNCOMMAND-ONSTART
# RETURNS
# 0 > call found
# 1 > call created
function 5aRUNCOMMAND-ONSTART ()
{
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tSTART\n" >> "${logfile}"
# check if RUNCOMMAND-ONSTART.sh exists
if [ -f /opt/retropie/configs/all/runcommand-onstart.sh ]
then
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tFILE FOUND\n" >> "${logfile}"
# check if there's a call to RCLONE_SCRIPT
if grep -Fq "~/scripts/rclone_script/rclone_script.sh" /opt/retropie/configs/all/runcommand-onstart.sh
then
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tCALL FOUND\n" >> "${logfile}"
return 0
else
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tCALL NOT FOUND\n" >> "${logfile}"
# add call
printf "\n~/scripts/rclone_script/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"\n" >> /opt/retropie/configs/all/runcommand-onstart.sh
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tCALL CREATED\n" >> "${logfile}"
return 1
fi
else
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tFILE NOT FOUND\n" >> "${logfile}"
printf "#!/bin/bash\n~/scripts/rclone_script/rclone_script.sh \"down\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"\n" > /opt/retropie/configs/all/runcommand-onstart.sh
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONSTART\tFILE CREATED\n" >> "${logfile}"
return 1
fi
}
# Checks call of RCLONE_SCRIPT by RUNCOMMAND-ONEND
# RETURNS
# 0 > call found
# 1 > call created
function 5aRUNCOMMAND-ONEND ()
{
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tSTART\n" >> "${logfile}"
# check if RUNCOMMAND-ONEND.sh exists
if [ -f /opt/retropie/configs/all/runcommand-onend.sh ]
then
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tFILE FOUND\n" >> "${logfile}"
# check if there's a call to RCLONE_SCRIPT
if grep -Fq "~/scripts/rclone_script/rclone_script.sh" /opt/retropie/configs/all/runcommand-onend.sh
then
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tCALL FOUND\n" >> "${logfile}"
return 0
else
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tCALL NOT FOUND\n" >> "${logfile}"
# add call
printf "\n~/scripts/rclone_script/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"\n" >> /opt/retropie/configs/all/runcommand-onend.sh
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tCALL CREATED\n" >> "${logfile}"
return 1
fi
else
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tFILE NOT FOUND\n" >> "${logfile}"
printf "#!/bin/bash\n~/scripts/rclone_script/rclone_script.sh \"up\" \"\$1\" \"\$2\" \"\$3\" \"\$4\"\n" >> /opt/retropie/configs/all/runcommand-onend.sh
printf "$(date +%FT%T%:z):\t5aRUNCOMMAND-ONEND\tFILE CREATED\n" >> "${logfile}"
return 1
fi
}
function 6LocalSAVEFILEDirectory ()
{
# 6a. Test for local SAVEFILE directory
updateStep "6a" "in progress" 70
6aCheckLocalBaseDirectory
case $? in
0) updateStep "6a" "found" 75 ;;
1) updateStep "6a" "created" 75 ;;
esac
# 6b. Check local <SYSTEM> directories
updateStep "6b" "in progress" 75
6bCheckLocalSystemDirectories
case $? in
0) updateStep "6b" "found" 80 ;;
1) updateStep "6b" "created" 80 ;;
esac
}
# Checks if the local base SAVEFILE directory exists
# RETURN
# 0 > directory exists
# 1 > directory has been created
function 6aCheckLocalBaseDirectory ()
{
printf "$(date +%FT%T%:z):\t6aCheckLocalBaseDirectory\tSTART\n" >> "${logfile}"
# check if local base dir exists
if [ -d ~/RetroPie/saves ]
then
printf "$(date +%FT%T%:z):\t6aCheckLocalBaseDirectory\tFOUND\n" >> "${logfile}"
return 0
else
printf "$(date +%FT%T%:z):\t6aCheckLocalBaseDirectory\tNOT FOUND\n" >> "${logfile}"
mkdir ~/RetroPie/saves
printf "$(date +%FT%T%:z):\t6aCheckLocalBaseDirectory\tCREATED directory\n" >> "${logfile}"
# share that new directory on the network
if [[ $(grep -c "\[saves\]" /etc/samba/smb.conf) -eq 0 ]]
then
# add new share to SAMBA
printf "[saves]\ncomment = saves\npath = \"/home/pi/RetroPie/saves\"\nwritable = yes\nguest ok = yes\ncreate mask = 0644\ndirectory mask = 0755\nforce user = pi\n" | sudo tee --append /etc/samba/smb.conf | cat > /dev/null
# restart SAMBA
sudo service smbd restart
printf "$(date +%FT%T%:z):\t6aCheckLocalBaseDirectory\tCREATED network share\n" >> "${logfile}"
fi
return 1
fi
}
# Checks if the local system specific directories exists
# RETURN
# 0 > all found
# 1 > created at least one
function 6bCheckLocalSystemDirectories ()
{
printf "$(date +%FT%T%:z):\t6bCheckLocalSystemDirectories\tSTART\n" >> "${logfile}"
local retval=0
# for each directory in ROMS directory...
for directory in ~/RetroPie/roms/*
do
system="${directory##*/}"
# check if ROMS directory is a real directory and not a SymLink
if [ ! -L ~/RetroPie/roms/${system} ]
then
# check if same directory exists in SAVES, create if necessary
if [ -d ~/RetroPie/saves/${system} ]
then
printf "$(date +%FT%T%:z):\t6bCheckLocalSystemDirectories\tFOUND directory ${system}\n" >> "${logfile}"
else
mkdir ~/RetroPie/saves/${system}
printf "$(date +%FT%T%:z):\t6bCheckLocalSystemDirectories\tCREATED directory ${system}\n" >> "${logfile}"
retval=1
fi
else
# check if same SymLink exists in SAVES, create if necessary
if [ -L ~/RetroPie/saves/${system} ]
then
printf "$(date +%FT%T%:z):\t6bCheckLocalSystemDirectories\tFOUND symlink ${system}\n" >> "${logfile}"
else
ln -s $(readlink ~/RetroPie/roms/${system}) ~/RetroPie/saves/${system}
printf "$(date +%FT%T%:z):\t6bCheckLocalSystemDirectories\tCREATED symlink ${system}\n" >> "${logfile}"
retval=1
fi
fi
done
return ${retval}
}
function 7RemoteSAVEFILEDirectory ()
{
# 7a. Check remote base directory
updateStep "7a" "in progress" 80
7aCheckRemoteBaseDirectory
case $? in
0) updateStep "7a" "found" 85 ;;
1) updateStep "7a" "created" 85 ;;
255) updateStep "7a" "failed" 80 ;;
esac
# 7b. Check remote <system> directories
updateStep "7b" "in progress" 85
7bCheckRemoteSystemDirectories
case $? in
0) updateStep "7b" "found" 90 ;;
1) updateStep "7b" "created" 90 ;;
255) updateStep "7b" "failed" 85 ;;
esac
}
# Checks if the remote base SAVEFILE directory exists
# RETURN
# 0 > directory exists
# 1 > directory has been created
# 255 > error while creating directory
function 7aCheckRemoteBaseDirectory ()
{
printf "$(date +%FT%T%:z):\t7aCheckRemoteBaseDirectory\tSTART\n" >> "${logfile}"
# try to read remote base dir
rclone lsf "retropie:${remotebasedir}/" > /dev/null 2>&1
case $? in
0)
printf "$(date +%FT%T%:z):\t7aCheckRemoteBaseDirectory\tFOUND\n" >> "${logfile}"
return 0
;;
3)
printf "$(date +%FT%T%:z):\t7aCheckRemoteBaseDirectory\tNOT FOUND\n" >> "${logfile}"
rclone mkdir "retropie:${remotebasedir}" >> "${logfile}"
case $? in