-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathinstall-jukebox.sh
More file actions
1487 lines (1294 loc) · 56.8 KB
/
Copy pathinstall-jukebox.sh
File metadata and controls
1487 lines (1294 loc) · 56.8 KB
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
#!/usr/bin/env bash
#
# see https://github.com/MiczFlor/RPi-Jukebox-RFID for details
#
# NOTE: Running automated install (without interaction):
# Each install creates a file called PhonieboxInstall.conf
# in you $HOME directory
# You can install the Phoniebox using such a config file
# which means you don't need to run the interactive install:
#
# 1. download the install file from github
# https://github.com/MiczFlor/RPi-Jukebox-RFID/tree/master/scripts/installscripts
# 2. make the file executable: chmod +x
# 3. place the PhonieboxInstall.conf in the folder $HOME
# 4. run the installscript with option -a like this:
# install-jukebox.sh -a
# The absolute path to the folder which contains this script
PATHDATA="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GIT_BRANCH=${GIT_BRANCH:-master}
GIT_URL=${GIT_URL:-https://github.com/MiczFlor/RPi-Jukebox-RFID.git}
DATETIME=$(date +"%Y%m%d_%H%M%S")
SCRIPTNAME="$(basename $0)"
JOB="${SCRIPTNAME}"
CURRENT_USER="${SUDO_USER:-$(whoami)}"
HOME_DIR=$(getent passwd "$CURRENT_USER" | cut -d: -f6)
JUKEBOX_HOME_DIR="${HOME_DIR}/RPi-Jukebox-RFID"
LOGDIR="${HOME_DIR}"/phoniebox_logs
JUKEBOX_BACKUP_DIR="${HOME_DIR}/BACKUP"
# Get the Raspberry Pi OS codename (e.g. buster, bullseye, ...)
OS_CODENAME="$( . /etc/os-release; printf '%s\n' "$VERSION_CODENAME"; )"
# Get the Raspberry Pi OS version id (e.g. 11, 12, ...)
OS_VERSION_ID="$( . /etc/os-release; printf '%s\n' "$VERSION_ID"; )"
WIFI_INTERFACE="wlan0"
INTERACTIVE=true
usage() {
printf "Usage: ${SCRIPTNAME} [-a] [-h]\n"
printf " -a\tautomatic/non-interactive mode\n"
printf " -h\thelp\n"
exit 0
}
while getopts ":ah" opt;
do
case ${opt} in
a ) INTERACTIVE=false
;;
h ) usage
;;
\? ) usage
;;
esac
done
# Setup logger functions
# Input from http://www.ludovicocaldara.net/dba/bash-tips-5-output-logfile/
log_open() {
[[ -d "${LOGDIR}" ]] || mkdir -p "${LOGDIR}"
PIPE="${LOGDIR}"/"${JOB}"_"${DATETIME}".pipe
mkfifo -m 700 "${PIPE}"
LOGFILE="${LOGDIR}"/"${JOB}"_"${DATETIME}".log
exec 3>&1
tee "${LOGFILE}" <"${PIPE}" >&3 &
TEEPID=$!
exec 1>"${PIPE}" 2>&1
PIPE_OPENED=1
}
log_close() {
if [ "${PIPE_OPENED}" ]; then
exec 1<&3
sleep 0.2
ps --pid "${TEEPID}" >/dev/null
if [ $? -eq 0 ] ; then
# a wait ${TEEPID} whould be better but some
# commands leave file descriptors open
sleep 1
kill "${TEEPID}"
fi
rm "${PIPE}"
unset PIPE_OPENED
fi
}
# local function as it is needed before the repo is checked out!
_escape_for_shell() {
local escaped="${1//\"/\\\"}"
escaped="${escaped//\`/\\\`}"
escaped="${escaped//\$/\\\$}"
echo "$escaped"
}
checkPrerequisite() {
#currently the user 'pi' is mandatory
#https://github.com/MiczFlor/RPi-Jukebox-RFID/issues/1785
if [ "${CURRENT_USER}" != "pi" ]; then
echo
echo "ERROR: User must be 'pi'!"
echo " Other usernames are currently not supported."
echo " Please check the wiki for further information"
exit 2
fi
if [ "${HOME_DIR}" != "/home/pi" ]; then
echo
echo "ERROR: HomeDir must be '/home/pi'!"
echo " Other usernames are currently not supported."
echo " Please check the wiki for further information"
exit 2
fi
if [ ! -d "${HOME_DIR}" ]; then
echo
echo "Warning: HomeDir ${HOME_DIR} does not exist."
echo " Please create it and start again."
exit 2
fi
}
welcome() {
clear
echo "#####################################################
# ___ __ ______ _ __________ ____ __ _ _ #
# / _ \/ // / __ \/ |/ / _/ __/( _ \ / \( \/ ) #
# / ___/ _ / /_/ / // // _/ ) _ (( O )) ( #
# /_/ /_//_/\____/_/|_/___/____/ (____/ \__/(_/\_) #
# #
#####################################################
You are turning your Raspberry Pi into a Phoniebox. Good choice.
This INTERACTIVE INSTALL script requires you to be online and
will guide you through the configuration.
If you want to run the AUTOMATED INSTALL (non-interactive) from
an existing configuration file, do the following:
1. exit this install script (press n)
2. place your PhonieboxInstall.conf in the folder ${HOME_DIR}
3. run the installscript with option -a. For example like this:
${HOME_DIR}/install-jukebox.sh -a
"
read -rp "Continue interactive installation? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
exit
;;
*)
echo "Installation continues..."
;;
esac
}
reset_install_config_file() {
#####################################################
# CONFIG FILE
# This file will contain all the data given in the
# following dialogue
# At a later stage, the install should also be done
# from such a config file with no user input.
# Remove existing config file
#rm "${HOME_DIR}/PhonieboxInstall.conf"
# Create empty config file
#touch "${HOME_DIR}/PhonieboxInstall.conf"
#echo "# Phoniebox config" > "${HOME_DIR}/PhonieboxInstall.conf"
echo "# Phoniebox config"
}
config_wifi() {
#####################################################
# Ask if wifi config
clear
echo "#####################################################
#
# CONFIGURE WIFI
#
# Requires SSID, WiFi password and the static IP you want
# to assign to your Phoniebox.
# (Note: can be done manually later, if you are unsure.)
"
read -rp "Do you want to configure your WiFi? [y/N] " response
echo ""
case "$response" in
[yY][eE][sS]|[yY])
WIFIconfig=YES
#Ask for SSID
read -rp "* Type SSID name: " WIFIssid
#Ask for wifi country code
read -rp "* WiFi Country Code (e.g. DE, GB, CZ or US): " WIFIcountryCode
#Ask for password
read -rp "* Type password: " WIFIpass
#Ask for IP
read -rp "* Static IP (e.g. 192.168.1.199): " WIFIip
#Ask for Router IP
read -rp "* Router IP (e.g. 192.168.1.1): " WIFIipRouter
echo ""
echo "Your WiFi config:"
echo "SSID : $WIFIssid"
echo "WiFi Country Code : $WIFIcountryCode"
echo "Password : $WIFIpass"
echo "Static IP : $WIFIip"
echo "Router IP : $WIFIipRouter"
read -rp "Are these values correct? [Y/n] " response
echo ""
case "$response" in
[nN][oO]|[nN])
echo "The values are incorrect."
read -rp "Hit ENTER to exit and start over." INPUT; exit
;;
*)
# append variables to config file
{
echo "WIFIconfig=\"$(_escape_for_shell "$WIFIconfig")\"";
echo "WIFIcountryCode=\"$(_escape_for_shell "$WIFIcountryCode")\"";
echo "WIFIssid=\"$(_escape_for_shell "$WIFIssid")\"";
echo "WIFIpass=\"$(_escape_for_shell "$WIFIpass")\"";
echo "WIFIip=\"$(_escape_for_shell "$WIFIip")\"";
echo "WIFIipRouter=\"$(_escape_for_shell "$WIFIipRouter")\"";
} >> "${HOME_DIR}/PhonieboxInstall.conf"
;;
esac
;;
*)
WIFIconfig=NO
echo "You want to configure WiFi later."
# append variables to config file
echo "WIFIconfig=\"$(_escape_for_shell "$WIFIconfig")\"" >> "${HOME_DIR}/PhonieboxInstall.conf"
# make a fallback for WiFi Country Code, because we need that even without WiFi config
echo "WIFIcountryCode=\"$(_escape_for_shell "DE")\"" >> "${HOME_DIR}/PhonieboxInstall.conf"
;;
esac
read -rp "Hit ENTER to proceed to the next step." INPUT
}
config_autohotspot() {
#####################################################
# Ask if an autohotspot should be created if no known network is found.
clear
echo "#####################################################
#
# CONFIGURE AUTOHOTSPOT
#
# Automatically sets up a wifi hotspot if no known network is found.
# This enables you to directly connect to your phoniebox
# and change configuration (e.g. while you travel).
# (Note: can be done manually later, if you are unsure.)
"
read -rp "Do you want to configure autohotspot? [y/N] " response
echo ""
case "$response" in
[yY][eE][sS]|[yY])
AUTOHOTSPOTconfig=YES
AUTOHOTSPOTssid="phoniebox"
AUTOHOTSPOTcountryCode="DE"
AUTOHOTSPOTpass="PlayItLoud"
AUTOHOTSPOTip="10.0.0.5"
echo ""
echo "The autohotspot configuration uses this default values:"
echo "SSID : $AUTOHOTSPOTssid"
echo "WiFi Country Code : $AUTOHOTSPOTcountryCode"
echo "Password : $AUTOHOTSPOTpass"
echo "Static IP : $AUTOHOTSPOTip"
read -rp "Do you want to use this default values? [Y/n] " response
echo ""
case "$response" in
[nN][oO]|[nN])
#Ask for SSID
read -rp "* Type SSID name: " AUTOHOTSPOTssid
#Ask for wifi country code
read -rp "* Type WiFi Country Code (e.g. DE, GB, CZ or US): " AUTOHOTSPOTcountryCode
#Ask for password
read -rp "* Type password (8 characters at least. max 63 characters): " AUTOHOTSPOTpass
#Ask for IP
read -rp "* Type Static IP (e.g. 10.0.0.5, 192.168.1.199): " AUTOHOTSPOTip
echo ""
echo "Your Autohotspot config:"
echo "SSID : $AUTOHOTSPOTssid"
echo "WiFi Country Code : $AUTOHOTSPOTcountryCode"
echo "Password : $AUTOHOTSPOTpass"
echo "Static IP : $AUTOHOTSPOTip"
read -rp "Are these values correct? [Y/n] " response
echo ""
case "$response" in
[nN][oO]|[nN])
echo "The values are incorrect."
read -rp "Hit ENTER to exit and start over." INPUT; exit
;;
*)
# step out and continue
;;
esac
;;
*)
# step out and continue
;;
esac
# append variables to config file
{
echo "AUTOHOTSPOTconfig=\"$(_escape_for_shell "$AUTOHOTSPOTconfig")\"";
echo "AUTOHOTSPOTssid=\"$(_escape_for_shell "$AUTOHOTSPOTssid")\"";
echo "AUTOHOTSPOTcountryCode=\"$(_escape_for_shell "$AUTOHOTSPOTcountryCode")\"";
echo "AUTOHOTSPOTpass=\"$(_escape_for_shell "$AUTOHOTSPOTpass")\"";
echo "AUTOHOTSPOTip=\"$(_escape_for_shell "$AUTOHOTSPOTip")\"";
} >> "${HOME_DIR}/PhonieboxInstall.conf"
;;
*)
AUTOHOTSPOTconfig=NO
echo "You don't want to configure Autohotspot."
# append variables to config file
echo "AUTOHOTSPOTconfig=\"$(_escape_for_shell "$AUTOHOTSPOTconfig")\"" >> "${HOME_DIR}/PhonieboxInstall.conf"
;;
esac
read -rp "Hit ENTER to proceed to the next step." INPUT
}
check_existing() {
local jukebox_dir="$1"
local backup_dir="$2"
local local_home_dir="$3"
#####################################################
# Check for existing Phoniebox
#
# In case there is no existing install,
# set the var now for later use:
EXISTINGuse=NO
# The install will be in the home dir of user pi
# Move to home directory now to check
cd "${local_home_dir}"
if [ -d "${jukebox_dir}" ]; then
# Houston, we found something!
clear
echo "#####################################################
#
# . . . * alert * alert * alert * alert * . . .
#
# WARNING: an existing Phoniebox installation was found.
#
"
# check if we find the version number
if [ -f "${jukebox_dir}"/settings/version ]; then
#echo "The version of your installation is: $(cat ${jukebox_dir}/settings/version)"
# get the current short commit hash of the repo
CURRENT_REMOTE_COMMIT="$(git ls-remote ${GIT_URL} ${GIT_BRANCH} | cut -c1-7)"
fi
echo "IMPORTANT: you can use the existing content and configuration"
echo "files for your new install."
echo "Whatever you chose to keep will be moved to the new install."
echo "Everything else will remain in a folder called 'BACKUP'.
"
###
# See if we find the PhonieboxInstall.conf file
# We need to do this first, because if we re-use the .conf file, we need to append
# the variables regarding the found content to the also found configuration file.
# That way, reading the configuration file for the (potentially) non-interactive
# install procedure will:
# a) overwrite whatever variables regarding re-cycling existing content which might
# be stored in the config file
# b) if there are no variables for dealing with re-cycled context, we will append
# them - to have them for this install
if [ -f "${jukebox_dir}"/settings/PhonieboxInstall.conf ]; then
# ask for re-using the found configuration file
echo "The configuration of your last Phoniebox install was found."
read -rp "Use existing configuration for this installation? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
EXISTINGusePhonieboxInstall=NO
;;
*)
EXISTINGusePhonieboxInstall=YES
# Copy PhonieboxInstall.conf configuration file to settings folder
sudo cp "${jukebox_dir}"/settings/PhonieboxInstall.conf "${local_home_dir}"/PhonieboxInstall.conf
sudo chown pi:www-data "${local_home_dir}"/PhonieboxInstall.conf
sudo chmod 775 "${local_home_dir}"/PhonieboxInstall.conf
echo "The existing configuration will be used."
echo "Just a few more questions to answer."
read -rp "Hit ENTER to proceed to the next step." INPUT
clear
;;
esac
fi
# Delete or use existing installation?
read -rp "Re-use config, audio and RFID codes for the new install? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
EXISTINGuse=NO
echo "Phoniebox will be a fresh install. The existing version will be dropped."
sudo rm -rf "${jukebox_dir}"
read -rp "Hit ENTER to proceed to the next step." INPUT
;;
*)
EXISTINGuse=YES
# CREATE BACKUP
# delete existing BACKUP dir if exists
if [ -d "${backup_dir}" ]; then
sudo rm -r "${backup_dir}"
fi
# move install to BACKUP dir
mv "${jukebox_dir}" "${backup_dir}"
# delete .git dir
if [ -d "${backup_dir}"/.git ]; then
sudo rm -r "${backup_dir}"/.git
fi
# delete placeholder files so moving the folder content back later will not create git pull conflicts
rm "${backup_dir}"/shared/audiofolders/placeholder
rm "${backup_dir}"/shared/shortcuts/placeholder
# ask for things to use
echo "Ok. You want to use stuff from the existing installation."
echo "What would you want to keep? Answer now."
read -rp "RFID config for system control (e.g. 'volume up' etc.)? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
EXISTINGuseRfidConf=NO
;;
*)
EXISTINGuseRfidConf=YES
;;
esac
# append variables to config file
echo "EXISTINGuseRfidConf=\"$(_escape_for_shell "$EXISTINGuseRfidConf")\"" >> "${local_home_dir}/PhonieboxInstall.conf"
read -rp "RFID shortcuts to play audio folders? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
EXISTINGuseRfidLinks=NO
;;
*)
EXISTINGuseRfidLinks=YES
;;
esac
# append variables to config file
echo "EXISTINGuseRfidLinks=\"$(_escape_for_shell "$EXISTINGuseRfidLinks")\"" >> "${local_home_dir}/PhonieboxInstall.conf"
read -rp "Audio folders: use existing? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
EXISTINGuseAudio=NO
;;
*)
EXISTINGuseAudio=YES
;;
esac
# append variables to config file
echo "EXISTINGuseAudio=\"$(_escape_for_shell "$EXISTINGuseAudio")\"" >> "${local_home_dir}/PhonieboxInstall.conf"
read -rp "Sound effects: use existing startup / shutdown sounds? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
EXISTINGuseSounds=NO
;;
*)
EXISTINGuseSounds=YES
;;
esac
# append variables to config file
echo "EXISTINGuseSounds=\"$(_escape_for_shell "$EXISTINGuseSounds")\"" >> "${local_home_dir}/PhonieboxInstall.conf"
if [ "$(printf '%s\n' "2.1" "$(cat ${local_home_dir}/BACKUP/settings/version-number)" | sort -V | head -n1)" = "2.1" ]; then
read -rp "GPIO: use existing file? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
EXISTINGuseGpio=NO
;;
*)
EXISTINGuseGpio=YES
;;
esac
else
echo ""
echo "Warning!
The configuration of GPIO-Devices has changed in the new version
and needs to be reconfigured. For further info check out the wiki:
https://github.com/MiczFlor/RPi-Jukebox-RFID/wiki/Using-GPIO-hardware-buttons"
read -rp "Hit ENTER to proceed to the next step." INPUT
config_gpio
fi
# append variables to config file
echo "EXISTINGuseGpio=\"$(_escape_for_shell "$EXISTINGuseGpio")\"" >> "${local_home_dir}/PhonieboxInstall.conf"
read -rp "Button USB Encoder: use existing device and button mapping? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
EXISTINGuseButtonUSBEncoder=NO
;;
*)
EXISTINGuseButtonUSBEncoder=YES
;;
esac
# append variables to config file
echo "EXISTINGuseButtonUSBEncoder=\"$(_escape_for_shell "$EXISTINGuseButtonUSBEncoder")\"" >> "${local_home_dir}/PhonieboxInstall.conf"
echo "Thanks. Got it."
echo "The existing install can be found in the BACKUP directory."
read -rp "Hit ENTER to proceed to the next step." INPUT
;;
esac
fi
# append variables to config file
echo "EXISTINGuse=\"$(_escape_for_shell "$EXISTINGuse")\"" >> "${local_home_dir}/PhonieboxInstall.conf"
# Check if we found a Phoniebox install configuration earlier and ask if to run this now
if [ "${EXISTINGusePhonieboxInstall}" == "YES" ]; then
clear
echo "Using the existing configuration, you can run a non-interactive install."
echo "This will re-cycle found content (specified just now) as well as the"
echo "system information from last time (wifi, audio interface, spotify, etc.)."
read -rp "Do you want to run a non-interactive installation? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
;;
*)
cd "${local_home_dir}"
clear
./install-jukebox.sh -a
exit
;;
esac
fi
}
config_audio_interface() {
#####################################################
# Audio iFace
clear
local amixer_scontrols=$(sudo amixer scontrols)
local audio_interfaces=$(echo "${amixer_scontrols}" | sed "s|.*'\(.*\)'.*|\1|g")
local first_audio_interface=$(echo "${audio_interfaces}" | head -1)
local default_audio_interface="${first_audio_interface:-PCM}"
echo "#####################################################
#
# CONFIGURE AUDIO INTERFACE (iFace)
#
# The default RPi audio interface is '${default_audio_interface}'.
# But this does not work for every setup.
# Here a list of available iFace names:
${audio_interfaces}
"
echo " "
read -rp "Use '${default_audio_interface}' as iFace? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
read -rp "Type the iFace name you want to use:" AUDIOiFace
;;
*)
AUDIOiFace="${default_audio_interface}"
;;
esac
# append variables to config file
echo "AUDIOiFace=\"$(_escape_for_shell "$AUDIOiFace")\"" >> "${HOME_DIR}/PhonieboxInstall.conf"
echo "Your iFace is called '$AUDIOiFace'"
read -rp "Hit ENTER to proceed to the next step." INPUT
}
config_spotify() {
#####################################################
# Configure spotify
clear
echo "#####################################################
#
# OPTIONAL: INCLUDE SPOTIFY
#
# Note: if this is your first time installing a phoniebox
# it might be best to do a test install without Spotify
# to make sure all your hardware works.
#
# If you want to include Spotify, MUST have your
# credentials ready:
#
# * username
# * password
# * client_id
# * client_secret
"
read -rp "Do you want to enable Spotify? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
SPOTinstall=YES
clear
echo "#####################################################
#
# CREDENTIALS for Spotify
#
# Requires Spotify username, password, client_id and client_secret
# to get connection to Spotify.
#
# (Note: You need a device with browser to generate ID and SECRET)
#
# Please go to this website:
# https://www.mopidy.com/authenticate/
# and follow the instructions.
#
# Your credential will appear on the site below the login button.
# Please note your client_id and client_secret!
#
"
read -rp "Type your client_id: " SPOTIclientid
read -rp "Type your client_secret: " SPOTIclientsecret
;;
*)
SPOTinstall=NO
echo "You don't want spotify support."
;;
esac
# append variables to config file
{
echo "SPOTinstall=\"$(_escape_for_shell "$SPOTinstall")\"";
echo "SPOTIclientid=\"$(_escape_for_shell "$SPOTIclientid")\"";
echo "SPOTIclientsecret=\"$(_escape_for_shell "$SPOTIclientsecret")\""
} >> "${HOME_DIR}/PhonieboxInstall.conf"
read -rp "Hit ENTER to proceed to the next step." INPUT
}
config_audio_folder() {
local jukebox_dir="$1"
#####################################################
# Folder path for audio files
# default: $HOME/RPi-Jukebox-RFID/shared/audiofolders
clear
echo "#####################################################
#
# FOLDER CONTAINING AUDIO FILES
#
# The default location for folders containing audio files:
# ${jukebox_dir}/shared/audiofolders
#
# If unsure, keep it like this. If your files are somewhere
# else, you can specify the folder in the next step.
# IMPORTANT: the folder will not be created, only the path
# will be remembered. If you use a custom folder, you must
# create it.
"
read -rp "Do you want to use the default location? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
echo "Please type the absolute path here (no trailing slash)."
echo "Default would be for example: ${jukebox_dir}/shared/audiofolders"
read -r DIRaudioFolders
;;
*)
DIRaudioFolders="${jukebox_dir}/shared/audiofolders"
;;
esac
# append variables to config file
echo "DIRaudioFolders=\"$(_escape_for_shell "$DIRaudioFolders")\"" >> "${HOME_DIR}/PhonieboxInstall.conf"
echo "Your audio folders live in this dir:"
echo "${DIRaudioFolders}"
read -rp "Hit ENTER to proceed to the next step." INPUT
}
check_variable() {
local variable=${1}
# check if variable exist and if it's empty
test -z "${!variable+x}" && echo "ERROR: \$${variable} is missing!" && fail=true && return
test "${!variable}" == "" && echo "ERROR: \$${variable} is empty!" && fail=true
}
config_gpio() {
#####################################################
# Configure GPIO
clear
echo "#####################################################
#
# ACTIVATE GPIO-Control
#
# Activation of the GPIO-Control-Service, which mangages Buttons
# or a Rotary Encoder for Volume and/or Track control.
# To configure the controls please consult the wiki:
# https://github.com/MiczFlor/RPi-Jukebox-RFID/wiki/Using-GPIO-hardware-buttons
# It's also possible to activate the service later (see wiki).
"
read -rp "Do you want to activate the GPIO-Control-Service? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
GPIOconfig=NO
echo "You don't want to activate GPIO-Controls now."
;;
*)
GPIOconfig=YES
echo "GPIO-Control-Service will be activated and set to default values."
;;
esac
# append variables to config file
echo "GPIOconfig=\"$(_escape_for_shell "$GPIOconfig")\"" >> "${HOME_DIR}/PhonieboxInstall.conf"
echo ""
read -rp "Hit ENTER to proceed to the next step." INPUT
}
check_config_file() {
local install_conf="${HOME_DIR}/PhonieboxInstall.conf"
echo "Checking PhonieboxInstall.conf..."
# check that PhonieboxInstall.conf exists and is not empty
# check if config file exists
if [[ -f "${install_conf}" ]]; then
# Source config file
source "${install_conf}"
cat "${install_conf}"
echo ""
else
echo "ERROR: ${install_conf} does not exist!"
exit 1
fi
fail=false
if [[ -z "${WIFIconfig+x}" ]]; then
echo "ERROR: \$WIFIconfig is missing or not set!" && fail=true
else
if [[ "$WIFIconfig" == "YES" ]]; then
check_variable "WIFIcountryCode"
check_variable "WIFIssid"
check_variable "WIFIpass"
check_variable "WIFIip"
check_variable "WIFIipRouter"
fi
fi
check_variable "EXISTINGuse"
check_variable "AUDIOiFace"
if [[ -z "${SPOTinstall+x}" ]]; then
echo "ERROR: \$SPOTinstall is missing or not set!" && fail=true
else
if [ "$SPOTinstall" == "YES" ]; then
check_variable "SPOTIclientid"
check_variable "SPOTIclientsecret"
fi
fi
check_variable "DIRaudioFolders"
check_variable "GPIOconfig"
# Feature optional. if config not present, defaults to NO
if [[ -z "${AUTOHOTSPOTconfig}" ]]; then
echo "INFO: \$AUTOHOTSPOTconfig is missing or not set"
else
if [[ "$AUTOHOTSPOTconfig" == "YES" ]]; then
check_variable "AUTOHOTSPOTssid"
check_variable "AUTOHOTSPOTcountryCode"
check_variable "AUTOHOTSPOTpass"
check_variable "AUTOHOTSPOTip"
fi
fi
if [ "${fail}" == "true" ]; then
exit 1
fi
echo ""
}
samba_config() {
local smb_conf="/etc/samba/smb.conf"
echo "Configuring Samba..."
# Samba configuration settings
# -rw-r--r-- 1 root root 9416 Apr 30 09:02 /etc/samba/smb.conf
sudo cp "${jukebox_dir}"/misc/sampleconfigs/smb.conf-default.sample ${smb_conf}
sudo chown root:root "${smb_conf}"
sudo chmod 644 "${smb_conf}"
# for $DIRaudioFolders using | as alternate regex delimiter because of the folder path slash
sudo sed -i 's|%DIRaudioFolders%|'"$(escape_for_sed "$DIRaudioFolders")"'|' "${smb_conf}"
# Samba: create user 'pi' with password 'raspberry'
# ToDo: use current user with a default password
(echo "raspberry"; echo "raspberry") | sudo smbpasswd -s -a pi
}
web_server_config() {
local lighthttpd_conf="/etc/lighttpd/lighttpd.conf"
local fastcgi_php_conf="/etc/lighttpd/conf-available/15-fastcgi-php.conf"
local php_ini="/etc/php/$(ls -1 /etc/php)/cgi/php.ini"
echo "Configuring web server..."
# make sure lighttp can access the home directory of the user
sudo chmod o+x ${HOME_DIR}
# Web server configuration settings
# -rw-r--r-- 1 root root 1040 Apr 30 09:19 /etc/lighttpd/lighttpd.conf
sudo cp "${jukebox_dir}"/misc/sampleconfigs/lighttpd.conf-default.sample "${lighthttpd_conf}"
sudo chown root:root "${lighthttpd_conf}"
sudo chmod 644 "${lighthttpd_conf}"
# Web server PHP7 fastcgi conf
# -rw-r--r-- 1 root root 398 Apr 30 09:35 /etc/lighttpd/conf-available/15-fastcgi-php.conf
sudo cp "${jukebox_dir}"/misc/sampleconfigs/15-fastcgi-php.conf-default.sample ${fastcgi_php_conf}
sudo chown root:root "${fastcgi_php_conf}"
sudo chmod 644 "${fastcgi_php_conf}"
# settings for php.ini to support upload
# -rw-r--r-- 1 root root 70999 Jun 14 13:50 /etc/php/7.3/cgi/php.ini
sudo cp "${jukebox_dir}"/misc/sampleconfigs/php.ini-default.sample ${php_ini}
sudo chown root:root "${php_ini}"
sudo chmod 644 "${php_ini}"
# SUDO users (adding web server here)
local sudoers_wwwdata="/etc/sudoers.d/www-data"
echo "www-data ALL=(ALL) NOPASSWD: ALL" | sudo tee "${sudoers_wwwdata}" > /dev/null
sudo chown root:root "${sudoers_wwwdata}"
sudo chmod 440 "${sudoers_wwwdata}"
}
install_main() {
local jukebox_dir="$1"
local apt_get="sudo apt-get -qq --yes"
local allow_downgrades="--allow-downgrades --allow-remove-essential --allow-change-held-packages"
local pip_install="sudo python3 -m pip install --upgrade --force-reinstall -q"
local pip_uninstall="sudo python3 -m pip uninstall -y -q"
clear
echo "#####################################################
#
# START INSTALLATION
#
# Good news: you completed the input.
# Let the install begin.
#
# Get yourself a cup of something. The install takes
# between 15 minutes to half an hour, depending on
# your Raspberry Pi and Internet connectivity.
#
# You will be prompted later to complete the installation.
"
if [[ ${INTERACTIVE} == "true" ]]; then
read -rp "Do you want to start the installation? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
echo "Exiting the installation."
echo "Your configuration data was saved in this file:"
echo "${HOME_DIR}/PhonieboxInstall.conf"
echo
exit
;;
esac
fi
# Start logging here
log_open
echo "################################################"
echo "Interactive mode: ${INTERACTIVE}"
echo "GIT_BRANCH ${GIT_BRANCH}"
echo "GIT_URL ${GIT_URL}"
echo "Current User: ${CURRENT_USER}"
echo "User home dir: ${HOME_DIR}"
echo "Used Raspberry Pi OS: ${OS_CODENAME}"
# Add conffile into logfile for better debugging
echo "################################################"
grep -v -e "SPOTI" -e "WIFIpass" "${HOME_DIR}/PhonieboxInstall.conf"
echo "################################################"
#####################################################
# INSTALLATION
# Read install config as written so far
. "${HOME_DIR}/PhonieboxInstall.conf"
# power management of wifi: switch off to avoid disconnecting
sudo iwconfig "$WIFI_INTERFACE" power off
# in the docker test env fiddling with resolv.conf causes issues, see https://stackoverflow.com/a/60576223
if [ "$DOCKER_RUNNING" != "true" ]; then
# create backup of /etc/resolv.conf
sudo cp /etc/resolv.conf /etc/resolv.conf.orig
fi
# Generate locales
sudo locale-gen "${LANG}"
# Install required packages
sudo mkdir -p /etc/apt/keyrings
${apt_get} update
${apt_get} upgrade
# Get github code. git must be installed before, even if defined in packages.txt!
${apt_get} install git
cd "${HOME_DIR}"
git clone ${GIT_URL} --branch "${GIT_BRANCH}"
source "${jukebox_dir}"/scripts/helperscripts/inc.helper.sh
source "${jukebox_dir}"/scripts/helperscripts/inc.networkHelper.sh
# Remove excluded libs, if installed - see https://github.com/MiczFlor/RPi-Jukebox-RFID/pull/2469
call_with_args_from_file "${jukebox_dir}"/packages-excluded.txt ${apt_get} ${allow_downgrades} remove
# some packages are only available on raspberry pi's but not on test docker containers running on x86_64 machines
if [[ $(uname -m) =~ ^armv.+$ ]]; then
call_with_args_from_file "${jukebox_dir}"/packages-raspberrypi.txt ${apt_get} ${allow_downgrades} install
fi
call_with_args_from_file "${jukebox_dir}"/packages.txt ${apt_get} ${allow_downgrades} install
# in the docker test env fiddling with resolv.conf causes issues, see https://stackoverflow.com/a/60576223
if [ "$DOCKER_RUNNING" != "true" ]; then
# restore backup of /etc/resolv.conf in case installation of resolvconf cleared it
sudo cp /etc/resolv.conf.orig /etc/resolv.conf
fi
# use python3 as default
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
# make compatible for Bookworm, which implements PEP 668
sudo python3 -m pip config set global.break-system-packages true
# VERSION of installation
# Get version number
VERSION_NO=`cat ${jukebox_dir}/settings/version-number`
# add used git branch and commit hash to version file
USED_BRANCH="$(git --git-dir=${jukebox_dir}/.git rev-parse --abbrev-ref HEAD)"
# add git commit hash to version file
COMMIT_NO="$(git --git-dir=${jukebox_dir}/.git describe --always)"
echo "${VERSION_NO} - ${COMMIT_NO} - ${USED_BRANCH}" > ${jukebox_dir}/settings/version
chmod 777 ${jukebox_dir}/settings/version
# Remove excluded libs, if installed - see https://github.com/MiczFlor/RPi-Jukebox-RFID/pull/2469
${pip_uninstall} -r "${jukebox_dir}"/requirements-excluded.txt
# Install required spotify packages
if [ "${SPOTinstall}" == "YES" ]; then
echo "Installing dependencies for Spotify support..."
# keep major verson 3 of mopidy
echo -e "Package: mopidy\nPin: version 3.*\nPin-Priority: 1001" | sudo tee /etc/apt/preferences.d/mopidy
sudo wget -q -O /etc/apt/keyrings/mopidy-archive-keyring.gpg https://apt.mopidy.com/mopidy.gpg
sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/${OS_CODENAME}.list
${apt_get} update
${apt_get} upgrade
call_with_args_from_file "${jukebox_dir}"/packages-spotify.txt ${apt_get} ${allow_downgrades} install
# not yet available on apt.mopidy.com, so install manually
local arch=$(dpkg --print-architecture)
local gst_plugin_spotify_name="gst-plugin-spotify_0.15.0.alpha.1-4_${arch}.deb"
wget -q https://github.com/kingosticks/gst-plugins-rs-build/releases/download/gst-plugin-spotify_0.15.0-alpha.1-4/${gst_plugin_spotify_name}
${apt_get} install ./${gst_plugin_spotify_name}
sudo rm -f ${gst_plugin_spotify_name}
# Install necessary Python packages
${pip_install} -r "${jukebox_dir}"/requirements-spotify.txt
local sudoers_mopidy="/etc/sudoers.d/mopidy"
# Include 'python' in the command to make testing later on easier. If this command fails it will not be included in the file.
local python_version=$(python -c 'import sys; print("python{}.{}".format(sys.version_info.major, sys.version_info.minor))')
echo "mopidy ALL=NOPASSWD: /usr/local/lib/${python_version}/dist-packages/mopidy_iris/system.sh" | sudo tee "${sudoers_mopidy}" > /dev/null
sudo chown root:root "${sudoers_mopidy}"
sudo chmod 440 "${sudoers_mopidy}"
fi
# always build lgpio as the pypi binaries are incomplete (armv6, python3.13) or broken (bullseye)
echo "Installing lgpio build dependecies..."
mkdir -p tmp && cd tmp && wget -q http://abyz.me.uk/lg/lg.zip && unzip lg.zip > /dev/null && cd lg && make && sudo make install
cd "${HOME_DIR}" && sudo rm -rf tmp > /dev/null
# Install more required packages
echo "Installing additional Python packages..."
${pip_install} -r "${jukebox_dir}"/requirements.txt
samba_config
web_server_config
# copy shell script for player
cp "${jukebox_dir}"/settings/rfid_trigger_play.conf.sample "${jukebox_dir}"/settings/rfid_trigger_play.conf